Need some help with my movement script

Good time of day to you all!


Basically, what I'm looking for is a script that will "rotate" my character before moving. By "rotate" I mean that if, for example, the player is facing up and I move down, I want my character to first turn top-right, right, bottom-right, and only then down.


So the algorithm I need is as follows:


if pressed button once ->
set_direction(top-right)
wait
set_direction(right)
wait
set_direction(bottom-right)
wait
set_direction(down)
if keep pressing button ->
move


I've been struggling with this for a few days now, and the only solution I came up with, involves a counter. Here is a small fragment:


case Input.dir8
...
when 0, 5
@turn_flag = 0
when 1 # down
...
if @prev_direction == 9 # up
if @turn_flag == 0 then set_direction(7) end
if @turn_flag == 3 then set_direction(2) end
if @turn_flag > 6
set_direction(Input.dir8)
move_diagonal(6, 2)
@prev_direction = Input.dir8
end
end
...
@turn_flag += 1
end


The problem with this solution is that if I release the button before the counter ticks to 6 (like in the example above), the character will not fully rotate in the desirable direction.


I've tried using wait functions based on Fiber.yield loops but they are not working properly.
You can make an additional variable to store the direction the character is turning to as soon as the player presses the button, then check that variable if no other direction is being pressed.
Thanks, but I've already done it, I just left that part out of my code. The main problem I'm facing right now is the "rotation" itself.
Where are you running this code?


Instead of waiting, you could put this logic inside the player's update method, so that it would be checked everytime.
Where are you running this code?

I have it in the "move_by_input" function of the "Game_Player < Game_Character" class

Instead of waiting, you could put this logic inside the player's update method, so that it would be checked everytime.

Don't quite understand what you mean. Could you give me an example of how that would work?
If you're using the move_by_input then you're already doing what I suggested.


You can't put a wait / yield there, though. I can't imagine how crazy the game would get if you put a wait inside one of the update methods (those methods are supposed to run every frame, if you put a wait there, you're basically telling it to stop executing it until the next frame).


Having a variable store the direction and another variable counting how many frames have passed since the direction was pressed is the right way to do.


Something like this:


@directions = [2, 4, 8, 6]

if (Input.dir4 > 0)
@turn_to_direction = Input.dir4
@turn_delay = 0
end

if (@turn_delay > 0)
@turn_delay = @turn_delay - 1;
end

if (@turn_to_direction > 0 && @turn_delay == 0)
turn_direction_index = @directions.find_index(@turn_do_direction)
curr_direction_index = @directions.find_index(@direction)
curr_direction_index = (curr_direction_index - 1) % @directions.size

dir = @directions[curr_direction_index]
set_direction(dir)
@turn_delay = 10
end




The @turn_delay variable makes the last if statemente to only be true once every 10 frames, adding some delay between each direction change.


Please note that it's been a long time since I last used ruby / rgss3, so I may have written something wrong here.
I've been able to make some changes to my code based on your idea, and now it all works like a charm!


Thank you so much for your help!

Original thread: https://forums.rpgmakerweb.com/threads/need-some-help-with-my-movement-script.56926/