GMS 2 How to store an objects x position on the first step of a state

T

Triela1

Guest
Hello,
New to GMS, first game and currently working on the movement for a platformer and I'm stuck on one particular movement.

When on the ground the player is in the "grounded" state, by tapping left or right on the analogue stick the player enters the "dash" state.
When in the "dash" state I wish for the player to move a set distance from where the dash started but in order to do that I have to find and store the x position when first entering the dash state.

I have tried everything I could think of, exhausted google and these forums but the closest I could get is to endlessly move in the desired direction with

move_towards_point((x + dash_distance), y, dash_spd);

Any attempt to store the x position resulted in x being 0 and the player moving to x = 0 at dash_spd then vibrating.

Any help is greatly appreciated.
Thank you

TL;DR : Need to store players x position to move a set distance from that position.
 

Simon Gust

Member
Do it upon state change.
I mean, somewhere in your code there has to be a "state = dash;" line.
In the same block of code, you could set a variable dash_start_x to x for example.
and then you could call
Code:
move_towards_point((dash_start_x + dash_distance), y, dash_spd);
 
T

Triela1

Guest
Do it upon state change.
I mean, somewhere in your code there has to be a "state = dash;" line.
In the same block of code, you could set a variable dash_start_x to x for example.
and then you could call
Code:
move_towards_point((dash_start_x + dash_distance), y, dash_spd);
So I tried what you said.
my code for changing to dash state is

if ((xaxis > -0.3) and (xaxis < 0.3)) {
dash_timer = room_speed * 1;
}

if dash_timer > 0 and ((xaxis > 0.3) or (xaxis < -0.3)) {
dash_timer -= room_speed * 0.3;
}

if ((dash_timer > 0) and (xaxis > 0.99)) {
player_state = "right_dash";
}

if ((dash_timer > 0) and (xaxis < -0.99)) {
player_state = "left_dash";
}

If I put dash_start_x = x below or above player_state = "dash" then I get a variable not set error from the move_towards_point code.
If I put dash_start_x = x at the start of the state then I endlessly move in one direction.

My dash state looks like this

sprite_index = spr_right_dash;

move_towards_point((dash_start_x + dash_distance), y, dash_spd);

if (gamepad_button_check_pressed(0, gp_face2)) {
player_state = "grounded";
}
 

TheouAegis

Member
if you are getting a variable not set error, that would imply that you have the dash state set from somewhere else other than just that code, assuming no typos. You can set a variable anywhere you want, it doesn't matter if it's in the create event, it just has to be set before you read it. So if it's saying it's not set, then it means you change to the dash state somewhere where it does not set that variable; or, more pslausibly, you have a typo and you ended up setting the wrong variable.
 
T

Triela1

Guest
fixed it. problem was I used a local variable. with an instance variable its working just fine.

setting dash_start_x = x in the code that changes to dash state is just what I needed.

Thank you for all for the help.
 
Top