noelburgundy27

Aspiring Jack-of-All-Trades
Veteran
Joined
Oct 12, 2013
Messages
44
Reaction score
8
First Language
English
Primarily Uses
N/A
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?
 

Der Botaniker

Veteran
Veteran
Joined
Mar 18, 2013
Messages
33
Reaction score
13
First Language
French
Primarily Uses
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...
 
Last edited by a moderator:

noelburgundy27

Aspiring Jack-of-All-Trades
Veteran
Joined
Oct 12, 2013
Messages
44
Reaction score
8
First Language
English
Primarily Uses
N/A
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...
Will this extend to other classes, though? Like Game_Actor?
 

Der Botaniker

Veteran
Veteran
Joined
Mar 18, 2013
Messages
33
Reaction score
13
First Language
French
Primarily Uses
Sorry, I don't understand your question :p
 

Wyn Wizard

Arcane Specialist
Veteran
Joined
Feb 23, 2013
Messages
979
Reaction score
80
First Language
English
Primarily Uses
RMVXA
what does the "extend: keyword do exactly?
 

Der Botaniker

Veteran
Veteran
Joined
Mar 18, 2013
Messages
33
Reaction score
13
First Language
French
Primarily Uses
Oh oké. 

In Ruby, modules are two goal: 

1) As a namespace

2) As a mixin.

Mixins are fore "cross overidding". 

If i'm write this : 

module Thing def a return "a" endendThing.a is not accessible. because a isn't public. But I Can graft features to a class : 

class Aha include ThingendNow, each instance of Aha has methods "a" (Aha.new.a). I Can graft to the singleton context 

class Oho extend ThingendWhere Oho.a works. 

A common way for API expostion, with module is to do that : 

module A def self.something return "aha" endendOr module A class << self def something return "aha" end endendIf all of my API must be accessible. I can write this : 

Code:
module Huhu   extend self end
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,548
Reaction score
3,303
First Language
Binary
Primarily Uses
RMMZ
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. :)
 

noelburgundy27

Aspiring Jack-of-All-Trades
Veteran
Joined
Oct 12, 2013
Messages
44
Reaction score
8
First Language
English
Primarily Uses
N/A
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.
 

Wyn Wizard

Arcane Specialist
Veteran
Joined
Feb 23, 2013
Messages
979
Reaction score
80
First Language
English
Primarily Uses
RMVXA
Oh oké. 

In Ruby, modules are two goal: 

1) As a namespace

2) As a mixin.

Mixins are fore "cross overidding". 

If i'm write this : 

module Thing def a return "a" endendThing.a is not accessible. because a isn't public. But I Can graft features to a class : 

class Aha include ThingendNow, each instance of Aha has methods "a" (Aha.new.a). I Can graft to the singleton context 

class Oho extend ThingendWhere Oho.a works. 

A common way for API expostion, with module is to do that : 

module A def self.something return "aha" endendOr module A class << self def something return "aha" end endendIf all of my API must be accessible. I can write this : 

module Huhu extend self end
ok, not sure what im not catching but none the less, you are saying that the "extend" keyword allows classes and methods outside of the module to access its functions and variables right? or am i missing something....?
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,548
Reaction score
3,303
First Language
Binary
Primarily Uses
RMMZ
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.
 

Wyn Wizard

Arcane Specialist
Veteran
Joined
Feb 23, 2013
Messages
979
Reaction score
80
First Language
English
Primarily Uses
RMVXA
oh....well thats nifty as all get out.... :guffaw:
 

Zeriab

Huggins!
Veteran
Joined
Mar 20, 2012
Messages
1,285
Reaction score
1,459
First Language
English
Primarily Uses
RMXP
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*
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,548
Reaction score
3,303
First Language
Binary
Primarily Uses
RMMZ
@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 ^_^
 
Last edited by a moderator:

Latest Threads

Latest Posts

Latest Profile Posts

Made the "My Beating Mystery" board today:
3ENFLSC.png

in Grim Trigger episodes 3 & 4, the main characters play a Clue rip-off board game called "My Beating Mystery" (haha), so of course I had to draw the board for it. Can't decide if I wanna add blood or not
Jeez...
I finally decide, after several ppl asking me to, to create a gamethread about project M... And my internet craps out on me halfway through the process :(
making my first game is like trying to stuff all my experience with old games into one
so its slow and painful when i dont get it right... i bet the next game is gonna be finished earlier!
When you don't find any time to work... :kaocry:
First Ten Minutes of my Caretaker inspired game

Forum statistics

Threads
110,398
Messages
1,052,980
Members
143,458
Latest member
KyN19
Top