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.

Synthesize's Stand/Walk/Run Script Problems

Status
Not open for further replies.
I'm referencing this script:

http://www.rmxp.org/forums/index.php?topic=42178.0

It's Synthesize's Stand/Walk/Run Script for RMVX (it's outdated, so I can't post in the topic).
Note: In his demo, rename the '_walk' sprite to '_idle'.

It works fine, except when you run, then stop, your character sprite does not cycle automatically to idle. The script also seems to require that there be an _idle version of absolutely everything, even '_run' sprites, or else it crashes. How can I fix this so that it doesn't crash if there is no '_idle' at all?

Here's the version I edited (note: the stuff about run counts is edited because I don't want it.)

Code:
#===============================================================================
# Stand/Walk/Run Script --- RMVX Version
#===============================================================================
# Written by Synthesize
# Version 2.00
# January 26, 2008
#     Revised: March 1, 2008
#===============================================================================
# Customization
#-------------------------------------------------------------------------------
module StandWalkRun
  Use_run = false   # Use Run Points?
  Use_run_sprite = true    # Use a Running sprite?
  Run_sprite_suffix = '_run'   # Running Sprite Suffix
  Run_points = 0   # The maximum amount of Run Points
  Run_points_restore = 0   # 1 Run Point is restored in X Frames
  Restore_run_while_walking = false   # Restore points while walking?
  Use_idle_sprite = true   # Use Idle Sprite?
  Idle_sprite_suffix = '_idle'   # idle Sprite Suffix
  Use_anime = true   # Animate your Idle Sprite?
  Idle_time = 1    # Time before sprite is animated
end
#-------------------------------------------------------------------------------
# Scene_Map:: The main functions of the script are here
#-------------------------------------------------------------------------------
class Scene_Map < Scene_Base
  # Aliases
  alias syn_map_update update
  alias syn_map_start start
  #-----------------------------------------------------------------------------
  # Initiate variables
  #-----------------------------------------------------------------------------
  def start
    $game_player.old_character_name = $game_player.character_name
    @wait_time = 0
    @wait_time2 = 0
    syn_map_start
  end
  #-----------------------------------------------------------------------------
  # Rewrite Scene_Change to reset sprite
  #-----------------------------------------------------------------------------
  def update_scene_change
    return if $game_player.moving? 
    case $game_temp.next_scene
    when "battle"
      call_idle($game_player.old_character_name, false)
      call_battle
    when "shop"
      call_idle($game_player.old_character_name, false)
      call_shop
    when "name"
      call_idle($game_player.old_character_name, false)
      call_name
    when "menu"
      call_idle($game_player.old_character_name, false)
      call_menu
    when "save"
      call_idle($game_player.old_character_name, false)
      call_save
    when "debug"
      call_idle($game_player.old_character_name, false)
      call_debug
    when "gameover"
      call_gameover
    when "title"
      call_title
    else
      $game_temp.next_scene = nil
    end
  end
  #-----------------------------------------------------------------------------
  # Update:: Update the scene
  #-----------------------------------------------------------------------------
  def update
    syn_map_update
    if Input.dir4 == 0
      wait(1, false) if StandWalkRun::Use_idle_sprite
      call_idle($game_player.character_name + StandWalkRun::Idle_sprite_suffix, StandWalkRun::Use_anime) if @wait_time == StandWalkRun::Idle_time
      $game_temp.syn_state = "idle"
      restore_run if StandWalkRun::Use_run
    else
      $game_temp.syn_state = ""
      restore_run if StandWalkRun::Restore_run_while_walking
      call_idle($game_player.old_character_name, false) if $game_player.character_name != $game_player.old_character_name
    end
  end
  #-----------------------------------------------------------------------------
  # Call_Idle:: Sets and animates the idle Sprite
  #-----------------------------------------------------------------------------
  def call_idle(sprite, anime)
    $game_player.set_step_anime(anime)
    $game_player.set_graphic(sprite, 0)
  end
  #-----------------------------------------------------------------------------
  # Restore_Run: Restore Run Points
  #-----------------------------------------------------------------------------
  def restore_run
    if $game_player.run_points < StandWalkRun::Run_points
      wait(1, true)
      $game_player.run_points += 1 if @wait_time2 == StandWalkRun::Run_points_restore
      @wait_time2 = 0 if @wait_time2 == StandWalkRun::Run_points_restore
    end
  end
  #-----------------------------------------------------------------------------
  # Wait:: Allows Wait Times
  #-----------------------------------------------------------------------------
  def wait(duration, value)
    for i in 0...duration
      @wait_time += 1 if value == false
      @wait_time2 += 1 if value
      break if i >= duration / 2
    end
  end
end  
#-------------------------------------------------------------------------------
# Game_Temp:: Create current state
#-------------------------------------------------------------------------------
class Game_Temp
  attr_accessor :syn_state
  alias syn_temp_init initialize
  def initialize
    @syn_state = ""
    syn_temp_init
  end
end
#-------------------------------------------------------------------------------
# Game_Character:: Create the Change_Sprite method
#-------------------------------------------------------------------------------
class Game_Character
  # Attr(s)
  attr_accessor :old_character_name
  attr_accessor :run_points
  alias syn_ch_init initialize
  #-----------------------------------------------------------------------------
  # Initialize Variables
  #-----------------------------------------------------------------------------
  def initialize
    @run_points = StandWalkRun::Run_points
    syn_ch_init
  end
  #-----------------------------------------------------------------------------
  # Set Setp Animation
  #-----------------------------------------------------------------------------
  def set_step_anime(value)
    @step_anime = value
    return @step_anime
  end
end
#-------------------------------------------------------------------------------
# Game_Player:: This handles the dash process
#-------------------------------------------------------------------------------
class Game_Player < Game_Character
  alias syn_player_update update
  alias syn_player_dash dash?
  def dash?
    return false if @run_points == 0 and StandWalkRun::Use_run
    syn_player_dash
  end
  #-----------------------------------------------------------------------------
  # Update:: Update the scene
  #----------------------------------------------------------------------------
  def update
    if dash?
      unless $game_temp.syn_state == "idle"
        set_graphic(@character_name + StandWalkRun::Run_sprite_suffix, 0) if StandWalkRun::Use_run_sprite
        @run_points -= 0
        syn_player_update        
      end
    else
      changed = false
      syn_player_update
    end
  end
end
#-------------------------------------------------------------------------------
#            * This script is not compatible with RPG Maker XP *
#-------------------------------------------------------------------------------
# Written by Synthesize
# Version 2.00
# Requested by Cerulean Skye
#===============================================================================
# Stand/Walk/Run   - RMVX Version
#===============================================================================

I was starting to learn RGSS, and now this RGSS2 stuff is killing me T_T

I appreciate any help I can get on this matter.

Thank you!!!

P.S., if you're wondering, no, I'm not giving up on my main project in RMXP, this is simply a side project so I can learn RMVX better. So far, not likin' it too much :/
 

khmp

Sponsor

Here is a rewrite of what I think what you were asking about. I sure hope Synthesize doesn't kill me for messing with his work so much. For starters when no key is being held down we drop into idle. Depending upon the Idle constant. Being false we move on, or if it's true, attempt to load a different sprite sheet containing the idle animation. What happens if we want our player to change to a different sprite sheet to idle and what happens if the image doesn't exist?

Well taking notes on the way Mr. SephirothSpawn solved your script request he attempted to load the new image through the Cache. Using a begin and rescue you can determine whether or not the image was loaded or not.

Quick Example:
Code:
character_name = $game_player.orig_char_name
begin
  Cache.character(character_name + Idle_Sprite_Suffix)
  character_name += Idle_Sprite_Suffix
rescue

end
$game_player.set_graphic(character_name, 0)

If it passes meaning the image exists we can change to the new image without a problem. Otherwise it doesn't exist and we can default back to the previous image. This will rid you of the annoying crash regarding missing images even if the constant that says to use Idle/Run Sprite is set to true. The same exact behavior is implemented for running.

Code:
#==============================================================================
# Stand/Walk/Run Script --- RMVX Version
#------------------------------------------------------------------------------
# Written by Synthesize
# Version 2.00
# January 26, 2008
#     Revised: March 1, 2008
#     Rewritten: June 2, 2008 - Support/Request
#------------------------------------------------------------------------------
# Customization
#==============================================================================

module Syn_IWR
  # Sprite Specific
  Use_Idle_Sprite = true      # Use different Sprite Sheet for Idle
  Idle_Suffix = '_idle'       # Idle Sprite Suffix
  Use_Run_Sprite = true       # Use different Sprite Sheer for Running
  Run_Suffix = '_run'         # Running Sprite Suffix
  
  # Run Point Specific
  Use_Run_Points = true       # Use Run Points
  Run_Points = 100            # The starting amount of Run Points
  Run_Point_Max = 100         # The maximum amount of Run Points
  Run_Points_Drained = 1      # Drain (X) Points from Run Points when Running
  Run_Points_Restore = 1      # Restore (X) Points to Run per Idling/Walking
  
  Animate_When_Idle = true    # Animate when Idling
  Restore_When_Walking = false# Restore points while walking
  Seconds_Before_Idle = 2     # The amount of time before dropping into Idle
end

#==============================================================================
# ** Scene_Map
#------------------------------------------------------------------------------
#  This class performs the map screen processing.
#==============================================================================

class Scene_Map < Scene_Base
  #--------------------------------------------------------------------------
  # * Alias Methods
  #--------------------------------------------------------------------------
  alias syn_map_update update
  alias syn_map_start start
  #--------------------------------------------------------------------------
  # * Start Processing                                                !ALIAS!
  #--------------------------------------------------------------------------
  def start
    @idle_timer = Syn_IWR::Seconds_Before_Idle * Graphics.frame_rate
    syn_map_start
  end
  #--------------------------------------------------------------------------
  # * Frame Update                                                    !ALIAS!
  #--------------------------------------------------------------------------
  def update
    syn_map_update
    
    # If the player is not moving.
    if Input.dir4 == 0
      
      # If Run Points matter.
      if Syn_IWR::Use_Run_Points
        # Gain back some stamina if not at the maximum already.
        if $game_player.run_points < Syn_IWR::Run_Point_Max
          $game_player.run_points += Syn_IWR::Run_Points_Restore
        elsif $game_player.run_points >= Syn_IWR::Run_Point_Max
          $game_player.can_run = true
        end
      end
      
      # Decrement the Idle Timer
      @idle_timer -= 1
      
      # Turn it off because we aren't sure of the current state.
      $game_player.step_anime = false
      
      # No need to continue if we are still counting down.
      return if @idle_timer > 0
      
      # Enable animated walking if requested by the user.
      $game_player.step_anime = true if Syn_IWR::Animate_When_Idle
      
      # If the user wants to use an Idle sprite for the player.
      if Syn_IWR::Use_Idle_Sprite && $game_player.character_name != 
        $game_player.orig_char_name + Syn_IWR::Idle_Suffix
        
        # Grab the original untouched name.
        character_name = $game_player.orig_char_name
        
        # Try to load the image. If it fails there is no Idle image.
        begin
          Cache.character(character_name + Syn_IWR::Idle_Suffix)
          character_name += Syn_IWR::Idle_Suffix
        rescue
          
        end
        
        # Try to set the image for the character.
        set_player_graphic(character_name)
      end
    else
      
      # Reset the Idle Timer.
      @idle_timer = Syn_IWR::Seconds_Before_Idle * Graphics.frame_rate
      
      # If the player can run.
      if $game_player.dash?
        
        # If Run Points matter.
        if Syn_IWR::Use_Run_Points
          # If we have stamina to lose drain it.
          if $game_player.run_points > 0
            $game_player.run_points -= Syn_IWR::Run_Points_Drained
          elsif $game_player.run_points <= 0
            $game_player.can_run = false
          end
        end
        
        # If the user wants a different Sprite Sheet for running.
        if Syn_IWR::Use_Run_Sprite && $game_player.character_name != 
          $game_player.orig_char_name + Syn_IWR::Run_Suffix
          
          # Grab the original untouched name.
          character_name = $game_player.orig_char_name
          
          # Try to load the image. If it fails there is no Run image.
          begin
            Cache.character(character_name + Syn_IWR::Run_Suffix)
            character_name += Syn_IWR::Run_Suffix
          rescue
            
          end
          
          # Try to set the image for the character.
          set_player_graphic(character_name)
        end
      else
        # Set it back to the default.
        set_player_graphic($game_player.orig_char_name)
        
        # If we can restore stamina while walking.
        if Syn_IWR::Use_Run_Points && Syn_IWR::Restore_When_Walking
          # Gain back some stamina if not at the maximum already.
          if $game_player.run_points < Syn_IWR::Run_Point_Max
            $game_player.run_points += Syn_IWR::Run_Points_Restore
          elsif $game_player.run_points >= Syn_IWR::Run_Point_Max
            $game_player.can_run = true
          end
        end
      end
    end
  end
  #--------------------------------------------------------------------------
  # * Set Player Graphic
  #     character_name  : new character graphic filename
  #--------------------------------------------------------------------------
  def set_player_graphic(character_name)
    $game_player.set_graphic(character_name, $game_player.character_index)
  end
end  

#==============================================================================
# ** Game_Character
#------------------------------------------------------------------------------
#  This class deals with characters. It's used as a superclass of the
# Game_Player and Game_Event classes.
#==============================================================================

class Game_Character
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_accessor :run_points
  attr_accessor :step_anime
  attr_accessor :can_run
  #--------------------------------------------------------------------------
  # * Alias Methods
  #--------------------------------------------------------------------------
  alias syn_ch_init initialize
  #--------------------------------------------------------------------------
  # * Object Initialization                                           !ALIAS!
  #--------------------------------------------------------------------------
  def initialize
    @run_points = [Syn_IWR::Run_Points, Syn_IWR::Run_Point_Max].min
    @can_run = (Syn_IWR::Run_Points > 0)
    syn_ch_init
  end
  #--------------------------------------------------------------------------
  # * Original Character Name
  #--------------------------------------------------------------------------
  def orig_char_name
    @orig_character_name = character_name if @orig_character_name.nil?
    return @orig_character_name
  end
end

#==============================================================================
# ** Game_Player
#------------------------------------------------------------------------------
#  This class handles maps. It includes event starting determinants and map
# scrolling functions. The instance of this class is referenced by $game_map.
#==============================================================================

class Game_Player < Game_Character
  #--------------------------------------------------------------------------
  # * Alias Methods
  #--------------------------------------------------------------------------
  alias syn_player_dash dash?
  #--------------------------------------------------------------------------
  # * Determine if Dashing                                            !ALIAS!
  #--------------------------------------------------------------------------
  def dash?
    return false if @run_points == 0 && Syn_IWR::Use_Run_Points
    return false unless @can_run
    syn_player_dash
  end
end

#-------------------------------------------------------------------------------
#            * This script is not compatible with RPG Maker XP *
#-------------------------------------------------------------------------------
# Written by Synthesize
# Version 2.00
# Requested by Cerulean Skye
#===============================================================================
# Stand/Walk/Run   - RMVX Version
#===============================================================================

Good luck with it ma'am! :thumb:
 
khmp you're my savior T_T

I'm at work right now, but I'll use this info tonight!! Thank you!!


Edit: Oh dear god, it works T_T

I never, ever would have even come close to thinking of what you did. I kept trying to fix it and then it'd just lose all idle functionality altogether. So frustrating. It's a wonder I can code anything.

Thank you T_T x1,000,000,000
 
Status
Not open for further replies.

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