What is wrong with this script?

Hi, I use a very cool hunger/thirst script for my game. It calculates how long the player has been without consuming certain food items and if that time grows too long, it adds states like hungry, thirsty, starving and dehydrated. The latter two cause slip damage.

The problem I'm having with the script is that the states are not applied normally. The player walks around with no idea of needing food or water and suddenly just dies. The HP abruptly drops to 0 without giving the state warnings. What could be wrong? The script worked perfectly well in a previous version of my game. I haven't changed anything in the script apart from the configuration numbers etc.

Here's the script as it is applied in my game.

Spoiler
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:
# Hunger/Thirst
# Author: ForeverZer0
# Date: 5.14.2010
# Version: 1.0
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:
#
# Features:
#   - Easy to use
#   - MUCH less complicated than many other similar scripts, yet it has more
#     features and less code
#   - Can have hunger and thirst decrease by time intervals or by steps walked
#     by the player
#   - Ability to have each character's 'Max Hunger' increase so that they can go
#     longer in between food and drinks as they gain in power
#   - Can use script calls to easily change hunger/thirst values, and to disable
#     the system for long cutscenes, etc. so that nothing decreases while the
#     player does not have control
#   - Automatically added states if hunger/thirst is at specific rates.
#
# Compatibility:
#   - If using Zero Add-On Collection, place this script below it. There is a
#     minor issue with the Chemist Class script, but placing this script below
#     it will solve the problem.
#
# Instructions:
#   - Place script below debug, and above main (below Zer0 Add-Ons, if present)
#   - All configuration is below and described fully in its respective section
#   - The following script calls can be used in game:
#
#    ►  Zer0.hunger_system(true/false) 
#       - If false, hunger/thirst rates will not decrease. Good for long scenes
#         where the player is not in control, etc. The system will remain ON/OFF
#         until it is changed.
#
#    ►  Zer0.set_hunger(ACTOR_ID, AMOUNT)
#    ►  Zer0.set_thirst(ACTOR_ID, AMOUNT)
#       - Sets the hunger/thirst of actor in your database with ACTOR_ID to the
#         number specified by AMOUNT
#
#    ►  Zer0.set_max_hunger(ACTOR_ID, AMOUNT)
#    ►  Zer0.set_max_thirst(ACTOR_ID, AMOUNT)
#       - Sets the MAX hunger/thirst of actor in your database with ACTOR_ID to
#         the number specified by AMOUNT
#
#    ►  Zer0.recover_hunger(ACTOR_ID)
#       - Will recover all hunger/thirst to actor with ACTOR_ID. If you omit the
#         ( ) at the end of the script call, it will recover the entire party.
#
#    ►  Zer0.actor_hunger_id(ACTOR_ID)
#       - Returns the current hunger of actor with ACTOR_ID
#
#    ►  Zer0.actor_thirst_id(ACTOR_ID)
#       - Returns the current thirst of actor with ACTOR_ID
#
#    ►  Zer0.actor_hunger_pos(POSITION)
#       - Return the current hunger of actor at party POSITION
#         (0 is the leader, 1 is actor 2, etc)
#
#    ►  Zer0.actor_thirst_pos(POSITION)
#       - Return the current thist of actor at party POSITION
#         (0 is the leader, 1 is actor 2, etc)
#
#    ►  Zer0.hunger_debugger
#       - Displays info on-screen of each characters hunger/thirst, current
#         count for steps and time, and shows which features are currently ON.
#
# Special Thanks:
#   - kukusu, whose request gave me the inspiration for the script
#   - SBR*, for pointing out a stupid error I made with the stepcount
#
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:

#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:
#                          BEGIN CONFIGURATION
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:

module Zer0
 
  DEFAULT_HUNGER = 100          
  DEFAULT_THIRST = 100         
  # Default max hunger/thirst players will begin with. This can be increased as
  # the player becomes more powerful, through items, etc. if desired.
 
  BY_TIME   = false
  BY_STEPS  = true
  # There are two ways hunger and thirst can be calculated; one by just how many
  # seconds pass, and the other by how many steps have been taken. You can use
  # both if you like, but I would suggest that if you do to try to keep it more
  # skewed to one or the other as the main method of decreasing, and use the
  # other as a minor added dynamic.
 
  STEPS_HUNGER_DOWN = 100 
  STEPS_THIRST_DOWN = 80
  # Only if using 'BY_STEPS', otherwise ignore these. These are the amount
  # of steps before hunger/thirst decrease.
 
  SECS_HUNGER_DOWN = 120
  SECS_THIRST_DOWN = 120
  # Only if using 'BY_TIME', otherwise ignore these. These are the amount
  # of seconds before hunger/thirst decrease.
 
  LVL_UP_INCREASE = true
  # If true, will increase max hunger/thirst at each level up, the amount of
  # increase is defined just below.
  LVL_UP_AMOUNT = 5
  # This will be added to the actor's max hunger/thirst at each level up, only
  # if LVL_UP_INCREASE is true.

#-------------------------------------------------------------------------------
# Food/Drink Items:
#
#   Set up your food/drink items recovery rates here. Food and drink are each in
#   their own section. Use this syntax to set them up:
#
#     when ITEM_ID the return RECOVERY_AMOUNT
#
#   For example, in the first example below, it means that the item in your
#   database with ID(1) will cure hunger by 15. If RECOVER_BY_PERCENT (below) is
#   set to true, it will recover 15% of that actors MAX hunger/thirst.
#
#   You can also create items that have negative effects by making the
#   RECOVERY_AMOUNT a negative number. DO NOT have food/drink items do anything
#   other than recover hunger/thirst and/or add/remove states. Anything else,
#   such has HP/MP recovery, will have no effect.
#
#-------------------------------------------------------------------------------

  RECOVER_BY_PERCENT = true
  # Recovery can be by either absolute value, or by percent. Setting this to
  # true will make it by percent.

  def self.food(item_id)
    case item_id
    when 33 then return 20
    when 34 then return 30
    when 35 then return 15
    when 36 then return 25
    when 37 then return 35
    when 38 then return 50
    when 143 then return 20
    end
  end
 
  def self.drink(item_id)
    case item_id
    when 39 then return 15
    when 40 then return 25
    when 41 then return 35
    when 42 then return 50
    when 43 then return 75
    when 44 then return 100
    end
  end
 
#-------------------------------------------------------------------------------
# Hunger/Thirst States
#
#   Set up states that will be applied when an actors hunger/thirst fall within
#   a defined range, such as 'Starvation', 'Dehydration', etc.
#   Configure like this:
#
#   when MIN..MAX then return STATE_ID
#
#   If actor's hunger/thirst is equal to or between MIN and MAX, the state with
#   STATE_ID will be applied. This method is refreshed everytime the system
#   decreases hunger/thirst.
#
#   Keep in mind that these states will NOT be automatically cured if the actor's
#   hunger/thirst rise above. Create your food/drink items to cure these states.
#
#-------------------------------------------------------------------------------

  def self.hunger_states(current_hunger)
    case current_hunger
    when 0 then return 1
    when 1..9 then return 20
    when 10..15 then return 19
    end
  end
 
  def self.thirst_states(current_thirst)
    case current_thirst
    when 0 then return 1
    when 1..9 then return 18
    when 10..15 then return 17
    end
  end
     
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:
#                             END CONFIGURATION
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:

  def self.hunger_system(value)
    $game_party.hunger = value
    return true
  end

  def self.set_hunger(actor_id, amount)
    $game_party.actors.each {|a| a.hunger = amount if a.id == actor_id}
  end
   
  def self.set_thirst(actor_id, amount)
    $game_party.actors.each {|a| a.thirst = amount if a.id == actor_id}
  end 
 
  def self.set_max_hunger(actor_id, amount)
    $game_party.actors.each {|a| a.max_hunger = amount if a.id == actor_id}
  end
   
  def self.set_max_thirst(actor_id, amount)
    $game_party.actors.each {|a| a.max_thirst = amount if a.id == actor_id}
  end  
 
  def self.actor_hunger_id(actor_id)
    $game_party.actors.each {|a| return a.hunger if a.id == actor_id}
  end
 
  def self.actor_thirst_id(actor_id)
    $game_party.actors.each {|a| return a.thirst if a.id == actor_id}
  end
 
  def self.actor_hunger_pos(pos = 0)
    return $game_party.actors[pos].hunger if $game_party.actors[pos] != nil
  end
 
  def self.actor_thist_pos(position = 0)
    return $game_party.actors[pos].thirst if $game_party.actors[pos] != nil
  end
 
  def self.recover_hunger(actor_id = nil)
    $game_party.actors.each {|actor|
      if actor_id == nil
        actor.hunger = actor.max_hunger
        actor.thirst = actor.max_thirst
      elsif actor.id == actor_id
        actor.hunger = actor.max_hunger
        actor.thirst = actor.max_thirst
      end
    }
  end
 
  def self.hunger_debugger
    sys = "System ON: #{$game_party.hunger}\n\n" +
          "By Time: #{BY_TIME}\n" +
          "By Steps: #{BY_STEPS}\n\n"
    actors = ''
    $game_party.actors.each {|actor| actors +=
      "#{actor.name}\nHunger: #{actor.hunger}\\#{actor.max_hunger}\n" +
      "Thirst: #{actor.thirst}\\#{actor.max_thirst}\n\n"}
    ht = "HungerTime: #{$game_party.time_hunger/40}\\#{SECS_HUNGER_DOWN}\n"
    tt = "ThirstTime: #{$game_party.time_thirst/40}\\#{SECS_THIRST_DOWN}\n"
    hs = "HungerStep: #{$game_party.steps}\\#{$game_party.step_hunger}\n"
    ts = "ThirstStep: #{$game_party.steps}\\#{$game_party.step_thirst}\n"
    print(sys + actors + ht + tt + hs + ts)
  end
 
end

#-------------------------------------------------------------------------------
# ** Game_Actor
#-------------------------------------------------------------------------------

class Game_Actor < Game_Battler
 
  attr_accessor :hunger
  attr_accessor :max_hunger
  attr_accessor :thirst
  attr_accessor :max_thirst
 
  alias zer0_hunger_setup setup
  def setup(actor_id)
    zer0_hunger_setup(actor_id)
    @hunger = @max_hunger = Zer0:😀EFAULT_HUNGER
    @thirst = @max_thirst = Zer0:😀EFAULT_THIRST
    if Zer0::LVL_UP_INCREASE && @level > 1
      increase = Zer0::LVL_UP_AMOUNT
      @hunger = @max_hunger = (@level - 1) * increase + Zer0:😀EFAULT_HUNGER
      @thirst = @max_thirst = (@level - 1) * increase + Zer0:😀EFAULT_THIRST
    end
  end
 
  alias zer0_hunger_level_up exp=
  def exp=(exp)
    level = @level
    zer0_hunger_level_up(exp)
    if Zer0::LVL_UP_INCREASE && level != @level
      lvls = @level - level
      @max_hunger += (Zer0::LVL_UP_AMOUNT * lvls)
      @max_thirst += (Zer0::LVL_UP_AMOUNT * lvls)
    end
  end 
end

#-------------------------------------------------------------------------------
# ** Game_Battler
#-------------------------------------------------------------------------------

class Game_Battler
 
  alias test item_effect
  def item_effect(item)
    food_item = false
    hunger_plus = Zer0.food(item.id)
    thirst_plus = Zer0.drink(item.id)
    if hunger_plus != nil
      if Zer0::RECOVER_BY_PERCENT
        hunger_plus = (self.max_hunger * (hunger_plus * 0.01)).ceil
      end
      self.hunger += hunger_plus
      self.hunger = [[self.hunger, self.max_hunger].min, 0].max     
      food_item = true
    end
    if thirst_plus != nil
      if Zer0::RECOVER_BY_PERCENT
        thirst_plus = (self.max_thirst * (thirst_plus * 0.01)).ceil
      end
      self.thirst += thirst_plus
      self.thirst = [[self.thirst, self.max_thirst].min, 0].max
      food_item = true
    end
    if food_item
      states_plus(item.plus_state_set)
      states_minus(item.minus_state_set)
      return true
    end
    test(item)
  end
end

#-------------------------------------------------------------------------------
# ** Game_Party
#-------------------------------------------------------------------------------

class Game_Party
 
  attr_accessor :hunger
  attr_accessor :time_hunger
  attr_accessor :time_thirst
  attr_accessor :step_hunger
  attr_accessor :step_thirst
 
  alias zer0_hunger_system_init initialize
  def initialize
    zer0_hunger_system_init
    @hunger = true
    @time_hunger = @time_thirst = 0
    @step_hunger = Zer0::STEPS_HUNGER_DOWN
    @step_thirst = Zer0::STEPS_THIRST_DOWN
  end
 
  def check_hunger_states
    @actors.each {|actor|
    state_id = Zer0.hunger_states(actor.hunger)
    actor.add_state(state_id) if state_id != nil}
  end
 
  def check_thirst_states
    @actors.each {|actor|
    state_id = Zer0.thirst_states(actor.thirst)
    actor.add_state(state_id) if state_id != nil}
  end
 
end

#-------------------------------------------------------------------------------
# ** Scene_Map
#-------------------------------------------------------------------------------

class Scene_Map
 
  alias zer0_hunger_map_upd update
  def update
    zer0_hunger_map_upd
    if $game_party.hunger
      update_hunger
    end
  end
 
  def update_hunger
    if Zer0::BY_TIME
      $game_party.time_hunger += 1
      $game_party.time_thirst += 1
      if $game_party.time_hunger == (Zer0::SECS_HUNGER_DOWN * 40)
        $game_party.actors.each {|actor|
          actor.hunger -= 1 unless actor.dead? || actor.hunger == 0}
        $game_party.check_hunger_states
        $game_party.time_hunger = 0
      end
      if $game_party.time_thirst == (Zer0::SECS_THIRST_DOWN * 40)
        $game_party.actors.each {|actor|
          actor.thirst -= 1 unless actor.dead? || actor.thirst == 0}
        $game_party.check_thirst_states
        $game_party.time_thirst = 0
      end
    end
    if Zer0::BY_STEPS
      if $game_party.steps == $game_party.step_hunger
        $game_party.actors.each {|actor|
          actor.hunger -= 1 unless actor.dead? || actor.hunger == 0}
        $game_party.check_hunger_states
        $game_party.step_hunger = $game_party.steps + Zer0::STEPS_HUNGER_DOWN
      end
      if $game_party.steps == $game_party.step_thirst
        $game_party.actors.each {|actor|
          actor.thirst -= 1 unless actor.dead? || actor.thirst == 0}
        $game_party.check_thirst_states
        $game_party.step_thirst = $game_party.steps + Zer0::STEPS_THIRST_DOWN
      end
    end
  end
end
I've moved this thread to RGSSx Script Support. Please be sure to post your threads in the correct forum next time. Thank you.


RGSS Scripts is for completed scripts that YOU have created and are sharing, not for asking for help with scripts.


When you add a script to a post, please enclose it in code tags to maintain formatting and prevent the forum software converting certain character sequences to emoticons. Then, for large scripts, you can still wrap the lot in spoiler tags.


Note to people reading - this is an RMXP script.

Original thread: https://forums.rpgmakerweb.com/threads/what-is-wrong-with-this-script.17828/