Need some help with my movement script

Q-Ball

Villager
Member
Joined
Feb 14, 2016
Messages
5
Reaction score
2
First Language
Russian
Primarily Uses
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.
 

Hudell

Dog Lord
Veteran
Joined
Oct 2, 2014
Messages
3,546
Reaction score
3,717
First Language
Java's Crypt
Primarily Uses
RMMZ
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.
 

Q-Ball

Villager
Member
Joined
Feb 14, 2016
Messages
5
Reaction score
2
First Language
Russian
Primarily Uses
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.
 

Hudell

Dog Lord
Veteran
Joined
Oct 2, 2014
Messages
3,546
Reaction score
3,717
First Language
Java's Crypt
Primarily Uses
RMMZ
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.
 

Q-Ball

Villager
Member
Joined
Feb 14, 2016
Messages
5
Reaction score
2
First Language
Russian
Primarily Uses
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?
 

Hudell

Dog Lord
Veteran
Joined
Oct 2, 2014
Messages
3,546
Reaction score
3,717
First Language
Java's Crypt
Primarily Uses
RMMZ
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.
 
Last edited by a moderator:

Q-Ball

Villager
Member
Joined
Feb 14, 2016
Messages
5
Reaction score
2
First Language
Russian
Primarily Uses
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!
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

Don't forget, aspiring writers: Personality isn't what your characters do, it is WHY they do it.
Hello! I would like to know if there are any pluggings or any way to customize how battles look?
I was thinking that when you start the battle for it to appear the eyes of your characters and opponents sorta like Ace Attorney.
Sadly I don't know how that would be possible so I would be needing help! If you can help me in any way I would really apreciate it!
The biggest debate we need to complete on which is better, Waffles or Pancakes?
rux
How is it going? :D
Day 9 of giveaways! 8 prizes today :D

Forum statistics

Threads
106,050
Messages
1,018,548
Members
137,835
Latest member
yetisteven
Top