• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker New script broke my character animation and can't get it back

J

Jacob Colston

Guest
As the title said I added a script that would update my player sprite based on the position of the mouse. If I had my mouse above the player sprite, the sprite would look up. This script works, however, after adding this script my character stopped conducting the movement animation. I have spent 2 days trying to get my sprite to animate to no avail. I tried everything I could come up with including creating a ton of if statements (for example if character is looking up AND is moving then sprite_index = spr_player_up) but no matter what I could not get it to work.

My end goal is to make it so that the sprite animates in the direction of the mouse when moving (so if the mouse is above the player the sprite would be animated spr_player_up, if below then spr_player_down and so on) Currently the player successfully LOOKS towards the mouse, however, it DOES NOT do the movement animation at all.

Here are my codes:

obj_player create event
event_inherited();
spd = 4;
hspd = 0;
vspd = 0;
xaxis = 0;
yaxis = 0;
len = 0;
dir = 0;
depth = -10;
scr_get_input();
state = scr_move_state;


/// Create Event - creating the sectors

player_look[5] = spr_player_down;
player_look[6] = spr_player_down;

player_look[7] = spr_player_right;
player_look[0] = spr_player_right;

player_look[1] = spr_player_up;
player_look[2] = spr_player_up;

player_look[3] = spr_player_left;
player_look[4] = spr_player_left;

obj_player step event
/// Move the player in the step event
script_execute(state);
event_inherited();


// where to look
sprite_index = player_look[point_direction(x,y,mouse_x,mouse_y) div 45];

obj_player end animation event
/// change back to move state
if (state == scr_attack_state) {
state = scr_move_state;
}

scr_move_state
///scr_move_state
scr_get_input();
image_speed = .5;

/// this line says that if player faces direction the state is to move
if (player_look[0 || 7 || 1 || 2 || 3 || 4 || 5 || 6]){
image_index = 0;
state = scr_move_state;
}


// Dashing
if(DASH && (xaxis!=0 or yaxis!=0)) {
state=scr_dash_state;
alarm[0]=room_speed/6;
}

// Attacking
if (ATTACK){
image_index = 0;
state = scr_attack_state;
}


// Get direction
dir = point_direction(0, 0, xaxis, yaxis);

// Get the length
if (xaxis == 0 and yaxis = 0) {
len = 0;
} else {
len = spd;
}

// Get the hspd and vspd
hspd = lengthdir_x(len, dir);
vspd = lengthdir_y(len, dir);

//Move
phy_position_x += hspd;
phy_position_y += vspd;

scr_get_input
///scr_get_input
MOVELEFT = keyboard_check(ord("A"));
MOVERIGHT = keyboard_check(ord("D"));
MOVEUP = keyboard_check(ord("W"));
MOVEDOWN = keyboard_check(ord("S"));
DASH = mouse_check_button_pressed(mb_right);
ATTACK = mouse_check_button_pressed(mb_left);

// Get the axis
xaxis = (MOVERIGHT - MOVELEFT);
yaxis = (MOVEDOWN - MOVEUP);


// Check for gamepad input
if (gamepad_is_connected(0)) {
gamepad_set_axis_deadzone(0, .35);
xaxis = gamepad_axis_value(0, gp_axislh);
yaxis = gamepad_axis_value(0, gp_axislv);
DASH = gamepad_button_check(0, gp_face1);
ATTACK = gamepad_button_check(0, gp_face3);
}
 
C

chico_haze

Guest
It looks like this might be setting your image_index to 0 every frame. That would break animation.

Code:
if (player_look[0 || 7 || 1 || 2 || 3 || 4 || 5 || 6]){
image_index = 0;
state = scr_move_state;
}
 
J

Jacob Colston

Guest
It looks like this might be setting your image_index to 0 every frame. That would break animation.

Code:
if (player_look[0 || 7 || 1 || 2 || 3 || 4 || 5 || 6]){
image_index = 0;
state = scr_move_state;
}
That code is used to stop the attack state from repeating endlessly while moving. Without it, the player would keep attacking while it was moving and this was the only thing that I've tried that's worked. Do you have any suggestions as to what I should do?
 
if (player_look[0 || 7 || 1 || 2 || 3 || 4 || 5 || 6]){
...how....is this even possibly working?

"0 || 7 || 1 || 2 || 3 || 4 || 5 || 6" <-- This part is a concatenated OR statement, which will return true (which at the moment would return an number 1 I guess?

so it's actually saying:

if ( player_look[1] ) <--- if whatever is in this array index is true...etc...

or am I reading this wrong?
 
J

Jacob Colston

Guest
...how....is this even possibly working?

"0 || 7 || 1 || 2 || 3 || 4 || 5 || 6" <-- This part is a concatenated OR statement, which will return true (which at the moment would return an number 1 I guess?

so it's actually saying:

if ( player_look[1] ) <--- if whatever is in this array index is true...etc...

or am I reading this wrong?
It just works? I have no idea how to explain it with words but the logic works out in my head.
 
Just had a chance to test this on my machine.

This : "0 || 7 || 1 || 2 || 3 || 4 || 5 || 6" is a boolean check.

It *always* equals TRUE, which in GMS equals the numeric value 1.

For example:

var test = 0 || 7 || 1 || 2 || 3 || 4 || 5 || 6;

In the line above, test is always assigned the value of 1.

Because in GML, any number greater than 0.5 has the value of true.

So the line is actually saying if ( false || true || true || true || true || true || true || true ) which is of course true.

And as I said, the value of true is represented as 1 in GML.

Therefore:

player_look[0 || 7 || 1 || 2 || 3 || 4 || 5 || 6] is the same as *always* saying player_look[1].

What is in your player_look array at position [1]? (i.e, player_look[1])

Because that is what is being tested every time. Only position 1.

If player_look[1] is returning a value that >= 0.5, then:

This statement:

if( player_look[0 || 7 || 1 || 2 || 3 || 4 || 5 || 6] ) is *always* returning TRUE.

Therefore, you are resetting your image_index to 0 every time this is executed.

Now this code is already part of "scr_move_state". So the line "state = scr_move_state" is redundant.

And image_index = 0 is setting the sprite frame to 0 all the time.

Looking at your code above, you are storing sprite_index's in your player_look array, so can assume that the value of player_look[1] is most likely true, unless by some chance the sprite_index was 0, but that would only happen if the sprite was the first sprite in your resource tree.

What is your scr_attack_state doing?

How do you change into scr_attack_state, I don't see where you do this?

You already change to scr_move_state in the animation end event if you are in scr_attack_state already.

Without seeing the rest of your code, I would guess you need to put a check something like:

if ( moving && attacking)
*change to move state*
 

FrostyCat

Redemption Seeker
It just works? I have no idea how to explain it with words but the logic works out in my head.
If you can't explain what you've done in words, then it's done wrong.

Take another look at the problem line again, along with its comment:
Code:
/// this line says that if player faces direction the state is to move
if (player_look[0 || 7 || 1 || 2 || 3 || 4 || 5 || 6]){
First of all, that use of || in your code is completely made-up syntax. What you think it does is most certainly not what it really does. Anyone who has read my How NOT to use && and || article would know this.

Second of all, not even the description of the line makes sense. If the player exists and its facing direction is one of its properties, then of course it is facing some direction. And from the looks of it, you wanted to check for all 8 possibilities. That always comes up true. It's like checking "if a person with a preferred hand is left-handed or right-handed". Even if the code was done to the spec, it still wouldn't do what you want because the spec was not for wanted behaviour.

You don't just have a problem with made-up syntax, you also have a problem with your idea of what needs to be done. Scrap of this part of your code, go back to your drawing board, come up with a condition that makes actual sense and don't add any code until you've thought it through.

Novices are often so eager to have code that does something, they neglect to start by figuring out what exactly that something is.
 
Top