Confused about Modules

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
I have been wondering things about modules. My first insight about modules are that they are in any sense, constant. Therefore, things inside them can not be changed.

Example:

module Spirit GREEN = 100 BLUE = 200 Use = falseendthen whenever I want to access Green, I can do a script call Spirit::GREEN. However, this value can not be changed. My question is, though its much practical to include them in a class for them to be easily changed, say you're working on a hash, 

Spirit_Core = { 1 => 100, 2 => 300, 3 => 400, }and soon after, I want to change spirit core's value #1 to 400, when its under a module, it can't be done. Would this property be able to change?

For example, I made a module and placed the hash inside it, and wanted to make 1=> $game_variables[1], somehow it won't work.

Can someone explain how module values can work as a class value.
 

ShadowLurk

Tanoshii~
Veteran
Joined
Feb 14, 2014
Messages
226
Reaction score
53
Primarily Uses
Module is not constant. The best way to describe module is they are singular (even though you can include modules inside a class), like you cannot do something like foo = Module_Name.new. Like classes, modules can also contain instance variables (the @var things), but they (to my knowledge, anyway) cannot have a class variable (@@var), it's meaningless as modules are singular object.

You cannot touch variables inside a module directly, just like you cannot touch variables inside a class directly. If you want to change a variable in a module, make a method inside module that changes it.

Example:

module SampleModule  @sample_hash = {    1 => 100,    2 => 900,  } def self.change_hash(key, value)    @sample_hash[key] = value  endendIf you call SampleModule.change_hash(1, 500), the hash value will be able to change.

You cannot attr_accessor inside a module, but you can always make a method like the above. Also remember that your Spirit_Core hash is a constant (capital first letter) so Ruby will complain anyway.

By the way, you can do something like this:

module SampleModule class << self attr_accessor :sample_hash end   @sample_hash = {    1 => 100,    2 => 900,  }endWith this, SampleModule.sample_hash will be a valid method.
 
Last edited by a moderator:

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
I suggest you read the different variable types of ruby/RGSS... that problem is not really just for modules

and wanted to make 1=> $game_variables[1], somehow it won't work
Probably because the module was initialized before the game variable is initialized...
 
Last edited by a moderator:

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,042
First Language
French
Primarily Uses
RMMV
Well most of time modules are use for ''put'' your script configuration ....well most of scripter use the modules method in that ways...
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
Actually my Map HUD script uses modules for the most part, from config up to the the actual HUD showing itself.


Basically when you need only a single instance of an object, better use a module than class. that is IMO.
 
Last edited by a moderator:

ShadowLurk

Tanoshii~
Veteran
Joined
Feb 14, 2014
Messages
226
Reaction score
53
Primarily Uses
Actually my Map HUD script uses modules for the most part, from config up to the the actual HUD showing itself.

Basically when you need only a single instance of an object, better use a module than class. that is IMO.
I also think so. That's why most of script configuration is put into a module, because configs (usually) only need one instance.
 

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
So is there a way to integrate switches to the module's hash which does not take an error?

I have been trying for the past few days and I can't figure out a way to set them up perfectly or what you can say is the best route on making them.
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
module Heart


#Save the switch IDs


FIRE = {


"Rawer" = 1,


"Awoo" = 2,


}


def self.fire(key)


return $game_switches[FIRE[key]]


end


end


#now these should work


Heart.fire("Rawer")


Heart.fire("Rawer") = false
 

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
That solved a lot of the problems I am having. At least I can have them on modules now, thanks a lot.
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
though normally, you'd only really use them if you will access those switches INSIDE the module a lot of times... basically, you just saved the ID into the hash so that in case you need to change switches, you only need to edit the ID saved into the hash...
 

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
I'll keep than in mind, thanks for pointing it out to me. thank you.
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
or if it's a config module :)
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,860
Messages
1,017,040
Members
137,569
Latest member
Shtelsky
Top