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.

how do i change move speed by pressing a button? [SOLVED]

*EDIT* if figured out how to make the map appear and disappear when you press a button..

*EDIT #2* nevermind, i figured it all out on my own.

could a mod please close this thread.
thanks
 

khmp

Sponsor

:dead: I type way too slowly. Default speed is 4 btw.

First thing you need to do is make it possible to reach the movement speed of the player. By default there is no access to it outside of the class. So lets make an attr_accessor for the Game_Player's @move_speed. After that we will need to find the Scene where we would want the player to run around in. That scene is Scene_Map. We want to test for input as much as we can and to do that we will need to add to the update method of Scene_Map. We will then alias this method so we don't destroy all the old code like updating the map and receiving all the other input. After we alias_method the update method we can add our code to it. We want to be testing for the key being held down which is Input.press? and the key I will be checking for is Input::C. While the key is held down I want the speed to increase otherwise I want the speed value to go back to the default value which is 4.

Code:
#==============================================================================
# ** Game_Player
#------------------------------------------------------------------------------
#  This class handles the player. Its functions include event starting
#  determinants and map scrolling. Refer to "$game_player" for the one
#  instance of this class.
#==============================================================================

class Game_Player
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_accessor :move_speed
end

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

class Scene_Map
  #--------------------------------------------------------------------------
  # * Constant Variables
  #--------------------------------------------------------------------------
  Run_Speed = 5
  Run_Key = Input::C
  #--------------------------------------------------------------------------
  # * Alias Methods
  #--------------------------------------------------------------------------
  alias_method :line_running_scene_map_update, :update
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    $game_player.move_speed = Input.press?(Run_Key) ? Run_Speed : 4
    line_running_scene_map_update
  end
end

Just insert an empty section above main and post in that code. The key to run is C by default but its a constant in the Scene_Map declaration so feel free to change it. Run_Speed as well can be adjusted to whatever you like.

Good luck with it Lost_in_Exsistance! :thumb:

We don't close threads with answers. When people see a lock they think it has a negative connotation. And will look elsewhere or create a thread regarding the same topic.
 
lol sorry bout that, i didnt know you stopped doing that,
should i re-post the question i originally had in the first post? or just leave it, cause i know i didnt mention it in the first post but what i was originally looking for was a way to do it with events instead of a script using a common event.
 

khmp

Sponsor

Through events. Well I guess you will still need to use a script to do this.

In the Script Editor(F11). Insert an empty section above main and post in this code:
Code:
class Game_Character
  attr_accessor :move_speed
end

In the database go to the common event tab. In the common event make it parallel process. Use the script event call with the following:
Code:
if Input.press?(Input::C)
  $game_player.move_speed = 5
else
  $game_player.move_speed = 4
end
In game make sure the switch is enabled for this event is turned on.

Good luck with it Lost_in_Exsistance! :thumb:
 
lol i guess i should have put down what i did to do this since i did say that i solved this on my own, and i ONLY used events and it was extremely easy.
all i did was make a common even and set it to parallel process and made a switch and turned the switch on at the very beginning of my game, so you wont ever have to worry about the switch again. then in the common event this is what i did.
Code:
<>Conditional Branch: Key Y pushed
  <>Move Event:: Player
     :                :<>Change Speed: 5
     :
     <>
:  Else Handler
   <>Move Event:: Player
     :                :<>Change Speed: 4
     :
     <>
:    End
<>

it works miracles Just press and hold the "S" button on your keyboard and move around and when you let go, it changes back.
 
Sorry to ask this on a solved topic but I didn't want to make an extra one for this question.

I have a Common Event parallel process like this on my game, called "Track Player X/Y" with the same 'run' function.

If I defined the move speed with a script instead of Player move event, would it still conflict with other 'player move' event commands?

I only ask because I always have to remember to turn the switch on/off when forcing the player to move somewhere. If I forget, the player will only move 1 step and then his 'move route' is interupted by the parallel process.
 

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