When should I use global, instance, local, and constants in event systems?
#11
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:
Reply }
#12
Wow... this has turned into a discussion thread. O_O' (moves)
Reply }
#13
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.
Reply }
#14
Yeyinde wrote a script to track down $global variables so a scripter can replace 'em. It would make for a cleaner piece of code.
Reply }
#15
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.
Reply }
#16
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.
Reply }
#17
Ah. I misunderstood what you meant. My apologies.

Edit: Wtf... I got an email today about you having replied.
Reply }
#18
No problem.
Maybe you have weekly notifications, i guess it's default here.
Reply }


Possibly Related Threads…
Thread Author Replies Views Last Post
   Popular Battle Systems kyonides 6 7,590 02-24-2022, 10:40 PM
Last Post: kyonides
Brick Common Event Library (Development + Brainstorm thread) PK8 3 9,746 02-11-2016, 09:25 PM
Last Post: JayRay
   What do you dislike in ATB Systems? DoubleX 18 25,185 06-13-2015, 05:03 AM
Last Post: DoubleX
   What Makes an Event System DerVVulfman 8 14,303 01-19-2010, 09:07 PM
Last Post: Bolt
   What is "event scripting?" PK8 11 18,131 12-19-2008, 11:20 PM
Last Post: PK8



Users browsing this thread: 1 Guest(s)