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.

script help

I have this script that make a hud to display hp and sp but I need it to display it for all 4 characters

Code:
class Window_YourHUD < Window_Base
  def initialize
    super(0, 0, 155 , 85)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.contents.font.size = 14
    self.opacity = 0
    refresh
  end
  def refresh
    self.contents.clear
    reset_variables
    return if !@actor
    draw_actor_name(@actor, 0, 0)
    draw_actor_hp(@actor, 0, 15)
    draw_actor_sp(@actor, 0, 30)
  end
  def reset_variables
   @actor = $game_party.actors[0]
   @old_hp = @actor ? @actor.hp : 0
   @old_maxhp = @actor ? @actor.maxhp : 0
   @old_sp = @actor ? @actor.sp : 0
   @old_maxsp = @actor ? @actor.maxsp : 0
  end
  def update
    super
    refresh if (@actor = $game_party.actors[0] or
                @old_hp = @actor ? @actor.hp : 0 or
                @old_maxhp = @actor ? @actor.maxhp : 0 or
                @old_sp = @actor ? @actor.sp : 0 or
                @old_maxsp = @actor ? @actor.maxsp : 0)
  end
end
class Scene_Map
  alias yourhud_main main
  def main
    @yourhud = Window_YourHUD.new
    yourhud_main
    @yourhud.dispose
  end
end
 

khmp

Sponsor

This sounds very much like a request lukecovack? :huh:

My recommendation for you solving this yourself is finding a way to adjust what actor is being looked at in the party. Right now that HUD is only checking and drawing the information of $game_party.actors[0]. Which is the party leader the second party member would be $game_party.actors[1] and so on. If you are attempting to draw all this information in one window you'll need to make use of some sort of loop for drawing.

Code:
x = 0
# Iterate through the actors in the party.
for actor in $game_party.actors
  draw_actor_name(actor, x, 0)
  draw_actor_hp(actor, x, 15)
  draw_actor_sp(actor, x, 30)
  # Move to the right.
  x += n
end

However checking each actor's attributes for a single change during update and drawing the whole thing over again is bothersome and inefficient. You can do it it's possible and you could probably stream line to be efficient but that would take too long to explain. So here's what I suggest for the sake of ease. Let's alter your initialize for Window_YourHUD. Let's make it take in a parameter.
Code:
  def initialize(actor_id = 0)
    ... other code
    # Save the actor id.
    @actor_id = actor_id
    refresh
  end

Then our reset_variables would also change on the plus side there's no need to alter the refresh method. So in the reset_variables method we need to change that 0 to @actor_id in place of gathering the actor.
Code:
@actor = $game_party.actors[0]
becomes:
Code:
@actor = $game_party.actors[@actor_id]

Then in your update which I remember you asking about before you still have the mistake of comparison and equality. Ok so here it goes again:
Code:
a = 1
b = 2
if a = b 
  p 'true'
end

Copy that code and place it directly above "begin" in Main(scripts) in any project. Run it. You will see that it prints true even though it clearly is not true. 1 does not equal 2. So what is happening on that "if" line. a is being set equal to b which is 2. Inside the if block if you print a. You will see it is 2. So a and b are both now 2. The computer says alright assignment is true. Execute whatever code is within the if block. This same situation is occurring in the code you have in your update method:
Code:
refresh if @actor = $game_party.actors[0] # This will always be true

Two equal signs mean a check for equality while a single equals means assignment. Putting an assignment is always equated to true.

What I suspect you want to be doing instead is check to see if they are not equal. If they are not equal than yes it's time to redraw the information. And that code will look like this:
Code:
refresh if @actor != $game_party.actors[0]

But remember we need to use the id of the actor we want to look at instead of just the party leader.
Code:
    refresh if (@actor = $game_party.actors[@actor_id] or
                @old_hp != @actor ? @actor.hp : 0 or
                @old_maxhp != @actor ? @actor.maxhp : 0 or
                @old_sp != @actor ? @actor.sp : 0 or
                @old_maxsp != @actor ? @actor.maxsp : 0)

Alright now that we have that done. We just need to change the way the HUD is created. So in Scene_Map we are going to create an array of HUD's. One to represent each actor in the party. Inside the main method replace:
Code:
@yourhud = Window_YourHUD.new
with:
Code:
@huds = []
$game_party.actors.each_index do |i| 
  @huds << Window_YourHUD.new(i)
  @huds[i].x = i * @huds[i].width

And don't forget to dispose of all the windows. So replace:
Code:
@yourhud.dispose
with:
Code:
@huds.each { |hud| hud.dispose }

And inside the Scene_Map class include:
Code:
  alias yourhud_update update
  def update
    @huds.each { |hud| hud.update }
    yourhud_update
  end

Good luck with it lukecovack! :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