Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Two hash problems

I have this problem:

Items_groups[:curativos] = {
:items_ids => [1,7]
}

This is the hash im using. What i need is to acces to the array in a each loop.

Example:

search=''
Wep::Items_groups.each {|i,j| p i ; p j[0] ; p j.include?(item_id) ; search=i if j.include?(item_id)}

I also need to extract the key name as a string because i need it to acces another hash located in game system(to be saved).

I have been trying but without luck.

pd: the second problem is more simple :

I create an array with config hashes.

Blocker_slot_skills = []

Blocker_slot_skills[0] = {
:skill_id => 2,
:slot_num => 4,
:force => 50,
:block_pos => 77,
:rec_pos => 100
}

And iterate the array and i want to acces each hash:

for s in Wep::Blocker_slot_skills
p (s.is_a? (Hash)) , s, s["block_pos"], skill.id

I supose that s is a hash. It says true to is_a, s says the hash, but the s["block_pos"] gives nil.
Why?
 

MicKo

Member

Hey. Don't know if you fixed this, but this might help:

1st problem:
First of all, I'm not sure what you're trying to do with this :items_ids. Unless you want other things in Items_groups[:curativos], just get rid of this :items_ids. Otherwise, maybe this'll help:
[ruby]Wep::Items_groups.each {|i, j|
  p i # => :curativos : keys of Items_groups
  p j # => {:items_ids => [1,7]} : values of Items_groups
  j.each {|k, l|
    p k # => :items_ids : keys of Items_groups[j]
    p l # => [1, 7] : values of Items_groups[j]
  }
}
[/ruby]
But if you don't need this :items_id, you might want something like this:
[ruby]module Wep
  Items_groups = {}
  Items_groups[:curativos] = [1, 7]
end
 
Wep::Items_groups.each {|i, j|
  p i # => :curativos : keys of Items_groups
  p j # => [1, 7] : values of Items_groups
}
[/ruby]
For your other problem, you're calling "block_pos" instead of :block_pos.
[ruby]for s in Wep::Blocker_slot_skills
  p s[:skill_id] # => 2
end
[/ruby]
 

Zeriab

Sponsor

Internally a hash uses object.eql? method for comparing key equality.
With this knowledge you can easily check that "block pos".eql?:)block_pos) returns false which means they are considered to be different keys. nil was returned because it is the default value.
Do not be confused by the syntactic look of symbols. It's not about them having special meaning in this regard, but rather that strings and symbols are not equal.

*hugs*
 
But : is better for the interperter and also you type less, no?
Anyway, is there a way to acces a simbol with a string? Like if i wanted to have a variable with a word and use it to acces a hash that use symbols, its posible?
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top