Author Topic: Cant remember if nested loops have no in-script memory.  (Read 376 times)

Legacy_Claiming Light

  • Newbie
  • *
  • Posts: 16
  • Karma: +0/-0
Cant remember if nested loops have no in-script memory.
« on: October 16, 2015, 06:38:25 am »


               

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.


 



In the nested loop below, I'm using a while statement to cycle through to the Correct, randomly chosen item from TheBox and updating TokenToMove each time.

 

When I later AssignCommand for TokenToMove, will the var remember that I made that change?

 

I guess I could just test, but I really prefer to have my ducks in a row before I start throwing darts.


 


while (TokenCount != 0)

{

Randomizer = Random(TokenCount);      //Get a random number, choosing 0 and TokenCount-1.

TokenToMove = GetFirstItemInInventory(TheBox);

 

        while (Randomizer !=0)

        {

        TokenToMove = GetNextItemInInventory(TheBox);

        Randomizer--;

        }

 

    AssignCommand(TheBox,ActionGiveItem(TokenToMove,TokenBag));

    TokenCount --;

    }

 


 


 



               
               

               
            

Legacy_Tarot Redhand

  • Hero Member
  • *****
  • Posts: 4165
  • Karma: +0/-0
Cant remember if nested loops have no in-script memory.
« Reply #1 on: October 16, 2015, 10:54:24 am »


               

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



               
               

               
            

Legacy_Proleric

  • Hero Member
  • *****
  • Posts: 1750
  • Karma: +0/-0
Cant remember if nested loops have no in-script memory.
« Reply #2 on: October 16, 2015, 11:01:57 am »


               

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.



               
               

               
            

Legacy_Claiming Light

  • Newbie
  • *
  • Posts: 16
  • Karma: +0/-0
Cant remember if nested loops have no in-script memory.
« Reply #3 on: October 16, 2015, 09:14:36 pm »


               

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.