iPhone App Directory
RPG Maker VX
 
Gallery Members Search Calendar Help


Welcome Guest ( Log In | Register )

Collapse

>Announcement

Keep an eye out for the upcoming 3rd Quarter Contest!
 
Reply to this topicStart new topic
> Value in a hash included in an array, a[index][:symbol] = a[index].method
Onkei69
post Mar 17 2012, 09:58 PM
Post #1



Group Icon


Type: Undisclosed
Alignment: Chaotic Neutral




Hi guys,
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:
CODE
a = [{:x=>1,:y=>2,:z=>3},{:x=>3,:y=>8,:z=>20}] #randomly chosen values for the example


What I would like to do, is to write a method that can extract the datas in the hashes like this:
CODE
a[0][:x]
=> 1
a[0].unknown_meth
=> 1


This is what I tried:
CODE
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 :
QUOTE
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.
So, in my case, I should have self refering to a[0] but it seems I'm all wrong 'cause it doesn't work.

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 ?


--------------------
Projects I support:
Spoiler:



My project (dropped):


Spoiler:
Go to the top of the page
 
+Quote Post
   
IceDragon
post Mar 21 2012, 12:23 AM
Post #2


I demand cookies.
Group Icon


Type: Coder
Alignment: Neutral Good




If your gonna end up with multi dimension arrays/hashes its best to use a class.
Managing nested arrays/hashes can become tiresome.
I'm assuming you want to grab all the values in the hash, in x order:
This is a method of my own that I wrote, that allows you to pull multiple values from a hash into an array
CODE
class Hash
  def get_values(*args)
    args.collect {|a|self[a]}
  end
end  
hsh = {
  :bacon => "Yeah!",
  :eggs  => "UhHuh",
  :bread => "Alright",
  :orange=> "OkayThen"
}
hsh.get_values(:bread,:bacon,:eggs) # => ["Alright", "UhHuh", "Yeah!"]


Ruby is easier to understand, once you get down the fact that everything is an object.

self:
CODE
# // Refers to the top level Object
p self # => <main>
class Fridge
  # // Refers to the Fridge class
  p self # => <class Fridge> or something like that
  class Food
    # Refers to the Food class
    p self # => <class Food>
    def when_nommed
      # // Refers to the instance of the Food
      p self # => <Food:0x01fabc2> # // Or something of that nature
    end
  end
end


In some cases like the Numeric class (instances):
CODE
# // self returns its value
class Numeric
  def boo
    self * 20
  end
end
2.boo # 2 * 20 => 40


In order to use the [] (method) within Arrays, and Hashes, you have to use self before its
CODE
class Array
  def sum_all
    result = 0
    for i in 0...size
      result += self[i]
    end
    return result
  end
end
[1,2,3].sum_all() # => 6


If I misunderstood your question, dont be afraid to say so....


--------------------
Spoiler:





To be Hexa-fied


Spoiler:
Other/Reminders--
Support your cookie loving dragon!

CODE
[url="http://www.rpgmakervx.net/index.php?showuser=52960"][img]http://i56.tinypic.com/2d1pfmw.png[/img][/url]


http://www.rpgmakervx.net/index.php?showto...amp;#entry48215 HA AUTOTILES
Pine's Birthday is the 2th of July

WORK ON YOUR TIME MEMORY @___@
Go to the top of the page
 
+Quote Post
   
Nelderson
post Mar 21 2012, 04:53 AM
Post #3



Group Icon


Type: Designer
Alignment: Lawful Good




lol.....Icy helped me out with the same exact issue like 2 days ago laugh.gif



*We are not worthy....OTL

This post has been edited by Nelderson: Mar 21 2012, 04:53 AM


--------------------
STOP BEING A JAPFAG CRIMSON!!!

Meh.....F$#@ you Mike <=== Still applies....<_<
Go to the top of the page
 
+Quote Post
   
Onkei69
post Mar 21 2012, 03:43 PM
Post #4



Group Icon


Type: Undisclosed
Alignment: Chaotic Neutral




Hey, Icy, thanks a lot for answering my post !

I really like how you explain self. It is all neat in my mind now ! (phew, one year after I started Ruby I finally understand it !)

About hashes: I understand that it is not convenient at all. According to what you wrote, I should define my method in class Hash, huh ? I don't know why, I don't like the idea...
I've changed my mind, any way. Since hashes are not convenient in this case, I'm gonna make an array of instances of Game_BattleAction !
Thank you again wink.gif

I guess this topic can be closed now ^^


--------------------
Projects I support:
Spoiler:



My project (dropped):


Spoiler:
Go to the top of the page
 
+Quote Post
   

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 23rd July 2014 - 07:16 PM

RPGMakerVX.net is an Privacy Policy, Legal.