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.

Get String from Array.

Hello Again.

I have a little problem with this code, so. can someone helpme?.

Code:
    clothes = ['Shadow','Combate', 'Guardia', 'Marino', 'Prisionero', 'Campesino']
    for i in 0..clothes.size  
    if @actor.character_name.include?('_' + i)
       self.contents.draw_icon_text(80, 10, 150, 32, clothes[i])
    end   

What I was trying?
I wash trying to check, if the clothes names, that are included in the array. are included in the Character Name.
Example..


If the character name of the actor is:

001-Meph_Shadow.png

The window prints: the actual name, it's say... Shadow but I can't get the name of each index of the array. exist a code for that? like to_i, to_s....

Or in a ruby form.

Code:
if @actor.character_name.include?('shadow')
 self.contents.draw_icon_text(80, 10, 150, 32, 'Shadow')
elsif @actor.character_name.include?('Campesino')
self.contents.draw_icon_text(80, 10, 150, 32, 'Campesino')
elsif
etc...
etc...


Thats all.

Thanks.
 

Zeriab

Sponsor

I'm a not totally sure I understand what you are trying to do.
It sound has if you are simply trying to get the String from the specified index. I.e. clothes[i] in your case.
You are already using this code to draw the icon so I don't think that's your problem.

Can you explain the context? In other words: What are you trying to accomplish with this?
*hugs*
- Zeriab
 
I know that was really ugly description..
I want to make this:

Code:
if @actor.character_name.include?('shadow')
 self.contents.draw_icon_text(80, 10, 150, 32, 'Shadow')
elsif @actor.character_name.include?('Campesino')
self.contents.draw_icon_text(80, 10, 150, 32, 'Campesino')
elsif
etc...
etc...

But in a shorter way like this:

Code:
    clothes = ['Shadow','Combate', 'Guardia', 'Marino', 'Prisionero', 'Campesino']
    for i in 0..clothes.size  
    if @actor.character_name.include?('_' + i)
       self.contents.draw_icon_text(80, 10, 150, 32, clothes[i])
    end 

(In relation to the draw_icon_text, It's a method from the MACL. )
 

Zeriab

Sponsor

Would this work?
Code:
    clothes = ['Shadow','Combate', 'Guardia', 'Marino', 'Prisionero', 'Campesino']
    for i in 0..clothes.size  
    if @actor.character_name.include?(clothes[i])
       self.contents.draw_icon_text(80, 10, 150, 32, clothes[i])
    end 
 
'i' is an integer.  I think you want....

Code:
if @actor.character_name.include?('_' + clothes[i])

[ edit ]
...too slow...  :scruff:
or what Z said, you don't really need to search for the '_'.
 
That's True, And works, but Now, I have this:



Code:
module Status_Symbols
  #--------------------------------------------------------------------------
  # â–º Clothes
  #   Keep the list of clothes that deals with the characters.
  #--------------------------------------------------------------------------
  Clothes = {0 => 'Shadow', 1 => 'Campesino', 2 => 'Guardia', 3 => 'Marino'}
end

    for i in 0..Status_Symbols::Clothes.size  
    if @actor.character_name.include?("#{Status_Symbols::Clothes[i]}")
       self.contents.draw_icon_text(80, 10, 150, 32, Status_Symbols::Clothes[i].to_s)
    else
     self.contents.draw_icon_text(80, 10, 150, 32, 'Casual')
     end  
   end

The problem with this code, is that the text is over another text, so i get this:

http://img262.imageshack.us/img262/3940/asssvj0.png[/img]
::

What is the solution for this?
 

Zeriab

Sponsor

Here's one way of doing it.
Code:
  text_drawn = false
  for i in 0..Status_Symbols::Clothes.size
    if @actor.character_name.include?("#{Status_Symbols::Clothes[i]}")
       self.contents.draw_icon_text(80, 10, 150, 32, Status_Symbols::Clothes[i].to_s)
       text_drawn = true
       break
    end  
  end
  unless text_drawn
    self.contents.draw_icon_text(80, 10, 150, 32, 'Casual')
  end
 
Mmmm...Works fine, but the:

  unless text_drawn
    self.contents.draw_icon_text(80, 10, 150, 32, 'Casual')
  end

When text_drawn is false, doesn't draw the text 'Casual'

What's the problem?
 

Zeriab

Sponsor

Oh, I though it shouldn't draw the 'Casual' when it drew something else.
Here ya go:
Code:
  for i in 0..Status_Symbols::Clothes.size
    if @actor.character_name.include?("#{Status_Symbols::Clothes[i]}")
       self.contents.draw_icon_text(80, 10, 150, 32, Status_Symbols::Clothes[i].to_s)
       break
    end  
  end
  self.contents.draw_icon_text(80, 10, 150, 32, 'Casual')
 

Zeriab

Sponsor

Could you tell me again what the problem was with this version:
Code:
  text_drawn = false
  for i in 0..Status_Symbols::Clothes.size
    if @actor.character_name.include?("#{Status_Symbols::Clothes[i]}")
       self.contents.draw_icon_text(80, 10, 150, 32, Status_Symbols::Clothes[i].to_s)
       text_drawn = true
       break
    end  
  end
  unless text_drawn
    self.contents.draw_icon_text(80, 10, 150, 32, 'Casual')
  end
 
When my character has a graphic that not include any of the arrays indexes, it's supossed to draw a 'Casual', but doesn't draw anything.

Instead of Casual appears a blank.
That's the problem.
 

Zeriab

Sponsor

That is certainly strange since a quick test shows that the logic behind my reasoning works.
The methods are called correctly. The problem most likely lies elsewhere. (Check for copy-paste errors)
Maybe you are calling a draw method with arguments where you don't draw anything.
Try changing 0..Status_Symbols::Clothes.size to 0...Status_Symbols::Clothes.size. That is 3 dots instead of 2 so you don't get the Status_Symbols::Clothes.size along as the last iteration. (You have 4 elements in the provided example.)
Try putting p 'some message' around the loop and find out where the problem is.

*hugs*
- Zeriab
 
Aleluya... I don't really know what was the function or the difference between .. and ..., so the problem was that.

Code:
  text_drawn = false
  for i in 0...Status_Symbols::Clothes.size
    if @actor.character_name.include?("#{Status_Symbols::Clothes[i]}")
       self.contents.draw_icon_text(80, 10, 150, 32, Status_Symbols::Clothes[i].to_s)
       text_drawn = true
       break
    end  
  end
  unless text_drawn
    self.contents.draw_icon_text(80, 10, 150, 32, 'Casual')
  end

I changed the  0..Status_Symbols::Clothes.size for 0...Status_Symbols::Clothes.size and at the instance works perfect.

So thanks for the help. I expect this will not give more problems or bugs.

Thanks thanks. :thumb:
 

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