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.

[SOLVED]adding party hp to a variable?

Code:
# Add above main (or in Game_Party)

class Game_Party

  def sum_hp

    n = 0

    @actors.each {|a| n += a.hp}

    return n

  end

end

 

# Assign to variable

$game_variables[variable_id] = $game_party.sum_hp
 
It is an iterator function. Each is a function of the Array class where it passes through all objects within the array giving them the local variable name defined between the || marks within that block.

So, it passes through each character within your party. Between the { } marks, each actor will be defined as a since that is what we told it (through |a|). From there it is just a matter of adding the hp value to our n counter. Could have also wrote it like:

Code:
@actors.each do |a|

  n += a.hp

end

 

# or

 

for a in @actors

  n += a.hp

end

 

# or

 

for i in [email=0...@actors.size]0...@actors.size[/email]

  a = @actors[i]

  n += a.hp

end
 

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