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.
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.
Well most of time modules are use for ''put'' your script configuration ....well most of scripter use the modules method in that ways...
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.
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.
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
That solved a lot of the problems I am having. At least I can have them on modules now, thanks a lot.
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...
I'll keep than in mind, thanks for pointing it out to me. thank you.
or if it's a config module 🙂