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.

Execute code on player stop

Basically what I'm trying to do is execute a piece of code everytime the player is in a stopped state, ie. not moving. It doesn't matter what this code is, so for my current purposes let's just say it's a print function.

I've had two ideas so far, but both don't seem to work quite how I want it.

Firstly, in Game_Character.update:
Code:
      if not @step_anime and @stop_count > 0
        # Return to original pattern
        @pattern = @original_pattern
      # If stop animation is ON when moving
      else
        # Update pattern
        @pattern = (@pattern + 1) % 4
      end
if I add "print "blah" so it looks like this:
Code:
      if not @step_anime and @stop_count > 0
        # Return to original pattern
        @pattern = @original_pattern
        print "blah"
      # If stop animation is ON when moving
      else
        # Update pattern
        @pattern = (@pattern + 1) % 4
      end
it should conceivably output a message dialog everytime the player stops. And it does... sorta. You see I get the dialog most times, but not every single time - if I did it would be perfect. Maybe it's because the stop_count resets right when the character stops, so if I let go of the key on the same frame when he stops, the branch is no longer executed?


My other method was in Game_Player.update, specifically this part:
Code:
    # If not moving
    unless moving?
      # If player was moving last time
      if last_moving
        # Event determinant is via touch of same position event
        result = check_event_trigger_here([1,2])
        # If event which started does not exist
        if result == false
          # Disregard if debug mode is ON and ctrl key was pressed
          unless $DEBUG and Input.press?(Input::CTRL)
            # Encounter countdown
            if @encounter_count > 0
              @encounter_count -= 1
            end
          end
        end
      end
      # If C button was pressed
      if Input.trigger?(Input::C)
        # Same position and front event determinant
        check_event_trigger_here([0])
        check_event_trigger_there([0,1,2])
      end
    end
If I modify it so something only executes when a direction is not being pressed (otherwise the dialog will come up every square that is walked) like so:
Code:
    # If not moving
    unless moving?
      # If player was moving last time
      if last_moving
        if Input.dir4 == 0
          print "blah"
        end
        # Event determinant is via touch of same position event
        result = check_event_trigger_here([1,2])
        # If event which started does not exist
        if result == false
          # Disregard if debug mode is ON and ctrl key was pressed
          unless $DEBUG and Input.press?(Input::CTRL)
            # Encounter countdown
            if @encounter_count > 0
              @encounter_count -= 1
            end
          end
        end
      end
      # If C button was pressed
      if Input.trigger?(Input::C)
        # Same position and front event determinant
        check_event_trigger_here([0])
        check_event_trigger_there([0,1,2])
      end
    end
it works a treat - except that the code seems to get executed a frame or two too early, just before the sprite stops.

Any ideas?
(writing Scene_Map conditions are out of the question as that is a sloppy way to do it when most of the flags are already there - and besides, I've already got it working that way, but I am a perfectionist)
 
Try this:
Code:
      if not @step_anime and @stop_count > 0
        @pattern = @original_pattern
        if @temp_flag.nil?
          p "Blah"
          @temp_flag = true
        end
      else
        @temp_flag = nil
        @pattern = (@pattern + 1) % 4
      end
In the Game_Character update, for sure.
 
What I mean is what I said in my first post. I mean, what you posted works fine and all, but keep going and stopping, going and stopping, taking your finger off the arrow key at random times, and every so often there won't be a "Blah" dialog. So your @temp_flag didn't make any difference to my original try :P
 
Add this to Game_Character
Code:
  def update
    p "blah" if !@temp_flag.nil?
# the rest of the code....

I don´t quite understand what you want... ALso, if you need to wait one frame just take out the p "blah" in the if-end statement.

BTW, what will you call? evals or Procs?
 
I'll be calling a method that changes the player graphic. My problem is not the method, just when it will be executed, which is when the player sprite is not moving/animating.
 
This may be a little tricky, but may work (i guess...)
Game_Player
Code:
  def update
    last_moving = moving?
    unless moving? or $game_system.map_interpreter.running? or
           @move_route_forcing or $game_temp.message_window_showing
      case Input.dir4
      when 2
        move_down
      when 4
        move_left
      when 6
        move_right
      when 8
        move_up
      when 0
        p "blah" if not moving? and @pattern == 0
      end
    end
#... Rest of the code...
To make sure i maximized window and put framerate down to 10 xD
 
Only problem with that method is that it constantly executes it while the player is stopped, rather than just once :( It's probably my fault for not making it clear enough :P
 
So, make the @temp_flag thing.
Code:
case Input.dir4
      when 2
        move_down
        @temp_blah = nil
      when 4
        move_left
        @temp_blah = nil
      when 6
        move_right
        @temp_blah = nil
      when 8
        move_up
        @temp_blah = nil
      when 0
        if not moving? and @pattern == 0 and @temp_blah.nil?
          p "blah" 
          @temp_blah = true
        end
      end

Haven´t tested though (i´m without RMXP here now...) ^^
 
Sorry for the late reply, but it does indeed do what I want :)

I can't help but think it's not the cleanest way of doing it though, so I'll play around with some other things, but at least I know this one method works.
 

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