GMS 2 Manual says nothing about var

rIKmAN

Member
The manual does explain the use of var but it does so in the section reserved for Local Variables, it doesn't have an entry exclusively for the keyword var.
If you go to the manual, select the A-Z index and type "var" it appears in the results.
Click that it will load the Local Variables page that Tsuka linked.

Using "var" designates a variable as a "Local Variable", so that page is the correct page.
 

D_W

Member
also while in the IDE you can middle click on something and it will bring up the manual page for that thing. It's probably even better than using google or trying to search manually. Doing that for var brings up the Local Variable page.
 
Pro tip: Look for contents in the manual using Google and the site:manual.yoyogames.com query syntax.

You have the power of probably the biggest search engine the internet has to offer at your fingertips. Use it.
I am bad at using the right keywords when I use the google search engine, so I eventually had to ask here. The results I got was also not even close to being your manual.
I just wanna make it very clear that I tried what you said before asking here.
Also, I searched "var" in the manual but I got confused about what I got.

However I will have a look at the links you posted first.
 

Attachments

also while in the IDE you can middle click on something and it will bring up the manual page for that thing. It's probably even better than using google or trying to search manually. Doing that for var brings up the Local Variable page.
Thank you, I keep forgetting that all the time!
 

TsukaYuriko

☄️
Forum Staff
Moderator
I am bad at using the right keywords when I use the google search engine, so I eventually had to ask here. The results I got was also not even close to being your manual.
YoYo Games' manual, not my manual. I'm not YoYo Games. Not even affiliated. :)

site:manual.yoyogames.com var leads me directly to the Local Variables page as the first result.

I just wanna make it very clear that I tried what you said before asking here.
I'm not saying you didn't - just throwing it out there that the option exists for anyone who finds it useful. :)

Also, I searched "var" in the manual but I got confused about what I got.
Make sure to also check the Index and Glossary tabs. var on Index also leads me to Local Variables, for example.
 
One thing I wonder about that very page (the one about local variables) is that, what is meant by the variable being "discarded"?
Is it being reset? So that when the function or event (what is meant by an event in this case?) is carried out, it becomes 0 again if it initially was set to 0?
 

kburkhart84

Firehammer Games
Is it being reset? So that when the function or event (what is meant by an event in this case?) is carried out, it becomes 0 again if it initially was set to 0?
EXACTLY this. That is the very definition of "local variables." They exist only in the code section they are created in, and nowhere else. You could have a ton of functions use the same local variable names, and they won't interfere with each other. As soon as the function/event is over, the variable is discarded and no longer exists.

This is directly the opposite of what happens if you make a variable without the 'var' keyword. Those variables will stay around on whatever instance the code was ran on. That means if you initialize the variable to zero at the beginning of the step event, it will always start at zero. But if you happen to access it in another event(like the draw event), the variable will still be around, at whatever value it was when the step event was done. This is why in most cases you create these types of variables in a create event, so they don't get reinitialized back to some value every step. There are a few cases however where it still makes sense(like in code you want to make assumptions and then disprove them).
 

TsukaYuriko

☄️
Forum Staff
Moderator
Is it being reset? So that when the function or event (what is meant by an event in this case?) is carried out, it becomes 0 again if it initially was set to 0?
I wouldn't say it's being "reset", more like "destroyed". It'll be like the variable never existed. Say, you declare var thing = 0; in Create. If you then try to access thing in Step, your game will crash because there is no thing (anymore).

the function or event (what is meant by an event in this case?)
An object's event, like the Create event, Step event or Draw event. When you declare local variables, once the code reaches the end of the event or function it was declared in (when declaring a function in an event, the function scope takes precedence), they are discarded from memory.

Think of the end of a function as the curly brace that closes the function declaration, and the end of an event as the last line of code in that event. There are also other ways to end a function or event immediately, namely the return and exit keywords.
 
I wouldn't say it's being "reset", more like "destroyed". It'll be like the variable never existed. Say, you declare var thing = 0; in Create. If you then try to access thing in Step, your game will crash because there is no thing (anymore).
Oh, so it just doesn't exist anymore, and then it can be used again like nothing happened in a new function?
 

Mr Magnus

Viking King
Oh, so it just doesn't exist anymore, and then it can be used again like nothing happened in a new function?
correct.

var x; means "For the rest of this function/event/scope whenever I say x I mean this specific value. Once you're done with this function/event/scope x stops existing".
 
correct.

var x; means "For the rest of this function/event/scope whenever I say x I mean this specific value. Once you're done with this function/event/scope x stops existing".
Does that mean it's easier to copy objects and not make the variables interfere with one other and make all objects for example change color at the same time etc?
Or maybe variables can't influence other variables written in events of other objects?
 

TsukaYuriko

☄️
Forum Staff
Moderator
I'm not sure what you're trying to get at with the whole thing about copying objects... could you elaborate on that?

Variables can only influence things if
1) they are accessible under the current scope, with the added notion that you can change scope (with with) and/or access variables of different scope if you can identify them (e.g. variables in other instances if you know that instance's ID) and
2) you are actually accessing them and incorporating their values into your game logic.

Thus, a local variable declared via var can never affect anything that happens in any event or function other than the one it was declared in because it can not and will not exist (anymore) under those circumstances.
 

TailBit

Member
One misstake that sometimes happen is if you have a loop that runs a function, but the function does also run a loop with the same variable:
GML:
for(i=0;i<8;i++){
    my_draw_code(i);
}
GML:
// draws a row of 8 #
function my_draw_code(row){
    for(i=0;i<8;i++){
        draw_text(i*3,row*10,"#");
    }
}
if the function had been doing the loop to i<6 or lower, then it would be a neverending loop, using var i=0 's would make sure they don't interfer
 

chamaeleon

Member
One misstake that sometimes happen is if you have a loop that runs a function, but the function does also run a loop with the same variable:
[...]
if the function had been doing the loop to i<6 or lower, then it would be a neverending loop, using var i=0 's would make sure they don't interfer
And to stress why it is a problem, without using var, i becomes an instance variable, and as such is accessible both in the event code and the function being called, without going through all 8 iterations, only the first.

This means that when the my_draw_code() is done, and the event code is back in scope for execution, the loop over calls to my_draw_code() will finish because i is now 8 from the code in the function.

On the other hand, if the loop in the function only executed while i is less than 4, it would mean the event code after each call would see i is 4, do the increment, loop back because 5 is less than 8, call the function, see i is 4, do the increment to 5, loop back because 5 is less than 8, ad infinitum. The observed effect of this would be that the game appears to have hanged or crashed.
 
Does that mean it's easier to copy objects and not make the variables interfere with one other and make all objects for example change color at the same time etc?
Or maybe variables can't influence other variables written in events of other objects?
This sounds a bit more like you're getting confused about scope regarding objects and instances (possibly global variables, but I think it's more likely the former). The first thing to do is make sure you understand the difference between objects and instances. Ok, so we're on the same page now. If you use dot notation with the object name (example: obj_enemy.colour = c_white), you will indeed change the colour variable for every instance of obj_enemy in the game (or a single random instance of obj_enemy will have it's variable changed, depending on what target you are exporting to). If you want to only target one specific obj_enemy instance, you have to retrieve the ID of the instance (as outlined in Frosty's writeup I linked). This problem doesn't really have anything to do with var though (beyond being a scoping issue).

As a general guide, there are three types of variables: global, instance, and local. Changing global variables will definitely affect any instance that is reading from that global variable. Changing an instance variable will only affect multiple instances if you are incorrectly using the object name, instead of the instance ID, to refer to the instance. If you use the correct ID the instance variable will only be changed for that specific instance. As others have pointed out, local variables (variables with var preceding them) only last for the specific event or function they are in.

If you are thinking "I'm having a problem where multiple instances are getting their variable updated when I only want a single instance's variable to be updated...Ah! I'll use a local variable, that should fix it!" Then get ready for disappointment because that is not the purpose of local variables and using them to fix a problem like that will more than likely simply crash the game. Use an instance variable if you want to give an instance an "attribute" of some sort (such as colour), use a local variable if you don't want the variable to "bleed out" to other events/functions.

As an example, if you have a function that has a for loop which uses the variable i as its incrementor and you are running the function in an instance that already has a variable called i used for something, you would want to make sure the i variable in the for loop is a local variable by declaring it with var so that you aren't altering the i variable in the instance when the function is run (remember, local variables disappear after the event/function, so the instance itself has no idea that there was a variable called i inside the function as long as i is a local variable, if function i was not a local variable, it would overwrite the i variable in the instance).
 
Top