Can't remember if this will work... I seem to recall loop statements forgetting about their changes to a declared variable once outside of the loop.
Can't remember if this will work... I seem to recall loop statements forgetting about their changes to a declared variable once outside of the loop.
Just declare the variables outside the loop(s) then they will be in scope when you exit said loop(s). It's not as though you will run out of memory for want of the few bytes involved.
TR
In a nutshell, this isn't a problem as long as you declare your variables at the start of the script.
In more detail, what you're asking about is called the "scope" of variables.
It all depends on where you declare the variable. You don't have any declarations in your example. For example,
object TokenToMove;
is a declaration.
Normal practice is to declare variables at the start of the script. In that case, any reference to the variable, whether inside a loop or not, is unambiguous, so the change to TokenToMove will be remembered for the duration of the script.
You can also declare variables inside a block of code, such as a loop. For example,
while (Randomizer !=0)
{
int TokenToMove;
TokenToMove = GetNextItemInInventory(TheBox);
Randomizer--;
}
In that case, the scope of the variable is only inside the loop. If you also declared TokenToMove at the start of the script (which would be necessary to avoid a compiler error), NWScript behaves as though you have two variables, TokenToMove1 outside of the loop, and TokenToMove2 inside. So, the change to TokenToMove inside the loop would not affect the value outside of the loop.
Some scripters prefer to declare all variables at the top of the script, to avoid this confusion. Arguably, in a very long script, it's easier to understand if variables which are only used in one block are declared there, but it's potentially dangerous.
When a script stops, all variable values are forgotten. If you need to refer to a variable when the script starts again, or in a different script, use local variables on an object.
Ah-that's what it was! Declaring a variable inside a loop limits it to that loop. I knew there was something, I just couldn't remember what it was.
Sweet. Than it's back to work. Thanks guys.