UbuntuJackson
Member
Hey I wonder why does it say nothing about var in the gamemaker manual?
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.The manual does explain the use ofvar
but it does so in the section reserved for Local Variables, it doesn't have an entry exclusively for the keywordvar
.
This is important to remember as many things don't show up if searched in some of the search tabs. Search in all of them for the keywords if one doesn't return a result.If you go to the manual, select the A-Z index and type "var" it appears in the results.
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.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.
Thank you, I keep forgetting that all the time!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 forvar
brings up the Local Variable page.
YoYo Games' manual, not my manual. I'm not YoYo Games. Not even affiliated.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'm not saying you didn't - just throwing it out there that the option exists for anyone who finds it useful.I just wanna make it very clear that I tried what you said before asking here.
Make sure to also check the Index and Glossary tabs. var on Index also leads me to Local Variables, for example.Also, I searched "var" in the manual but I got confused about what I got.
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.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 declareIs 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?
var thing = 0;
in Create. If you then try to access thing in Step, your game will crash because there is no thing (anymore).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.the function or event (what is meant by an event in this case?)
Oh, so it just doesn't exist anymore, and then it can be used again like nothing happened in a new function?I wouldn't say it's being "reset", more like "destroyed". It'll be like the variable never existed. Say, you declarevar thing = 0;
in Create. If you then try to access thing in Step, your game will crash because there is no thing (anymore).
correct.Oh, so it just doesn't exist anymore, and then it can be used again like nothing happened in a new function?
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?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".
for(i=0;i<8;i++){
my_draw_code(i);
}
// draws a row of 8 #
function my_draw_code(row){
for(i=0;i<8;i++){
draw_text(i*3,row*10,"#");
}
}
var i=0
's would make sure they don't interferAnd to stress why it is a problem, without usingOne 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, usingvar i=0
's would make sure they don't interfer
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.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.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.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: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?
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).var
preceding them) only last for the specific event or function they are in.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).