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.

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:
Code:
  def refresh

    self.contents.clear

    self.contents.font.size = 24

    y = 60

    for enemy in $game_troop.enemies

      next if enemy.dead?

      self.contents.font.color = Color.new(0,0,0,192)

      self.contents.draw_text(-24, y+2, 192, 18, enemy.name, 2)

      y += 18

    end

  end

end


Thanks :grin:
 
Try this:
Code:
def refresh

    self.contents.clear

    self.contents.font.size = 24

    self.contents.font.color = Color.new(0,0,0,192)

    y = 60

    names = []

    for e in $game_troop.enemies

      names.push e.name

    end

    shown_enemies = []

    for enemy in $game_troop.enemies

      next if enemy.dead?

      if names.count(enemy.name) > 1

        shown_enemies.push(enemy.name + "x"+names.count(enemy.name) if 

          !shown_enemies.include?(enemy.name)

      else

        shown_enemies.push(enemy.name)

      end

    end

    for name in shown_enemies

      self.contents.draw_text(-24, y+2, 192, 18, name, 2)

      y += 18

    end

  end

end

Goodluck with the CMS :)
 
ZenVirZan":31f23gwe said:
Try this:
Code:
def refresh

    self.contents.clear

    self.contents.font.size = 24

    self.contents.font.color = Color.new(0,0,0,192)

    y = 60

    names = []

    for e in $game_troop.enemies

      names.push e.name

    end

    shown_enemies = []

    for enemy in $game_troop.enemies

      next if enemy.dead?

      if names.count(enemy.name) > 1

        shown_enemies.push(enemy.name + "x"+names.count(enemy.name) if 

          !shown_enemies.include?(enemy.name)

      else

        shown_enemies.push(enemy.name)

      end

    end

    for name in shown_enemies

      self.contents.draw_text(-24, y+2, 192, 18, name, 2)

      y += 18

    end

  end

end

Goodluck with the CMS :)

Thanks ZenVirZan! ^^

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

Code:
def refresh

    self.contents.clear

    self.contents.font.size = 24

    self.contents.font.color = Color.new(0,0,0,192)

    y = 60

    names = []

    for e in $game_troop.enemies

      names.push e.name

    end

    shown_enemies = []

    for enemy in $game_troop.enemies

      next if enemy.dead?

      if names.count(enemy.name) > 1

        shown_enemies.push(enemy.name + "x" + names.count(enemy.name)) if 

        !shown_enemies.include?(enemy.name)

      else

        shown_enemies.push(enemy.name)

      end

    end

    for name in shown_enemies

      self.contents.draw_text(-24, y+2, 192, 18, name, 2)

      y += 18

    end

  end

end

 

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
Code:
def refresh

    self.contents.clear

    self.contents.font.size = 24

    self.contents.font.color = Color.new(0,0,0,192)

    y = 60

    names = {}

    for e in $game_troop.enemies

      if !names.keys.include?(e.name)

        names[e.name] = 1

      else

        names[e.name] += 1

      end

    end

    shown_enemies = []

    for enemy in $game_troop.enemies

      next if enemy.dead?

      if names[enemy.name] > 1

        shown_enemies.push(enemy.name + "x" + names[enemy.name]) if

        !shown_enemies.include?(enemy.name)

      else

        shown_enemies.push(enemy.name)

      end

    end

    for name in shown_enemies

      self.contents.draw_text(-24, y+2, 192, 18, name, 2)

      y += 18

    end

  end

end

 
 
Almost almost!! :D

I got an error "cannot convert fixnum into string"

so i change the line 18 to this:

Code:
shown_enemies.push(enemy.name + "x" + names[enemy.name].to_s)

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
Code:
def refresh

    self.contents.clear

    self.contents.font.size = 24

    self.contents.font.color = Color.new(0,0,0,192)

    y = 60

    names = {}

    for e in $game_troop.enemies

      if !names.keys.include?(e.name)

        names[e.name] = 1

      else

        names[e.name] += 1

      end

    end

    shown_enemies = []

    for enemy in $game_troop.enemies

      next if enemy.dead?

      if names[enemy.name] > 1

        shown_enemies.push(enemy.name + "x" + names[enemy.name].to_s) if

        !shown_enemies.include?(enemy.name)

      else

        shown_enemies.push(enemy.name)

      end

    end

    shown_enemies.uniq!

    for name in shown_enemies

      self.contents.draw_text(-24, y+2, 192, 18, name, 2)

      y += 18

    end

  end

end

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

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