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.

Hunger Hud Script Help <resolved thanks to KHMP & Gando>

Hey I'm trying to get this script to work for me but it keeps throwing up errors. I don't know anything bout scripting but I tried modifing two scripts I found. The end product is this,
Code:
#==============================================================================
# Hunger_Hud
#------------------------------------------------------------------------------
# By: L0rdPh0enix
# 5/4/2008
#------------------------------------------------------------------------------
#==============================================================================
class Hunger_Hud < Window_Base
  HUD_SWITCH = 2
  Variablehunger = 22

  #--------------------------------------------------------------------------
  # * Initialize             
  #--------------------------------------------------------------------------
  def initialize
    super(0, 0, 200, 70)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.opacity = 150
    self.visible = false
    refresh
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    reset_variables
    self.contents.draw_text(0, 0, 100, 32, "Hunger:", 0)
  if $game_switches[2] = true
    self.contents.draw_text(120, 0, 32, 32, @variablehunger.to_s, 0)
  end
  end
  def reset_variables
   @variableone = $game_variables[VariableOne]
  end
#==============================================================================
# ** Scene_Map
#==============================================================================
class Scene_Map

  alias hungerhud_main main
  alias hungerhud_update update
  def main
    @hungerhud = Hunger_Hud.new
    hungerhud_main
    @hungerhud.dispose
  end
  end
  def update
    @hud.visible = $game_switches[HUD_SWITCH]    
    end
    @hungerhud.update
    hungerhud_update
  end
end

As you can sorta see, I'm trying to get it to simply show a variable in a window that's controled by a switch [2]. Where if the switch is on the window shows and if the switch is off the window doesn't show. Instead of a number being shown thou i'd like it to show a pic in the window from the Pictures folder. The variable is controlled by a common event so that it can't be over 100 and if it is 0 you start to loose hp's. I know I can get the displaying of pics in a common event but it lags a bit and the event starts all over with each map change. i.e. Health is at 50% and player moves to a different map so hp becomes 100% again for the new map. Besides I'm trying to build a User Interface bit by bit with scripts so that it's not just a *png file. Thanks in advance for the help.

EDIT: Oh and once I've got it working, I'll gladly post it in the script resources as a Hunger System/Life System with instructions on how to set it up.
 
hmm.. So what youre saying is that you want to show a new picture everytime the value of the variable changes?
like this:

variable's value = 1 (show's picture 1)...
variable's value = 2 (show's picture 2)...
variable's value = 3 (show's picture 3)...
variable's value = 4 (show's picture 4)...
...etc.

Am i right, or am i just really confused? ^^

Over and out - Gando
 
Yeah that's what I'd like to do. I have 11 pics from 100% to 0% that i'll be using. But I want them show in a "hud" which I tell to show in a certain part of the screen when switch [2] is on, and goes away when the switch isn't on. It doesn't have to update every frame, just every 20 frames or so.
 
Okay, i think this will work, although i haven't tried it.
Code:
#==============================================================================
# Hunger_Hud
#------------------------------------------------------------------------------
# By: L0rdPh0enix
# 5/4/2008
#------------------------------------------------------------------------------
#==============================================================================
  HUD_SWITCH = 2
  Variablehunger = 22
  
class Hunger_Hud < Window_Base
  #--------------------------------------------------------------------------
  # * Initialize             
  #--------------------------------------------------------------------------
  def initialize
    super(0, 0, 200, 270)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.opacity = 150
    self.visible = false
    refresh
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    reset_variables
    self.contents.draw_text(0, 0, 100, 32, "Hunger:", 0)
    if @variablehunger <= 100 && @variablehunger >= 91
      bitmap = RPG::Cache.picture('Picture10')
      self.contents.blt(120, 3, bitmap, Rect.new(0,0,24,54))
    else if @variablehunger <= 90 && @variablehunger >= 82
      bitmap = RPG::Cache.picture('Picture9')
      self.contents.blt(120, 3, bitmap, Rect.new(0,0,24,54))
    else if @variablehunger <= 81  && @variablehunger >= 73
      bitmap = RPG::Cache.picture('Picture8')
      self.contents.blt(120, 3, bitmap, Rect.new(0,0,24,54))
    else if @variablehunger <= 72 && @variablehunger >= 64
      bitmap = RPG::Cache.picture('Picture7')
      self.contents.blt(120, 3, bitmap, Rect.new(0,0,24,54))
    else if @variablehunger <= 63 && @variablehunger >= 55
      bitmap = RPG::Cache.picture('Picture6')
      self.contents.blt(120, 3, bitmap, Rect.new(0,0,24,54))
    else if @variablehunger <= 54 && @variablehunger >= 46
      bitmap = RPG::Cache.picture('Picture5')
      self.contents.blt(120, 3, bitmap, Rect.new(0,0,24,54))
    else if @variablehunger <= 45 && @variablehunger >= 37
      bitmap = RPG::Cache.picture('Picture4')
      self.contents.blt(120, 3, bitmap, Rect.new(0,0,24,54))
    else if @variablehunger <= 36 && @variablehunger >= 28
      bitmap = RPG::Cache.picture('Picture3')
      self.contents.blt(120, 3, bitmap, Rect.new(0,0,24,54))
    else if @variablehunger <= 27&& @variablehunger >= 19
      bitmap = RPG::Cache.picture('Picture2')
      self.contents.blt(120, 3, bitmap, Rect.new(0,0,24,54))
    else if @variablehunger <= 18 && @variablehunger >= 10
      bitmap = RPG::Cache.picture('Picture1')
      self.contents.blt(120, 3, bitmap, Rect.new(0,0,24,54))
    else if @variablehunger <= 9
      bitmap = RPG::Cache.picture('Picture0')
      self.contents.blt(120, 3, bitmap, Rect.new(0,0,24,54))
    end
    end
    end
    end
    end
    end
    end
    end
    end
    end
  end
end

  def reset_variables
   @variablehunger = $game_variables[Variablehunger]
 end
  #--------------------------------------------------------------------------
  # * Update
  #--------------------------------------------------------------------------
  def update
    super
    refresh if @variablehunger = $game_variables[Variablehunger]
  end 
end
#==============================================================================
# ** Scene_Map
#==============================================================================
class Scene_Map

  alias hungerhud_main main
  alias hungerhud_update update
  def main
    @hungerhud = Hunger_Hud.new
    hungerhud_main
    @hungerhud.dispose
  end
  def update
    if $game_switches[HUD_SWITCH]
      @hungerhud.visible = true
    else
      @hungerhud.visible = false
    end
    @hungerhud.update
    hungerhud_update
  end
end

Replace 'Picture1', 'Picture2', 'Picture3'.. etc in:
Code:
bitmap = RPG::Cache.picture('Picture1'),
bitmap = RPG::Cache.picture('Picture2'),
bitmap = RPG::Cache.picture('Picture3').. etc

With the names of your pictures in the pictures folder.
Then you should look at these lines:

Code:
    if @variablehunger <= 100 && @variablehunger >= 91
      bitmap = RPG::Cache.picture('Picture10')
      self.contents.blt(120, 3, bitmap, Rect.new(0,0,24,54))
    else if @variablehunger <= 90 && @variablehunger >= 82
      bitmap = RPG::Cache.picture('Picture9')
      self.contents.blt(120, 3, bitmap, Rect.new(0,0,24,54)) ...etc.

The " if @variablehunger <= 100 && @variablehunger >= 91"  checks if your variables value is between 100-91, if it's true, it will show picture10. 
The next line "else if @variablehunger <= 90 && @variablehunger >= 82" checks if your variables value is between 90-82, if it's true, it will show picture 9.
You can change these numbers yourself so that it suits you.

If you have any problems with it, tell me and i'll help you.
Good luck! :thumb:

Over and out - Gando
 

khmp

Sponsor

Gando if I might suggest a case statement that uses ranges instead?

Code:
filename = nil
case @variablehunger
when 82..90
  filename = 'Picture9'
when 91..100
  filename = 'Picture10'
else
  filename = 'PictureElse'
end

bitmap = Bitmap.new('Graphics/Pictures/' + filename)
self.contents.blt(120, 3, bitmap, Rect.new(0, 0, 24, 54)

What you might want to switch too instead is percentage drawing. This eliminates the case statement and the if statement completely. So inside your initialize you would have the bitmap loaded just once.

Code:
  Max_Hunger = 100
  Hunger_Bar = 'Picture10'
  #--------------------------------------------------------------------------
  # * Initialize             
  #--------------------------------------------------------------------------
  def initialize
    super(0, 0, 200, 270)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.opacity = 150
    self.visible = false
    @hunger_bar = Bitmap.new('Graphics/Pictures/' + Hunger_Bar)
    refresh
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    reset_variables
    self.contents.draw_text(0, 0, 100, 32, "Hunger:", 0)

    # How full are we?
    percent = [@variablehunger / Max_Hunger.to_f, 1.0].min

    bar_width = (@hunger_bar.width * percent).to_i

    # First clear the bar's position
    self.contents.fill_rect(120, 3, @hunger_bar.width, @hunger_bar.height, 
      Color.new(0,0,0,0))
    # Now draw the bar.
    self.contents.blt(120, 3, @hunger_bar, Rect.new(0, 0, bar_width, 54))
  end
  #--------------------------------------------------------------------------
  # * Dispose
  #--------------------------------------------------------------------------
  def dispose
    super()
    @hunger_bar.dispose unless @hunger_bar.disposed?
  end
 
You know a fresh nights sleep does wonders for the mind. I don't have to have the hunger bar done in pic files. It would be a lot easier (i think) if it was just done with gradual bars like people make for their CMS and CBS for the health and mana. But no the help you two are giving is good still, I can prolly adapt the scripts into one for drawing a hud for my compass system (common event based right now) if one of you could tell me how to make the script show a pic of 4 depending on which direction the player is facing.

Edit: Yep still waking up, I admit it I'm an idiot. Just really read through and understood what KHMP just said and that's easier then what I posted earlier here so let me plug that in. Oh and I've got a working credits scripts so both of you have been credited.

Edit: WOO HOO, thanks to both of you and the help you've done, my entire User Interface is now huded.
It went from this
http://i28.tinypic.com/qwx0g5.png[/img]

To this
http://i31.tinypic.com/2iqlcms.png[/img]
The "wood" work that was there in the first picture is now done entirely with scripts. This is very cool since I use the Advanced Configuration by The Sleeping Leonhart script so now when ever the player changes the games "skin" it does it for the User Interface too  :grin:. Now I can close my other script request.
 
khmp":3o6e8aol said:
Gando if I might suggest a case statement that uses ranges instead?

Code:
filename = nil
case @variablehunger
when 82..90
  filename = 'Picture9'
when 91..100
  filename = 'Picture10'
else
  filename = 'PictureElse'
end

bitmap = Bitmap.new('Graphics/Pictures/' + filename)
self.contents.blt(120, 3, bitmap, Rect.new(0, 0, 24, 54)

What you might want to switch too instead is percentage drawing. This eliminates the case statement and the if statement completely. So inside your initialize you would have the bitmap loaded just once.

Code:
  Max_Hunger = 100
  Hunger_Bar = 'Picture10'
  #--------------------------------------------------------------------------
  # * Initialize             
  #--------------------------------------------------------------------------
  def initialize
    super(0, 0, 200, 270)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.opacity = 150
    self.visible = false
    @hunger_bar = Bitmap.new('Graphics/Pictures/' + Hunger_Bar)
    refresh
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    reset_variables
    self.contents.draw_text(0, 0, 100, 32, "Hunger:", 0)

    # How full are we?
    percent = [@variablehunger / Max_Hunger.to_f, 1.0].min

    bar_width = (@hunger_bar.width * percent).to_i

    # First clear the bar's position
    self.contents.fill_rect(120, 3, @hunger_bar.width, @hunger_bar.height, 
      Color.new(0,0,0,0))
    # Now draw the bar.
    self.contents.blt(120, 3, @hunger_bar, Rect.new(0, 0, bar_width, 54))
  end
  #--------------------------------------------------------------------------
  # * Dispose
  #--------------------------------------------------------------------------
  def dispose
    super()
    @hunger_bar.dispose unless @hunger_bar.disposed?
  end
Ok I've "plugged" it in but the Heath bar is a no show.

Here's what i've done to it.
Code:
#==============================================================================
# Hunger_Hud
#------------------------------------------------------------------------------
# By: L0rdPh0enix
# Helped by: Khmp, Gando  
# 5/4/2008
#------------------------------------------------------------------------------
#==============================================================================
  HUD_SWITCH = 2
  Variablehunger = 22
  
class Hunger_Hud < Window_Base
  Max_Hunger = 100
  Hunger_Bar = 'Hunger_100'
  #--------------------------------------------------------------------------
  # * Initialize             
  #--------------------------------------------------------------------------
  def initialize
    super(151, 0, 96, 61)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.opacity = 200
    self.visible = false
    @hunger_bar = Bitmap.new('Graphics/Pictures/' + Hunger_Bar)
    refresh
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    reset_variables
        # How full are we?
    percent = [@variablehunger / Max_Hunger.to_f, 1.0].min

    bar_width = (@hunger_bar.width * percent).to_i

    # First clear the bar's position
    self.contents.fill_rect(120, 3, @hunger_bar.width, @hunger_bar.height, 
      Color.new(0,0,0,0))
    # Now draw the bar.
    self.contents.blt(120, 3, @hunger_bar, Rect.new(0, 0, bar_width, 54))
  end
  #--------------------------------------------------------------------------
  # * Dispose
  #--------------------------------------------------------------------------
  def dispose
    super()
    @hunger_bar.dispose unless @hunger_bar.disposed?
  end

  def reset_variables
   @variablehunger = $game_variables[Variablehunger]
 end
  #--------------------------------------------------------------------------
  # * Update
  #--------------------------------------------------------------------------
  def update
    super
    refresh if @variablehunger = $game_variables[Variablehunger]
  end 
end
#==============================================================================
# ** Scene_Map
#==============================================================================
class Scene_Map

  alias hungerhud_main main
  alias hungerhud_update update
  def main
    @hungerhud = Hunger_Hud.new
    hungerhud_main
    @hungerhud.dispose
  end
  def update
    if $game_switches[HUD_SWITCH]
      @hungerhud.visible = true
    else
      @hungerhud.visible = false
    end
    @hungerhud.update
    hungerhud_update
  end
end
 

khmp

Sponsor

I just noticed your health bar is actually taller than it is wide. Is this bar vertical because that change the code a little. I'm not sure why it wouldn't draw though. Maybe percent always comes out to zero. But I don't think that would be the case.

Change your update code from:
Code:
refresh if @variablehunger = $game_variables[Variablehunger]
To:
Code:
refresh if @variablehunger != $game_variables[Variablehunger]

Remember assignment statements are always true so it was refreshing on every update call. The double equals is for comparing two or more objects.

You know what this is going to be in the demo right? I'll look into it then. Get them all done in one fell swoop.
 
Lol yeah it'll be in the demo which will be done uploading in 10min. Then you can see all the big messes i've made :P. Lol and I know my Credits scripts isn't up to date my computer crashed in the middle of loading the test game so I had to restore all my *.rxdata files from an earlier version when the Credits scripts wasn't in it.
 
Lol yay the sites back. anyway Thanks for the help Khmp, but some reason the Health bar no worky ? it just stays Full, and the time hud still makes it a bit jerky. Bet you didn't bargain for all this when you signed on to help eh :P
 
@khmp
Aaah, stupid, stupid me!
So obvious, and still i had to complicate it all by making a bunch of if statements..
Well, let's just blame it on temporary insanity, or.. maybe that i was high? :wink:


@l0rdph0enix
Sorry, but i'm having a hard time to keeping up with this topic,
could you post the script you have so far? And i'll try to help you. (i'll do better this time khmp, i promise  :tongue:)
 

khmp

Sponsor

l0rdph0enix":3whgxqfs said:
Lol yay the sites back. anyway Thanks for the help Khmp, but some reason the Health bar no worky ? it just stays Full, and the time hud still makes it a bit jerky. Bet you didn't bargain for all this when you signed on to help eh :P

Do you mean Health or Hunger? The code I PM'ed you was not jerky on my machine I made sure that all bitmaps were loaded at initialization and blt'ed only when absolutely necessary. Are you turning the Hunger switch on?

@Gando I'm all for your thoughts:
Code:
#==============================================================================
# Hunger_Hud
#------------------------------------------------------------------------------
# By: L0rdPh0enix
# Helped by: Khmp, Gando  
# 5/4/2008
#------------------------------------------------------------------------------
#==============================================================================
  HUD_SWITCH = 2
  Variablehunger = 22
  
class Hunger_Hud < Window_Base
  Max_Hunger = 100
  Hunger_Bar = 'Hunger_100'
  Hunger_Empty = 'Hunger_0'
  #--------------------------------------------------------------------------
  # * Initialize             
  #--------------------------------------------------------------------------
  def initialize
    super(151, 0, 96, 61)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.opacity = 200
    self.visible = false
    @hunger_bar = Bitmap.new('Graphics/Pictures/' + Hunger_Bar)
    @empty_bar = Bitmap.new('Graphics/Pictures/' + Hunger_Empty)
    refresh
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    reset_variables
        # How full are we?
    percent = [@variablehunger / Max_Hunger.to_f, 1.0].min

    bar_width = (@hunger_bar.width * percent).to_i

    # First clear the bar's position. Draw the old bar first
    self.contents.blt(0, 3, @empty_bar, 
      Rect.new(0, 0, @empty_bar.width, @empty_bar.height))
    # Now draw the bar.
    self.contents.blt(0, 3, @hunger_bar, 
      Rect.new(0, 0, bar_width, @hunger_bar.height))
  end
  #--------------------------------------------------------------------------
  # * Dispose
  #--------------------------------------------------------------------------
  def dispose
    super()
    @hunger_bar.dispose unless @hunger_bar.disposed?
    @empty_bar.dispose unless @empty_bar.disposed?
  end
  #--------------------------------------------------------------------------
  # * Reset
  #--------------------------------------------------------------------------
  def reset_variables
    @variablehunger = $game_variables[Variablehunger]
  end
  #--------------------------------------------------------------------------
  # * Update
  #--------------------------------------------------------------------------
  def update
    super
    refresh if @variablehunger != $game_variables[Variablehunger]
  end 
end
#==============================================================================
# ** Scene_Map
#==============================================================================
class Scene_Map

  alias hungerhud_main main
  alias hungerhud_update update
  def main
    @hungerhud = Hunger_Hud.new
    hungerhud_main
    @hungerhud.dispose
  end
  def update
    if $game_switches[HUD_SWITCH]
      @hungerhud.visible = true
    else
      @hungerhud.visible = false
    end
    @hungerhud.update
    hungerhud_update
  end
end
 
Lol K if it's smooth on your's i'll take your word on it, Just means my own game now has higher system requirements then my computer has :P lol that's sad. As soon as I get the funds i'll upgrade my computer so hopefully the smoothness of the game returns.

and for the hunger system, the switch is on, it's weird, the only way to get the bars to show and work is if i have their common event running (sorry the hunger system was a mess don't know if you noticed but it had two common events)
and  I just tested my game again and it's alot smoother, just can't have much running in the background :P
 
@khmp
Well, i can't find anything wrong with the script you posted, except that the bars are being drawed outside the window.
The window's width is 96, and the x-position where the bars are being drawed are 120, but that's really not a big deal.
Other than that it's rock solid.

@l0rdph0enix
Lol yay the sites back. anyway Thanks for the help Khmp, but some reason the Health bar no worky ? it just stays Full, and the time hud still makes it a bit jerky. Bet you didn't bargain for all this when you signed on to help eh :P

Do you still have the problem with the bar staying full all the time?
If so, i just have to ask: Are you using the same picture for "Hunger_Bar" and "Hunger_Empty"?
Because if you are, then that's your problem. You need an "Empty" bar picture and a "Full" bar picture.
 

khmp

Sponsor

Gando":ny1b1q4i said:
@khmp
Well, i can't find anything wrong with the script you posted, except that the bars are being drawed outside the window.
The window's width is 96, and the x-position where the bars are being drawed are 120, but that's really not a big deal.
Other than that it's rock solid.

That's why you're my go to guy Gando! :wink: I corrected the above code with this version as well. The problem is indeed that the bar was being drawn outside the window.
Code:
    # First clear the bar's position. Draw the old bar first
    self.contents.blt(0, 3, @empty_bar, 
      Rect.new(0, 0, @empty_bar.width, @empty_bar.height))
    # Now draw the bar.
    self.contents.blt(0, 3, @hunger_bar, 
      Rect.new(0, 0, bar_width, @hunger_bar.height))
 
WOO HOO Gando  you're keen eye there did it, I installed the patch KHMP provided and it works like a charm :D and now that it works i only have to have 1 common event to run the hunger systems instead of two which means the hunger bar goes away when the interface switch is turned of (by another script ;);)) Now that this hud is completed I'll put is in the scripts resources with a tut. on how to get it up and working for other people who want a hunger/life system for their game. You two are awsome and credited thanks.
 

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