Enemy Names

Hi there everyone! :D

I'm working in a CBS and i want to show the names of the enemies in a little window
Actually if you are fighting to an slime, other slime, and a zombie for example... the window show this:

Slime
Slime
Zombie

But i want this:

Slime x2
Zombie

I know you can get the name with "enemy.name" but i need to compare that name with all of $game_troop.enemies
Sorry for my poor english

This is the part of the code:
  1.  def refresh
  2.     self.contents.clear
  3.     self.contents.font.size = 24
  4.     y = 60
  5.     for enemy in $game_troop.enemies
  6.       next if enemy.dead?
  7.       self.contents.font.color = Color.new(0,0,0,192)
  8.       self.contents.draw_text(-24, y+2, 192, 18, enemy.name, 2)
  9.       y += 18
  10.     end
  11.   end
  12. end
Expand to see the code.



Thanks :grin:
Try this:
  1. def refresh
  2.     self.contents.clear
  3.     self.contents.font.size = 24
  4.     self.contents.font.color = Color.new(0,0,0,192)
  5.     y = 60
  6.     names = []
  7.     for e in $game_troop.enemies
  8.       names.push e.name
  9.     end
  10.     shown_enemies = []
  11.     for enemy in $game_troop.enemies
  12.       next if enemy.dead?
  13.       if names.count(enemy.name) > 1
  14.         shown_enemies.push(enemy.name + "x"+names.count(enemy.name) if
  15.           !shown_enemies.include?(enemy.name)
  16.       else
  17.         shown_enemies.push(enemy.name)
  18.       end
  19.     end
  20.     for name in shown_enemies
  21.       self.contents.draw_text(-24, y+2, 192, 18, name, 2)
  22.       y += 18
  23.     end
  24.   end
  25. end
Expand to see the code.


Goodluck with the CMS :)
ZenVirZan wrote:
Try this:
  1. def refresh
  2.     self.contents.clear
  3.     self.contents.font.size = 24
  4.     self.contents.font.color = Color.new(0,0,0,192)
  5.     y = 60
  6.     names = []
  7.     for e in $game_troop.enemies
  8.       names.push e.name
  9.     end
  10.     shown_enemies = []
  11.     for enemy in $game_troop.enemies
  12.       next if enemy.dead?
  13.       if names.count(enemy.name) > 1
  14.         shown_enemies.push(enemy.name + "x"+names.count(enemy.name) if
  15.           !shown_enemies.include?(enemy.name)
  16.       else
  17.         shown_enemies.push(enemy.name)
  18.       end
  19.     end
  20.     for name in shown_enemies
  21.       self.contents.draw_text(-24, y+2, 192, 18, name, 2)
  22.       y += 18
  23.     end
  24.   end
  25. end
Expand to see the code.


Goodluck with the CMS :)


Thanks ZenVirZan! ^^

I got an error because a missing ")" in the line 14... so with this:

  1. def refresh
  2.     self.contents.clear
  3.     self.contents.font.size = 24
  4.     self.contents.font.color = Color.new(0,0,0,192)
  5.     y = 60
  6.     names = []
  7.     for e in $game_troop.enemies
  8.       names.push e.name
  9.     end
  10.     shown_enemies = []
  11.     for enemy in $game_troop.enemies
  12.       next if enemy.dead?
  13.       if names.count(enemy.name) > 1
  14.         shown_enemies.push(enemy.name + "x" + names.count(enemy.name)) if
  15.         !shown_enemies.include?(enemy.name)
  16.       else
  17.         shown_enemies.push(enemy.name)
  18.       end
  19.     end
  20.     for name in shown_enemies
  21.       self.contents.draw_text(-24, y+2, 192, 18, name, 2)
  22.       y += 18
  23.     end
  24.   end
  25. end
  26.  
Expand to see the code.


That error not happend anymore, but another comes up "no method for count in..." referring to this "name.count"
Oh, .count mustn't be part of the Ruby 1.8.x RMXP uses.
Try this then :P
  1. def refresh
  2.     self.contents.clear
  3.     self.contents.font.size = 24
  4.     self.contents.font.color = Color.new(0,0,0,192)
  5.     y = 60
  6.     names = {}
  7.     for e in $game_troop.enemies
  8.       if !names.keys.include?(e.name)
  9.         names[e.name] = 1
  10.       else
  11.         names[e.name] += 1
  12.       end
  13.     end
  14.     shown_enemies = []
  15.     for enemy in $game_troop.enemies
  16.       next if enemy.dead?
  17.       if names[enemy.name] > 1
  18.         shown_enemies.push(enemy.name + "x" + names[enemy.name]) if
  19.         !shown_enemies.include?(enemy.name)
  20.       else
  21.         shown_enemies.push(enemy.name)
  22.       end
  23.     end
  24.     for name in shown_enemies
  25.       self.contents.draw_text(-24, y+2, 192, 18, name, 2)
  26.       y += 18
  27.     end
  28.   end
  29. end
  30.  
Expand to see the code.
Almost almost!! :D

I got an error "cannot convert fixnum into string"

so i change the line 18 to this:

  1. shown_enemies.push(enemy.name + "x" + names[enemy.name].to_s)
Expand to see the code.


and works with the ".to_s"... but if you're fighting two slimes and a zombie this is the result:

Slime x2
Slime x2
Zombie

So, the x2 works well but still show the name of the rest of same enemies.


Thanks again ZerVirZan :)
This should be it :)
I'm still not the best at writing stuff without testing it xD
  1. def refresh
  2.     self.contents.clear
  3.     self.contents.font.size = 24
  4.     self.contents.font.color = Color.new(0,0,0,192)
  5.     y = 60
  6.     names = {}
  7.     for e in $game_troop.enemies
  8.       if !names.keys.include?(e.name)
  9.         names[e.name] = 1
  10.       else
  11.         names[e.name] += 1
  12.       end
  13.     end
  14.     shown_enemies = []
  15.     for enemy in $game_troop.enemies
  16.       next if enemy.dead?
  17.       if names[enemy.name] > 1
  18.         shown_enemies.push(enemy.name + "x" + names[enemy.name].to_s) if
  19.         !shown_enemies.include?(enemy.name)
  20.       else
  21.         shown_enemies.push(enemy.name)
  22.       end
  23.     end
  24.     shown_enemies.uniq!
  25.     for name in shown_enemies
  26.       self.contents.draw_text(-24, y+2, 192, 18, name, 2)
  27.       y += 18
  28.     end
  29.   end
  30. end
  31.  
Expand to see the code.
Yes!! it works perfectly, I'll put you in my credits obviosly!! :D

I still have to learn a lot, i can't understand some lines haha
i really apreciate the time you have to put in this

Thanks a lot :)

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=76762