Getting Integer Input

Rydhme
I have try to getting an 'integer' input from player. I'm still new to quest.
I want player inputs became integer, not string.
I have try a function 'ToInt', but i get an error message, Error running script: Function not found: 'ToInt'

msg ("How Many ?")
get input {
ToInt (result)
if (player.money <= ( 10 * result )) {
msg ("You cant afford it")
}
else {
bricks.count = bricks.count + result
AddToInventory (bricks)
}
}

That's in-game buy command's code (at the Shop). I think there's no problem with variables and attributes.

HegemonKhan
you almost got it... (and very good job!, as understanding that you need to set~make your input into a Type:Integer, is the more difficult concept~logic part for people new to coding, hehe. You only are missing the proper syntaxing of how to do it).

the 'ToInt (result)', must be set to something... as it is an Expression:
......................................................................../
.......................................................................V
Object_name.Attribute_name = Value_or_Expression
~OR~
variable_string_name = Value_or_Expression

so... in the below example:

variable_string_name = Expression
result2 = ToInt (result)

msg ("How Many ?")
get input {
// the 'get input' automatically (hidden from you) sets its built-in Variable: result = your_typed_in_input
// this method however will have an error if the person playing the game doesn't type in a non-decimal number...
// but now we need to convert it to being an Integer (and have to set it again to another VARIABLE, for this example, a local Variable, which I'm labeling~naming~calling it as 'result2', but you can call it whatever you want):
result2 = ToInt (result)
// now, we can use our 'result2' local Variable below:
if (player.money <= 10 * result2) {
msg ("You cant afford it")
}
else {
bricks.count = bricks.count + result2
AddToInventory (bricks)
}
}


I think this way would work too instead of the above way using a 2nd Variable (result2), but you still got the same issue~error if the person doesn't type in a non-decimal number:

msg ("How Many ?")
get input {
if (player.money <= 10 * ToInt (result)) {
msg ("You cant afford it")
}
else {
bricks.count = bricks.count + ToInt (result)
AddToInventory (bricks)
}
}


alternatively (and better coding design: 'Error and Exception Handling' pre-emptive coding), you can check the input if it's an Integer:

http://docs.textadventures.co.uk/quest/ ... isint.html

msg ("How Many ?")
get input {
if (IsInt (result)) {
if (player.money <= 10 * ToInt (result)) {
msg ("You cant afford it")
} else {
bricks.count = bricks.count + ToInt (result)
AddToInventory (bricks)
}
} else {
msg ("Wrong input, try again, using a non-decimal number this time, please.")
}
}

HegemonKhan
sorry, my apologies as I had some mistakes, which I just edited in some corrections with my code, and I also added in some additions too, with my previous post. Now my previous post's codings should work...

Rydhme
I understand, and it works ! Thanks !

HegemonKhan
p.s.

let me know if you want to place a limit upon just how many (quantity of) items you can buy~have~hold (as right now, if you got tons of money you can buy tons of bricks, lol), if you can't figure out how to do it yourself.

(quite creative~intelligent with your code-logic Expression~math equation, with the pricing~your_money limit you did!, by the way, too, hehe)

---------

HK edit:

p.s.s.

here's Sora's Stackable Library for item usage:

viewtopic.php?f=18&t=3515

not sure if I can help though with it if you got problems with trying to use it, as it is beyond my coding level, lol

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums