[XP] Simple Player Idle Sprites - wrong sprite being loaded

Hello! I'm using this simple script by for idle sprite animations that I found here: https://forum.chaos-project.com/index.php/topic,16211.0.html
It worked well until I started messing around with changing player character sprites with variables and conditional branches a little. To simplify, I put something akin to this in a parallel process:

1.png

This makes it revert back to the initial sprite as soon as the player character moves...

Holding a button for a bit while walking will switch the graphic properly, setting a player to move in a circle often fixes it too, which makes me think something about the idle script is loading only during walking. Either that, or I'm understanding something wrong about how events like these work, as I'm still a beginner.

I would appreciate any help for fixing this! Any alternative method for having idle animations would work, too.
Looking at the idle script, it only updates once per "transition" from walking to idle. It keeps internal track of whether the player is walking or idling and had been on the previous frame. If the play is idling and was previously idling, then it doesn't do anything. Likewise if the player was walking and was previously walking, then it doesn't do anything. So let's run through an example sequence.

  1. The character's graphic starts out as 'Fighter01'
  2. The player stops moving
  3. The idle script:
    1. Stores the original character name of 'Fighter01'
    2. Changes the character name to 'Fighter01_idl'
    3. Enables the "step animation" to play the idle loop
    4. Marks itself as being in the 'idle' state
  4. On the next frame, nothing happens because the player is still idling (Note that the idle script doesn't re-do anything it's already done)
  5. Using your parallel process (or some other event):
    1. You change the character graphic to 'Fighter02'
    2. You don't press any move buttons
  6. The character graphic is now 'Fighter02' with step animation still enabled
  7. The idle script sees that we're still idling so it doesn't do anything
  8. You press to move
  9. The idle script sees that we're now moving so it:
    1. Disables the "step animation"
    2. Changes the character name back to what it had memorized which is 'Fighter01'
    3. Marks itself as being in the moving state
  10. It appears that your change had no effect
If you work through the logic for changing the graphic while walking, you should see that upon stopping it grabs that properly. So we just need some small tweaks to catch when a new graphic has been set and then either apply the idle state to the new graphic if we're idling or leave it alone if we're moving. I made those tweaks in the script below. Try it out and see if that solves your problem.
Spoiler
Ruby:
#==============================================================================
# ** Game_Player
#------------------------------------------------------------------------------
#  With idle sprites.  name the idle sprites the same as their normal ones
#  except with '_idl' on the end (before the extension)
#==============================================================================

class Game_Player < Game_Character
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_reader   :character_name_org       # original character file name
  attr_reader   :current_sprite           # current sprite suffix
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  alias initialize_idlesprites_before initialize
  def initialize
    initialize_idlesprites_before
    # set original character name
    @character_name_org = @character_name.clone
    # set current sprite
    @idling = false
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  alias update_idlesprites_before update
  def update
    update_idlesprites_before
    update_idlesprites
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  alias refresh_idlesprites_before refresh
  def refresh
    refresh_idlesprites_before   
    if @idling
      @character_name_org = @character_name.clone
      @character_name += '_idl'
    end
  end
  #--------------------------------------------------------------------------
  # * update_idlesprites
  # Updates idle sprites of the player
  #--------------------------------------------------------------------------
  def update_idlesprites
    if @character_name != ""
      if moving? && @idling
        @step_anime = false
        @character_name = @character_name_org.clone
        @idling = false
      elsif (not moving?) && (Input.dir4 == 0) && (not @idling)
        @character_name_org = @character_name.clone
        @step_anime = true
        @character_name += '_idl'
        @idling = true
      end
    end
  end
end
Looking at the idle script, it only updates once per "transition" from walking to idle. It keeps internal track of whether the player is walking or idling and had been on the previous frame. If the play is idling and was previously idling, then it doesn't do anything. Likewise if the player was walking and was previously walking, then it doesn't do anything. So let's run through an example sequence.

  1. The character's graphic starts out as 'Fighter01'
  2. The player stops moving
  3. The idle script:
    1. Stores the original character name of 'Fighter01'
    2. Changes the character name to 'Fighter01_idl'
    3. Enables the "step animation" to play the idle loop
    4. Marks itself as being in the 'idle' state
  4. On the next frame, nothing happens because the player is still idling (Note that the idle script doesn't re-do anything it's already done)
  5. Using your parallel process (or some other event):
    1. You change the character graphic to 'Fighter02'
    2. You don't press any move buttons
  6. The character graphic is now 'Fighter02' with step animation still enabled
  7. The idle script sees that we're still idling so it doesn't do anything
  8. You press to move
  9. The idle script sees that we're now moving so it:
    1. Disables the "step animation"
    2. Changes the character name back to what it had memorized which is 'Fighter01'
    3. Marks itself as being in the moving state
  10. It appears that your change had no effect
If you work through the logic for changing the graphic while walking, you should see that upon stopping it grabs that properly. So we just need some small tweaks to catch when a new graphic has been set and then either apply the idle state to the new graphic if we're idling or leave it alone if we're moving. I made those tweaks in the script below. Try it out and see if that solves your problem.
Spoiler
Ruby:
#==============================================================================
# ** Game_Player
#------------------------------------------------------------------------------
#  With idle sprites.  name the idle sprites the same as their normal ones
#  except with '_idl' on the end (before the extension)
#==============================================================================

class Game_Player < Game_Character
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_reader   :character_name_org       # original character file name
  attr_reader   :current_sprite           # current sprite suffix
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  alias initialize_idlesprites_before initialize
  def initialize
    initialize_idlesprites_before
    # set original character name
    @character_name_org = @character_name.clone
    # set current sprite
    @idling = false
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  alias update_idlesprites_before update
  def update
    update_idlesprites_before
    update_idlesprites
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  alias refresh_idlesprites_before refresh
  def refresh
    refresh_idlesprites_before  
    if @idling
      @character_name_org = @character_name.clone
      @character_name += '_idl'
    end
  end
  #--------------------------------------------------------------------------
  # * update_idlesprites
  # Updates idle sprites of the player
  #--------------------------------------------------------------------------
  def update_idlesprites
    if @character_name != ""
      if moving? && @idling
        @step_anime = false
        @character_name = @character_name_org.clone
        @idling = false
      elsif (not moving?) && (Input.dir4 == 0) && (not @idling)
        @character_name_org = @character_name.clone
        @step_anime = true
        @character_name += '_idl'
        @idling = true
      end
    end
  end
end
So initially the script had the same issue as before and changing the player graphic alone doesn't work. Interestingly, what works now is simply changing the party members, which didn't work with the previous version of the script. No idea how that happened but hey, if it works, it works - thank you for your help!

Original thread: https://forums.rpgmakerweb.com/threads/xp-simple-player-idle-sprites-wrong-sprite-being-loaded.176258/