Script gotcha...
by Tim McClarren · in Torque Game Engine · 07/23/2008 (5:30 pm) · 2 replies
==> echo("1" == 1);
1
==>echo("16967429" == 16967429);
0
This has to do with the way the operator "==" is implemented in the engine.
I don't have an easy fix for this, but suffice it to say, I don't think:
F64 consoleStringToNumber(const char *str, StringTableEntry file, U32 line)
is implemented very well.
The reason the latter example fails is because "16967429" is converted to 16967428
by this function, because it's using 'atof', which is, well, not good in many circumstances.
(as an example, ask it to give you the floating point value of the string "3.1").
I'm not sure why floats are used in a bunch of the comparison operators inside the
script engine -- it seems particularly egregious that the "==" operator uses them.
Check compiler.cc, "IntBinaryExprNode::getSubTypeOperand()"...
1
==>echo("16967429" == 16967429);
0
This has to do with the way the operator "==" is implemented in the engine.
I don't have an easy fix for this, but suffice it to say, I don't think:
F64 consoleStringToNumber(const char *str, StringTableEntry file, U32 line)
is implemented very well.
The reason the latter example fails is because "16967429" is converted to 16967428
by this function, because it's using 'atof', which is, well, not good in many circumstances.
(as an example, ask it to give you the floating point value of the string "3.1").
I'm not sure why floats are used in a bunch of the comparison operators inside the
script engine -- it seems particularly egregious that the "==" operator uses them.
Check compiler.cc, "IntBinaryExprNode::getSubTypeOperand()"...
About the author
#2
07/24/2008 (12:07 pm)
Even more interesting example:function troubleTest(%value)
{
// setup an object as if it were the %value'th object to be created so far:
new ScriptObject(foo);
foo.setId(%value); // just a plain wrapper around C++'s setId().
echo(foo.getId() SPC "==" SPC %value SPC ":" SPC (foo.getId() == %value));
echo(foo.getId() SPC "$=" SPC %value SPC ":" SPC (foo.getId() $= %value));
foo.delete();
}[7/24/2008 12:07:32][Inf][General] ==>troubleTest(16967428); [7/24/2008 12:07:32][Inf][General] 16967428 == 16967428 : 1 [7/24/2008 12:07:32][Inf][General] 16967428 $= 16967428 : 1 [7/24/2008 12:07:34][Inf][General] ==>troubleTest(16967429); [7/24/2008 12:07:34][Inf][General] 16967429 == 16967429 : 0 [7/24/2008 12:07:34][Inf][General] 16967429 $= 16967429 : 1
Associate Orion Elenzil
Real Life Plus
function troubleTest(%value) { // setup an object as if it were the %value'th object to be created so far: new ScriptObject(foo); foo.setId(%value); // just a plain wrapper around C++'s setId(). %comparison = (foo.getId() == %value); echo(foo.getId() SPC "==" SPC %value SPC ":" SPC %comparison); foo.delete(); }yields:(yes, this implies that we've got simIDs > 2^24, but that's a separate issue.
as a side note, we can confirm that simIDs work great up to 2^24! )