Script variable scope persistence
by Dave Young · in iTorque 2D · 02/24/2009 (9:14 pm) · 1 replies
I experienced a new script behavior this week, and I did NOT imagine it ;)
I had a function that looked something like this:
function doSomething(%what)
{
if(%what==1)
%x++;
error("%X " @ %x);
}
That would get called when I clicked a button. Running straight out of iTGB (Project->Run Project) I saw in the console that %x was actually persisting its values across calls and increasing over time.
Moral of the story is: if you want to be sure, remember to initialize your variables. I've never seen this in TorqueScript before after hundreds of thousands of lines, maybe it has something to do with the script optimizations that were made last release.
I had a function that looked something like this:
function doSomething(%what)
{
if(%what==1)
%x++;
error("%X " @ %x);
}
That would get called when I clicked a button. Running straight out of iTGB (Project->Run Project) I saw in the console that %x was actually persisting its values across calls and increasing over time.
Moral of the story is: if you want to be sure, remember to initialize your variables. I've never seen this in TorqueScript before after hundreds of thousands of lines, maybe it has something to do with the script optimizations that were made last release.
Torque 3D Owner Mat Valadez
Default Studio Name
Of course, that's poor practice not too(try doing that in C), but Torquescript would allow for it before.