I've come up with a problem with hash included in arrays.
I would like to analyze a value in a hash that is included in an array.
I'll give an example that illustrates my problem.
Here is a setting:
a = [{:x=>1,:y=>2,:z=>3},{:x=>3,:y=>8,:z=>20}] #randomly chosen values for the exampleWhat I would like to do, is to write a method that can extract the datas in the hashes like this:
a[0][:x] => 1 a[0].unknown_meth => 1
This is what I tried:
def my_meth if self[:x] == 1 print "A" elsif self[:x] == 3 print "B" else print "error" end end a[0].my_meth => nil
I tried this on the online IRB Try ruby.
I used self because I thought it could work, but I still don't get what the self variable represents.
I've read this :
So, in my case, I should have self refering to a[0] but it seems I'm all wrong 'cause it doesn't work.self refers to the object depends on its context. self.title in the above example will be invoked by the (current) object, Book. While title will be invoked by the object, Book.new.
The reason why I'm working on this is that I'm trying to make a multi-act battle system that works both for actors and enemies.
The idea I came up with was to make Game_BattleAction (referred as @active_battler.action in Scene_Battle) an array that contains the sequence of chained actions for each battler (each hash contains the different parameters (@basic, @kind, etc.) of the default Game_BattleAction).
So, two questions:
1. Where am I wrong in my code ? How should I correct it ?
2. Are there "easier" ways to script a multi-act battle system than with arrays of hashes ? Can I still make it ?















