result Error

GAGE HOLSTON
10 Dec 2017, 16:12

I'm using this script, the player needs to put in an integer, the game checks if it is and if it is an odd number.

msg ("OK.")
player.colorbet = "Red"
msg ("Type a number to bet on.
Since you bet on red, you can only select odd numbers from 1-36.")
EnableTurnScript (redblackloop)
get input {
if (IsInt(result)) {
player.straightbet = result
if (player.straightbet % 2 = 0) {
msg ("That is an even number. Since you chose red, you CANNOT bet on an even number.
Pick again.")
return (false)
}
else {
if (result < 1) {
msg ("You MUST pick a number between 1 and 36.")
return (false)
}
else {
if (result > 36) {
msg ("You MUST pick a number between 1 and 36.")
return (false)
}
else {
DisableTurnScript (redblackloop)
player.straightbet = result
msg ("You put {player.wager} dollars on {player.colorbet} {player.straightbet}.")
RouletteGame.insidebet = "Straight"
}
}
}
}
else {
msg ("That is not a number. Try again.")
}
}

When I test it out, I keep getting the same error when I enter any number.

Error running script: Error compiling expression 'player.straightbet % 2 = 0': ArithmeticElement: Operation 'Mod' is not defined for types 'String' and 'Int32'

I do not understand exactly what's wrong here.


K.V.
10 Dec 2017, 16:50

Hello.

When you get input, result is a string, so you have to change it to an integer.

Try this:

EnableTurnScript (redblackloop)
get input {
  if (IsInt(result)) {
    result = ToInt(result) //KV added this one line to the script
    player.straightbet = result
    if (player.straightbet % 2 = 0) {
      msg ("That is an even number. Since you chose red, you CANNOT bet on an even number.
      Pick again.")
      return (false)
    }
    else {
      if (result < 1) {
        msg ("You MUST pick a number between 1 and 36.")
        return (false)
      }
      else {
        if (result > 36) {
          msg ("You MUST pick a number between 1 and 36.")
          return (false)
        }
        else {
          DisableTurnScript (redblackloop)
          player.straightbet = result
          msg ("You put {player.wager} dollars on {player.colorbet} {player.straightbet}.")
          RouletteGame.insidebet = "Straight"
        }
      }
    }
  }
  else {
    msg ("That is not a number. Try again.")
  }
}

NOTE:

I can't get into my Windows right now, and I can get Quest to create games, but not PLAY games under Wine in Linux...

So, I can't test anything out at the moment.


hegemonkhan
11 Dec 2017, 18:45

@ Gage Holston:

there's also an error in the logic of your scripting... you need the 'number range check' to be before your 'even/odd check', or to say it another way, you need the 'even/odd check' to be within the 'number range check'. The reason is that you can input '101' which would be an 'odd' number, but it's out of range/bounds of your between 0 and 37 desired requirement, and this is wrongly occuring first, meaning that it's not prevented, as it should be prevented (as it's a number that is out of range/bounds from your desired range/bounds of being between 0 and 37).

here's the fix (and cleaned the code up a bit too):

<![CDATA[
  EnableTurnScript (redblackloop)
  get input {
    if (IsInt (result)) {
      input_integer_variable = ToInt (result)
      if (input_integer_variable > 0 and input_integer_variable < 37) {
        if (input_integer_variable % 2 = 0) {
          // K.V. can fix this up for you, using the '<br>' stuff (to shorten the below 'msgs' to a single 'msg' code line), as I'm not that familiar with exactly how to get it to work, with regards to, and, as the 'cdata' tag, is needed for using the 'greater/lesser than' symbols/operations/operators
          msg ("That is an even number. Since you chose red, you CANNOT bet on an even number.")
          msg ("")
          msg ("Pick again.")
          return (false)
        } else { // if number is odd
          DisableTurnScript (redblackloop)
          player.straightbet = input_integer_variable
          msg ("You put {player.wager} dollars on {player.colorbet} {player.straightbet}.")
          RouletteGame.insidebet = "Straight"
        }
      } else { // if number is not between 0 and 37 (aka: out of range/bounds error)
        msg ("You MUST pick a number between 1 and 36.")
        return (false)
      }
    } else { // if not an integer data type
      msg ("That is not a number. Try again.")
    }
  }
]]>