Game Development Community

convert string to integer

by Louis Marchant · in Torque 3D Beginner · 10/23/2012 (10:24 am) · 5 replies

basically, i have a line of code as follows :

where $variableA1 is always an integer
and numericalMoneyHUD is always a numerical value also, albeit stored as text because it's a GUI control

if(numericalMoneyHUD.text >= $variableA1)

now this doesnt work, i can only assume because i need to convert the *.text to an integer, have checked the docs and can't find anything.

solution much appreciated, thanks :)

#1
10/23/2012 (1:53 pm)
TorqueScript treats all data as strings until it needs to do something like compare integers. Try typing this in the console:
==> "123" > 5
1
The comparison may be unexpected if you have non-numeric data in your string:
==> "Hello" > 5
0
==> "Hello" < 5
1
But it will work if the number's at the start of a string, apparently:
==> "123data" > 5
1
#2
10/23/2012 (2:12 pm)
Might want to try:

$variableTemp1 = numericalMoneyHUD.text;

if( $variableTemp1 >= $variableA1 )
doSomething();
#3
10/23/2012 (2:16 pm)
@louis, I can't reproduce your error, what gui text control are you using?
#4
10/24/2012 (7:29 am)
i'm pretty sure guicontrol.text doesnt work, you need to use guicontrol.getText().

I'm sure this is the solution, however you should check teh validity of your data when errors like this occur, the other side effect of torquescript is it makes assumptions and we all know what assumptions are.

I often uses messageboxes to throw out data to the screen as i'm working, just to see whats going on.
#5
10/24/2012 (8:19 am)
yea bloodknight, i just thought of that, and tried it, and yea it works with *.getText

my VB days comming back to haunt me, lost habbit of using *.get*

thanks for all the responses people. i'm sure it will come in handy knowing all variables are strings too, sot hanks for that especially daniel.