local variables in scripts
Posted 12 July 2012 - 03:42 PM
In what object is the local variable dog saved? Also, which is the best, most resource-efficient way to use variables within scripts? Are temporary variables a good choice? Why?
Posted 12 July 2012 - 03:51 PM
Edited by dannyjenn, 12 July 2012 - 03:52 PM.
Posted 12 July 2012 - 03:56 PM
I could be wrong, but I believe that when a non-existing variable is set to something in the script it becomes an instance variable of the instance that called the script. Which is not the same thing as a "local" variable, although people sometimes call it that. A local variable is a variable declared with the var keyword. It's completely temporary and goes away when the script completes (i.e. it does not become an instance variable). All variables (global, instance, or local) use up the same amount of memory so none are more "efficiant" than the others... the difference is that global variables will never go away, instance variables will only go away when the nstance is destroyed, and local variables always go away. So it really depends on what you need the variable for.
Wait a moment... Are you saying that local variables and temporary variables are the same? I have been taught (here on the forum) that temporary variables are declared using "var", and that they are removed from RAM as soon as the event, or script ends. The local variables however, are specific to the certain instance of an object that they are declared in, and are removed from RAM when the instance is removed... now i'm confused...
If it is the way you say it is, what happens when several objects call the same script?
Posted 12 July 2012 - 04:06 PM
edit - I've checked the manual and it seems kind of ambiguous. It doesn't really say what either are called... it just says that they are "local to the instance" for the instance variables and "only within the current piece of code or script" for the temporary variables. However, my point is that a variable not declared with the var keyword will become a local instance variable for that instance and will only go away when the instance is destroyed. But if you use the var keyword it remains as a temporary script variable and goes away as soon as the script ends.
Also, it seems I was wrong about efficiency... the manual says that global variables are slower than other variables. I'm not sure which is faster... a local instance variable or a temporary script variable.
Edited by dannyjenn, 12 July 2012 - 04:16 PM.
Posted 12 July 2012 - 05:08 PM
Posted 12 July 2012 - 07:00 PM
global variables : Available to all code blocks and scripts and changing it in one will change it for all. belongs to no one instance, but rather to the whole game and cannot be removed once created.
instance variables : these are created in the instance and are available to all code blocks within that instance. Individual instances can have the same variables with different values. These are removed when the instance with them is destroyed.
local variables (var declared) : these are variables local to the code block or script (in Studio they are local to the event) and can be used by all instances that are involved in the script or code block but once the code is run they cease to exist.
As for speed, the difference is so negligible that you should just go with what you need at the time! if you can do something with ten instance variables, or with one global, go with the global!!!! When dealing with variables, the performance hit is almost non existant unless you have millions of them.
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users