• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Gamemaker Local Variables

E

Emberex

Guest
I have this code where I want an instance to be created as long as the circumstances are met. This is the code:
GML:
if(hAmount < hMax){
    var spawnCounter = 0;
        spawnCounter += 1;

    if(spawnCounter >= 3*room_speed){
        var sRange = irandom_range(65,80);
        instance_create_layer(x + sRange,y + sRange,"Instances", oHuman);   
    }       
}
I understand why the instance is not being created. The
GML:
 Var spawnCounter
is being reset back to zero too fast. I was hoping to use a local variable for this to save variables and usage. So, how do you go around this situation, or, do you not?
 

samspade

Member
Unfortunately, you can't use local variables like that. Local variables essentially are destroyed once the event or function is done. So the local variable spawnCounter will always be reinitialized to 0 every time this runs. This would also be true if you used any other type of variable (as setting it to 0 will set the variable to 0) but if you used an instance variable, you could initialize the spawnCounter variable in the create event.


If you want to learn more about local variables (and scope in general) here are a couple more resources.

Manual on Scope

 
E

Emberex

Guest
Unfortunately, you can't use local variables like that. Local variables essentially are destroyed once the event or function is done. So the local variable spawnCounter will always be reinitialized to 0 every time this runs. This would also be true if you used any other type of variable (as setting it to 0 will set the variable to 0) but if you used an instance variable, you could initialize the spawnCounter variable in the create event.


If you want to learn more about local variables (and scope in general) here are a couple more resources.

Manual on Scope

I thought so, but I was hoping to use a var that would be erased when done. Quick question, this is in the example in the help pages, wouldn't this be set to 0 everytime as well?
GML:
var i = 0;
repeat (10)
   {
   inventory[i] = 0;
   i+=1;
   }
 

samspade

Member
I thought so, but I was hoping to use a var that would be erased when done. Quick question, this is in the example in the help pages, wouldn't this be set to 0 everytime as well?
GML:
var i = 0;
repeat (10)
   {
   inventory[i] = 0;
   i+=1;
   }
Yes and no depending upon what you mean. It will be set to 0 every time the line var i = 0 runs but only at that point, so during the repeat loop i will be increased without being reset.

Maybe another way to think about it is that the scope of the variable isn't what is setting it to 0. The line i = 0 is what is setting it to 0 and it will always do so regardless of the scope. Scope determines how long the variable persists and how you can access it. (However, since local variables are destroyed/go away at the end of the function/event that they are in they must be declared before use in the function/event - so with local variables you're always going to be assigning a value to them in the function or event that is running them, making the unsuitable to carry a value that must persist over multiple frames/steps).
 
E

Emberex

Guest
Yes and no depending upon what you mean. It will be set to 0 every time the line var i = 0 runs but only at that point, so during the repeat loop i will be increased without being reset.

Maybe another way to think about it is that the scope of the variable isn't what is setting it to 0. The line i = 0 is what is setting it to 0 and it will always do so regardless of the scope. Scope determines how long the variable persists and how you can access it. (However, since local variables are destroyed/go away at the end of the function/event that they are in they must be declared before use in the function/event - so with local variables you're always going to be assigning a value to them in the function or event that is running them, making the unsuitable to carry a value that must persist over multiple frames/steps).
Okay, thank you for the help!
 

TheouAegis

Member
If a variable needs to be maintained across multiple steps, do not put var before it. If a variable needs to be maintained across multiple events, do not put var before it. I'm not sure how local variables behave with functions now, but prior to GMS2.3 you could only pass local variables between scripts as arguments, which could only be read and slowed down script calls. Use var for iterators (e.g. for(var i=0; i<10; i++)), temporarily making a variable global for quick reading across multiple instances within a loop (e.g. var X=x; with oEnemy if abs(x-X)<16 {instance_destroy();}), or as temporary accumulators for calculations which would otherwise be performed multiple times or to keep lines uncluttered.

Personally -- and this from a guy who uses Alarms and path_* variables as a "free" variables -- if your initial idea is to toss var into a code and your code doesn't work consequentially, just ditch the var and initialize the variable in the Create event rather than fussing over trying to get your code to work with var. Overtaxing the CPU is a greater concern than a few bytes of RAM. Even if you never use var for anything other than iterators in loops, it's unlikely anyone will accuse your program's variables using too much RAM, since your graphics and audio will be the biggest hit to RAM.
 
Top