How do you go about declaring a new global variable for a script to be Above Main and Below Materials?
I'm working on a script and felt like the best way to go about it was using a global variable, but I'm not sure if I should set up like a pre-set value like...
$SAMPLE = 0module Something etc etcendor if I can declare it while in a module like....
module SOMETHING $SAMPLE = $game_actors[1].expendI mean, I'm well aware of how to point to actors and all that, since they're practically variables, but you can't help but worry over simple things if you're deviating, right?
IMHO, you don't have to use global variables. I prefer this solution
module Something extend self SAMPLE = 1 def sample $game_actors[SAMPLE].exp endendNow, you can use "Something.sample"
With your solution, the variable is declared before the declaration of $game_actors...
Sorry, I don't understand your question 😛
what does the "extend: keyword do exactly?
You have to call Something.sample
There is never any real need for a global variable ~ especially if you know how to avoid the use of it. ITs considered as bad programming, and generally fairly lazy.
Sometimes, yes, there may be a reason for creating a $global, but for RGSS programming, its just really not something that is useful. Like, even all of the default vx ace classes ~ the global variables they use are not required, and a little bit pointless. 🙂
Okay, it seems to work.
I'll keep this thread open if I find something because I'm getting stack errors, probably with where I'm pointing things.
module ModuleName extend self def methodname end def anothermethod endendIs identical to this...
module ModuleName def self.methodname end def self.anothermethod endendJust saves you typing the self each time.
oh....well thats nifty as all get out.... 🤣
Doesn't really matter when you do it. Frequently assigning values to global variables before usage is a good idea, but not always required.
Lurking within your questions is the design problem of how to best use your global variable. An answer very likely to be: Do not use a global variable. But, I do not really see how to properly learn the lesson if you do not burn your hands a little.
@Dekita:
How does it work if I at different script section open up the model and add methods?
What about Module inclusion? Also, what role does module_function play?
*hugs*
@Zeriab - Hmm...
I have never used the 'extend self' functionality myself, and subsequently, never experienced any of those situations, so everything below is theoretical...
How does it work if I at different script section open up the model and add methods?
I would hope that once the script that had stated to extend self had been closed (via end), that any scripts that alter the module functionality after this point would be restored to the 'default' module behaviour. However, having stated extend, that 'default functionality' logic could also process differently.
I have to go with, all further cases of that modules code would automatically extend. and thus, all methods declared with the "def methodname" syntax would cause the method to be a module function, rather than a regular method - the kind one may wish to include within a class.
What about Module inclusion?
I assume that any modules that have included the module whose logic 'extends self', would also, have all of its methods (the ones defined after the point of including the module that 'extends self') changed to module functions.
If it where included in a class, the regular instance methods of said class would change to class methods.
Also, what role does module_function play?
Perhaps when the module has been extended to self, it acts as a 'module_function :methodname' definition for each of them, which subsequently, creates both class and instance methods...
def methodnameenddef self.methodnameendGawd I hope I'm right at least a little on the right track... If not, then I hope I'm able to learn something on the journey to being right ^_^