Attribute Variable not working...

I am making a short maze game as a companion with some programming at my child care centre. They have robots they made that have different ATK, DEF and BATTERY life. I am running an Initialization script where they can enter their names and the values of their robots.
Unfortunately, when I start trying to remove from those values when they get damaged in the first room I get this error: Error running script: Error compiling expression 'player.Battery -1': ArithmeticElement: Operation 'Subtract' is not defined for types 'String' and 'Int32'
From what I read this means it doesn't recognize player.Battery. My Initialization looks like this:

msg ("What is your name?")
get input {
set (player, "alias", result)
msg ("What is your Attack power?")
get input {
set (player, "Attack", result)
msg ("What is your Defense power?")
get input {
set (player, "Defense", result)
msg ("What is your Battery power?")
get input {
set (player, "Battery", result)
msg ("Welcome to the maze, " + player.alias + "!")
MakeExitVisible (ENTER)
}
}
}
}

I tried adding set variable in front, but it kept deleting "variable". I am an incredible noob at this. Any and all help is greatly appreciated. To recap: I want to be able to subtract from values the player enters at the beginning of the game.


By default, result has a string value. But you want an integer value for player.Battery. ToInt(result) will convert a string (e.g., "five") into the integer 5. But what if the player inputs something that is not an integer? IsInt(result) can test for that.

Try this:

msg ("What is your Battery power?")
get input {
  if (IsInt(result)) {     // if "result" can be converted to integer, then equals "true"
    if (ToInt(result) < 0) {     // if "result" is a negative integer...
      msg ("Please enter a positive number...")
    }
    else {
      player.Battery = ToInt(result)
    }
  }
  else {     // "result" is a string that cannot be converted to an integer
    msg ("Please enter a whole number...")
  }
}

Oh, and official documentation for all the Quest functions: http://docs.textadventures.co.uk/quest/functions/


Thanks Dcoder!


Log in to post a reply.

Support

Forums