Newbie question - how to set maximum/minimum number for status attribute?

Hi, I'm very new to Quest (and to scripting in general). In my game I'm trying to add status attributes for characteristics such as intelligence, kindness etc that affect dialogue and actions, where 100 would be very intelligent and 0 would be really stupid, or 100 would be altruistic and 0 would be cruel/sadistic.

The problem is I'm not sure on how to cap these attributes at 100 and have a minimum of 0 to prevent it going into negatives.

Can anyone teach me how to do this? I'm not even sure where to begin - I assume in the game object's scripts section?


Io

There's a lot of different ways to do this.

What I would do is use Changescripts.

Basically, let's say you have the object FriendlyWizard. And he has attribute Intellect, which will range from 0-100.

When you create that attribute, select it and click the tiny button with a 'plus' sign to create a Changescript.

A changescript is basically just "Whenever this object's attribute changes, do this."

Under the changescript you put code dictating how high and how low it can go, something like this:

if (FriendlyWizard.Intellect>100){
FriendlyWizard.Intellect=100
}
else if (FriendlyWizard.Intellect<0){
FriendlyWizard.Intellect=0
}

And this handles it!

If you want to change the mins and maxes - like, say, the Wizard drinks a super magic potion and now they can go up to 200 - you instead have additional attributes for those: FriendlyWizard.MaxInt and FriendlyWizard.MinInt, in place of the 100 and 0.

Hope this helps!


Very helpful, thank you! Follow up question regarding it, so I put in:

  player.Intelligence = 100
}
else if (player.Intelligence<0) {
  player.Intelligence = 0
}

But my issue is when the game starts, it requests the player to input their starting intelligence and other attributes, but the player can type literally anything in (for example, if I type the word "tiger", then the status attribute will say Intelligence: tiger)

How can I limit it so that the player can only type in a number between 0 and 100?

Thank you for your help!


You need to check the result the player typed.

The function IsInt will test if a string can be turned into a number; and the function ToInt will turn the string 100 into the number 100.

So you'd create a function (maybe call it something like SetIntelligence) that asks the question, and instead of just assigning the response to an attribute, you'd do something like:

  if (not IsInt (result)) {
    msg ("Please enter a number.")
    // call the function to ask the question again
    SetIntelligence()
  }
  else {
    intresult = ToInt (result)
    if (intresult < 0 or intresult > 100) {
      msg ("Intelligence must be between 0 and 100
      // call the function to ask again
      SetIntelligence()
    }
    else {
      player.intelligence = intresult
    }
  }

(filler for getting my edited post, updated/posted)


your typed-in input for the 'get input' Script/Function is always taken as a String Value, however, you can then check it, and/or convert it to other Data Types

https://docs.textadventures.co.uk/quest/elements/

https://docs.textadventures.co.uk/quest/types/ (Data Types)

https://docs.textadventures.co.uk/quest/scripts/

https://docs.textadventures.co.uk/quest/functions/ (categorical order)
https://docs.textadventures.co.uk/quest/functions/index_allfunctions.html (alphabetical order)

https://docs.textadventures.co.uk/quest/functions/string/isnumeric.html (checks if string value can be converted to an integer or to a double, basically it is just doing both 'IsInt' and 'IsDouble' Scripts/Functions)

https://docs.textadventures.co.uk/quest/functions/isint.html (checks if string value can be converted to an integer)
https://docs.textadventures.co.uk/quest/functions/isdouble.html (checks if string value can be converted to a double)

https://docs.textadventures.co.uk/quest/functions/toint.html (converts a string to an integer)
https://docs.textadventures.co.uk/quest/functions/todouble.html (converts a string to a double)

<game name="EXAMPLE">

  <attr name="start" type="script">

    <![CDATA[

      msg ("Strength? (0 to 100)")

      get input {

        // quest automatically (hidden from you), STORES your String Value input into the built-in 'result' Variable, and because it's a String Value, quest causes (its able to parse the value for you, quest is programmed very powerfully/usefully with parsing) the 'result' Variable to be a String Variable:

        // result = YOUR_TYPED_IN_INPUT_AS_A_STRING_VALUE

        // the 'IsInt' Script/Function checks if the input value can be converted into Integer Value (aka, if the input is: ..., -999, -1, 0, 1, 999, ...):

        if (IsInt (result)) {

          // the 'ToInt' Script/Function actually converts your string value input into an integer value, and then we're storing it into an Integer Variable (my custom 'input_integer_variable' as seen below):

          input_integer_variable = ToInt (result)

          // we then check if its in-bounds (0 to 100) or not:

          if (input_integer_variable >= 0 and input_integer_variable <= 100) {

            player.strength_integer_attribute = input_integer_variable

          } else {

            msg ("Error: wrong input: type in a number from 0 to 100")

            invoke (game.start) // loops (calls this entire scripting: does it again)

          }

        } else {

          msg ("Error: wrong input: type in an integer number")

          invoke (game.start) // loops (calls this entire scripting: does it again)

        }

      }

    ]]>

  </attr>

</game>

to do this in the GUI/Editor:

add new script -> 'scripts' section/category -> 'if' Script -> (see below)

if [EXPRESSION] IsInt (result)

-> then -> add new script

else

-> add new script

----------

add new script -> 'scripts' section/category -> 'if' Script -> (see below)

if [EXPRESSION] input_integer_variable >= 0 and input_integer_variable <= 100

-> then -> add new script

else

-> add new script

--------------

add new script -> 'variables' section/category -> 'set a variable or attribute' Script (see below)

set variable input_integer_variable = [EXPRESSION] ToInt (result)

------------

add new script -> 'variables' section/category -> 'set a variable or attribute' Script (see below)

set variable player.strength_integer_attribute = [EXPRESSION] input_integer_variable

P.S.

here's the built-in randomization Scripts/Functions:

https://docs.textadventures.co.uk/quest/functions/corelibrary/diceroll.html

https://docs.textadventures.co.uk/quest/functions/getrandomint.html

https://docs.textadventures.co.uk/quest/functions/getrandomdouble.html

https://docs.textadventures.co.uk/quest/functions/corelibrary/randomchance.html

examples:

player.strength_integer_attribute = DiceRoll ("1d6")

// you're rolling a normal (6-sided) single die: 1 dice with 6 sides

// so, in this case/example, your strength will only be randomly: 1 to 6

-----------------

player.strength_integer_attribute = GetRandomInt (0,100)

---------------

create ("katana")

katana.damage_double_attribute = 50.0

player.weapon = katana

player.damage_double_attribute = player.weapon.damage_double_attribute + player.weapon.damage_double_attribute * GetRandomDouble ()

// player.damage_double_attribute = [50.0] + [ (50.0) * (0.0 to 1.0, EXCLUSIVE, I think, meh) ]

// so, let's say that the 'GetRandomDouble' returns/gets/selects: 0.5

// player.damage_double_attribute = [50.0] + [ (50.0) * (0.5) ]
// player.damage_double_attribute = [50.0] + [25.0]
// player.damage_double_attribute = 75.0

----------------

boolean_variable = RandomChance (75)

// true: 75%
// false: 25%

if (boolean_variable) {

  // true: 75%

  msg ("The death spell cast against you, was successful")

  msg ("You were killed by the death spell")

  msg ("GAME OVER")

  finish

} else {

  // false: 25%

  msg ("The death spell cast against you, failed")

  msg ("You survived the death spell, you're still alive")

]

-----------

example of how Negation ('NOT') works:

boolean_variable = RandomChance (75)

// true: 75%
// false: 25%

if (not boolean_variable) {

  // false: 25%

  msg ("The death spell cast against you, failed")

  msg ("You survived the death spell, you're still alive")

} else {

  // true: 75%

  msg ("The death spell cast against you, was successful")

  msg ("You were killed by the death spell")

  msg ("GAME OVER")

  finish

]

------------------

example 2 of how Negation ('NOT') works:

(I hope I got this, the logic, right, lol)

boolean_variable = not RandomChance (75)

// true: 25% // (75% true -> NOT -> 75% false -> 25% true)
// false: 75% // (25% false -> NOT -> 25% true -> 75% false)

if (boolean_variable) {

  // true: 25%

  msg ("The death spell cast against you, was successful")

  msg ("You were killed by the death spell")

  msg ("GAME OVER")

  finish

} else {

  // false: 75%

  msg ("The death spell cast against you, failed")

  msg ("You survived the death spell, you're still alive")

]

Log in to post a reply.

Support

Forums