Game Development Community

Type hadling in TGE1.5 script

by Nathan Bowhay - ESAL · in Torque Game Engine · 04/08/2008 (1:43 pm) · 3 replies

I came across this bug when using path cameras and talk about it on this page: http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=9690

But basically if you have the statement:

if (!(%speed = %node.speed))
%speed = %this.speed;

%speed gets truncated (casted to a int) so if %node.speed = 1.9 (is a float), %speed = 1 or if %node.speed = 0.5, %speed gets set to 0.

This is a big issue if you end up using statements like this.

This is the new working statement:

%speed = %node.speed;
if (%speed $= "")
%speed = %this.speed;

#1
04/08/2008 (1:58 pm)
It's actually a bit more complicated than that--you should not (in any programming language) check for equality of two float numbers--because of floating point precision, they are never guaranteed to be equal.

Instead, you should check to see if they are close to each other, along the lines of:

if (mAbs( %speed - %node.speed) > 0.0001)
{
    %speed = %this.speed;
}

You also are mixing the data types a bit yourself, when you use the line:

if (%speed $= "")
{

which in and of itself isn't wrong, but might generate results you don't expect.
#2
04/08/2008 (9:16 pm)
So in torque script how do you check for an undefined variable? cause in other high level languages you have something like undefined, what is the TGE script equivalent?

The idea behind the statement is that if %node.speed is undefined then set %speed equal to %this.speed else set %speed equal to %node.speed.

so in the statement mAbs( %speed - %node.speed) > 0.0001 how is the math handled if if %node.speed is undefined or if it is a string?
#3
04/08/2008 (10:42 pm)
Ok, that makes a bit more sense (I wasn't quite sure what your real goal was).

There are two ways--either the one you found ( $= ""), or setting it to an initialized state (-1 is a common one), and then seeing if that is set.

Float comparisons (to other floats, or to ints) still need to be used with the mAbs( x -y) > threshold technique--and that's true for TorqueScript, C++, and most programming languages--just the nature of how floats are stored internally.

TorqueScript will assume an undefined variable has a value of "" (as you noted), and when "cast" as an int, this will be treated as 0, and 0.0 if it needs a float.