When should I use global, instance, local, and constants in event systems? - Printable Version +- Save-Point (https://www.save-point.org) +-- Forum: Games Development (https://www.save-point.org/forum-4.html) +--- Forum: Development Discussion (https://www.save-point.org/forum-17.html) +--- Thread: When should I use global, instance, local, and constants in event systems? (/thread-2511.html) Pages:
1
2
|
When should I use global, instance, local, and constants in event systems? - Charlie Fleed - 12-28-2008 Quote:Originally Posted by Trickster at HBGames.Org 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: When should I use global, instance, local, and constants in event systems? - PK8 - 12-28-2008 Wow... this has turned into a discussion thread. O_O' (moves) When should I use global, instance, local, and constants in event systems? - MARBLES - 12-28-2008 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. When should I use global, instance, local, and constants in event systems? - DerVVulfman - 12-28-2008 Yeyinde wrote a script to track down $global variables so a scripter can replace 'em. It would make for a cleaner piece of code. When should I use global, instance, local, and constants in event systems? - Zeriab - 12-28-2008 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. When should I use global, instance, local, and constants in event systems? - Charlie Fleed - 12-28-2008 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. When should I use global, instance, local, and constants in event systems? - Zeriab - 12-28-2008 Ah. I misunderstood what you meant. My apologies. Edit: Wtf... I got an email today about you having replied. When should I use global, instance, local, and constants in event systems? - Charlie Fleed - 12-28-2008 No problem. Maybe you have weekly notifications, i guess it's default here. |