Jump to content


Photo

Value of a hash included in an array

VX hash array

  • Please log in to reply
3 replies to this topic

#1 Onkei69

Onkei69

    Member

  • Members
  • 27 posts
  • LocationFrance
  • Primarily UsesRMVX
  • First LanguageFrench

Posted 18 March 2012 - 01:27 AM

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:
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:
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 :

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):
Posted Image

Spoiler

#2 Kread-EX

Kread-EX

    You're all bakas

  • Members
  • 598 posts
  • Primarily UsesRMVX Ace
  • First LanguageFrench

Posted 18 March 2012 - 03:05 AM

In RGSS self refers in the vast majority of cases to a class instance. In your example, it's impossible to determine what it's referring to because we have no context. If you want it to point toward a specific hash inside the array, as your example seems to imply, then you need to define the method in the Hash class. Like this:
class Hash

  def my_meth
	if self[:x] == 1
	  print "A"
	elsif self[:x] == 3
	  print "B"
	else
	  print "error"
	end
end

a[0].my_meth
Now that's all said and done, I'm not sure this is really useful for what you want but there's also the fact that I don't understand what you're trying to do. What exactly is a "multi-act" battle system? Why exactly using an array of hashes?

#3 Onkei69

Onkei69

    Member

  • Members
  • 27 posts
  • LocationFrance
  • Primarily UsesRMVX
  • First LanguageFrench

Posted 18 March 2012 - 05:20 AM

Oh, thank you Kread-EX, I've finally understood how self works (phew, I spent a year without understanding it... It looks so simple now :P)

Here is a little description of my "Multi-act" battle system (in facts, I should call it something like Chain-action BS or something like that):
- it is turn based, like default battle system
- each battler turn consist in a sequence of actions (1-3) in stead of one, and each action uses action points (AP) and mana (for skills) but are all directed on the same single target unless action target is a whole group (which breaks the chain), with chain bonuses applied. Which means you can do: [attack mob1, attack mob1, skill all mobs]; or [attack mob1, skill all mobs] (end of the chain); [skill all mobs] (no chain); but you can't do [attack mob1, attack mob 2, attack mob 1] or [skill mob 1, skill all mobs, skill mob 1]. Of course, the action sequence can be inferior to the max length to save AP.

The reason why I thought of using an array of hashes is that I saw that an action was defined by variables (speed, kind, basic...). I thought of turning Game_BattleAction into an array that contained hashes of values to define an action. But it looked like I would have had to change plenty of code that way...

So, I've come to understand that it would be much simpler (and less code-altering) to do this:
- make an array for the action list
- concatenate @active_battler.action in the array since @active_battler.action is an object of Game_BattleAction (for some reasons, I didn't know you could put class objects in a array) and defines the action
- repeat until array.size = a max chain value (unless chain break)
- execute the action sequence by reading each items of the array.

Again, thank you for helping me realize these things =)
Projects I support:
Spoiler


My project (dropped):
Posted Image

Spoiler

#4 Kread-EX

Kread-EX

    You're all bakas

  • Members
  • 598 posts
  • Primarily UsesRMVX Ace
  • First LanguageFrench

Posted 18 March 2012 - 06:08 AM

Yes it would be much more useful to simply transform Game_Battler's @action into an array of Game_BattleAction instances. You can can also use Array#shift to retrieve the first object in the array while removing it at the same time.
For instance:
array = ['a', 'b', 'c']
value = array.shift

p value # outputs 'a'
p array # outputs ['b', 'c']
And use the array size (array.empty? or array.size > 0) to determine if there's no more actions in queue.





Also tagged with one or more of these keywords: VX, hash, array

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users