Game Development Community

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.

#1
02/25/2009 (10:35 am)
Yes it definately does, With PUAP_SCRIPT_CHANGE, you must initialize your variables.

Of course, that's poor practice not too(try doing that in C), but Torquescript would allow for it before.