Posts: 1,128
Threads: 43
Joined: May 2009
Quote:Originally Posted by Trickster at HBGames.Org
Global Variables should generally not be used. Global Variables are not an Object Oriented construct and violates information hiding. You do not want all of the scripts knowing about a variable, you only want your variables shown to methods/classes/scripts that need to view and perform operations on it. The only global variables that should be added are new $data_ and $game_ objects. System global variables created by RMXP (ex. $DEBUG and $scene are allowed). Global variables also consume more memory since they are not removed until the program terminates. The use of bad global variables is frowned upon, because you are cheating yourself, your script, and the users of your script.
Ok for the good OO practice, but the reasoning about the memory does not convince me. Because, the same memory that a global would keep reserved until the program terminates, will be used inside $game_temp, for instance, until the program terminates as well. Only local variables get removed at the end of the method in which they were defined.
Besides, in a way, the use of $game_temp "violates" information hiding as well. :geek:
Posts: 1,664
Threads: 391
Joined: May 2009
Wow... this has turned into a discussion thread. O_O' (moves)
Posts: 6
Threads: 0
Joined: May 2009
I wouldn't consider a byte of memory a "resource hog" anyway. In bigger programs that are built by multiple people, it becomes important to manage data better than just using a heap of globals. Programming teachers will tell you not to use globals really just for the practice: showing that programs can be built without ever using a global.
Posts: 11,239
Threads: 649
Joined: May 2009
Yeyinde wrote a script to track down $global variables so a scripter can replace 'em. It would make for a cleaner piece of code.
Posts: 112
Threads: 26
Joined: May 2009
Ah yes, I see Wolfie. That is indeed misleading.
What they failed to mention is how much memory is actually used. Totally insignificant except in the most extreme cases which certainly never have happened in RMXP or RMVX unless caused by a bug. (Not including specifically designed tests)
They may have been thinking about how the referenced object, which can be of a significant size, may be present in the memory all the time since the user just used a global variable rather than a local variable.
At this point I think I should remind you that when the scope of a local variable ends the local variable is freed, but the object it references is NOT freed. For freeing objects you have the Garbage Collector (GC) which frees objects you have no reference to (under transitive closure). If the local variable was the only reference to the object then the object will freed whenever the GC gets to it. You can't really know when the GC runs unless you force start it with GC.start. (It is generally preferable to let the GC manage itself)
What now if you have a global variable and want the object it points to freed? Just do something like $global_variable = nil. Actually it can be anything other than the current object.
If there are no more references to the object then the GC will take of it.
The rest of what Tricky wrote is pretty much true. (Note: I think than on a 1-to-1 ratio local variables might use more memory than global variables since they are in a dynamic scope as opposed to a static scope.)
Global variables are NOT resource hogs unless you consider humans as a resource. Then it might be so.
For the CPU global variables are insignificant.
*hugs*
- Zeriab
Edit:
@Charlie: The idea is that global variables don't disappear after introduced so they keep using memory where as local variables are disposed and therefore the number of variables would generally be lower.
For the scope of an RPG Maker XP or VX game I really believe the difference to be insignificant. Maybe it's only for 99 % though.
Posts: 1,128
Threads: 43
Joined: May 2009
Edit:
@Charlie: The idea is that global variables don't disappear after introduced so they keep using memory where as local variables are disposed and therefore the number of variables would generally be lower.
For the scope of an RPG Maker XP or VX game I really believe the difference to be insignificant. Maybe it's only for 99 % though.[/quote]
Wait, I wrote the same thing.
Quote:Only local variables get removed at the end of the method in which they were defined.
I was comparing the use of a brand new global $foo, with the use of $temp.foo, where foo is an accessor of class Game_Temp. Both will not be freed until the program ends, unless you force them to be freed of course.
Posts: 112
Threads: 26
Joined: May 2009
Ah. I misunderstood what you meant. My apologies.
Edit: Wtf... I got an email today about you having replied.
Posts: 1,128
Threads: 43
Joined: May 2009
No problem.
Maybe you have weekly notifications, i guess it's default here.