Convert string to integer

Camero2K
29 Jul 2021, 20:21

Hi! I'm currently making Los Dangerous and I want to imply a feature, but I have some problem.

I'm trying to make player be able to play poker. First, player enters the amount he'll bid, then he plays it and with a random chance, he wins four times of money (as it's played by four people on the table) or loses it. Depending on the amount put in the bid, there are three different conditions in an if/else if/else structure. If player bids more than the amount of money he/she has or it is under a specific value (I chose 50), player can't play poker.

However, the problem is that when I get the input (result), it takes the input as string, so it can't compare string to an integer value (player.money). So, how can I convert result to an integer so it can compare it with player money?


mrangel
29 Jul 2021, 21:20

Checking if what they typed is actually an integer:

if (IsInt (result)) {
  // rest of script goes here
}
else {
  msg ("That isn't a number!")
  // maybe you want to go back and ask them again here; or have them skip this round
}

Actually converting it to a number:

bid = ToInt (result)

Camero2K
30 Jul 2021, 07:37

Oh, thanks mrangel! From what I know, result always seems to be string, so ToInt will be the solution for me.
Thanks, again!


mrangel
30 Jul 2021, 08:57

Oh, thanks mrangel! From what I know, result always seems to be string, so ToInt will be the solution for me.

Yes, result will always be a string.
Just don't forget to check using IsInt before converting it.

(The function names are slightly confusing. IsInt doesn't check if a variable is an int - it checks if a string consists of digits. It tells you whether using ToInt on the same result will cause an error. It's always advisable to check that player input is what you expected. For example, if you ask the player to enter their bid and they type "fifty bucks", "help", or "leave table", you want it to give a helpful message rather than a script error)


DarkLizerd
31 Jul 2021, 19:00

Or... (And this is the magic or programming...)
Have poker chips, and place your bid that way...
IE:
[50] [25] [5] [1] [clear] [bid]
click on the object to add that to your bid.
[clear] will clear your bid
and [bid] will place your bet.

OH, one more thing to add...
Instead of a 1 in 4 chance to win, actually play a game of chance.
Each person get a card, 1 to 10.
Bet if you think you have high card, (or bluff, but that will require several rounds of betting.)
or throw your card in.
High card gets the pot, or split between high cards holders.