Jump to content


Photo

local variables in scripts


  • Please log in to reply
5 replies to this topic

#1 Manfrex

Manfrex

    GMC Member

  • GMC Member
  • 132 posts
  • Version:GM8

Posted 12 July 2012 - 03:42 PM

Hi! I have a really quick question. If i define a local variable in a script, such as: dog = "poodle"
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?
Thanks!
  • 0

#2 dannyjenn

dannyjenn

    GMC Member

  • GMC Member
  • 2087 posts
  • Version:Mac

Posted 12 July 2012 - 03:51 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 types of variables (global vs. instance vs. 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.

Edited by dannyjenn, 12 July 2012 - 03:52 PM.

  • 0

#3 Manfrex

Manfrex

    GMC Member

  • GMC Member
  • 132 posts
  • Version:GM8

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?
  • 0

#4 dannyjenn

dannyjenn

    GMC Member

  • GMC Member
  • 2087 posts
  • Version:Mac

Posted 12 July 2012 - 04:06 PM

Actually, you're right. Different people call them different things... "global" vs. "local" vs. "temporary" or "global" vs. "instance" vs. "local". I'll check what the official term is...

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.

  • 0

#5 howzer

howzer

    GMC Member

  • GMC Member
  • 67 posts
  • Version:GM7

Posted 12 July 2012 - 05:08 PM

I would take a guess that in the short term, they would be about the same. But in the long run temp variables will save memory and thus speed.
  • 0

#6 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 16820 posts
  • Version:GM:Studio

Posted 12 July 2012 - 07:00 PM

In GM:Studio the naming convention has been clarified... and it goes as follows (this is true for GM8 and 8.1 too):

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users