• 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!

optimizing movement

S

supermasher/ti

Guest
is there any way i can optimize this movement code to save space

OBJ_Player

Crate//:
Code:
idle = 1;
walking = 0;

up = 3;
right = 2;
down = 1;
left = 0;

Action = idle;
Direction = down;

View[1,3] = v_p_upIdle;
View[1,2] = v_p_rightIdle;
View[1,1] = v_p_downIdle;
View[1,0] = v_p_leftIdle;

View[0,3] = v_p_upWalking;
View[0,2] = v_p_rightWalking;
View[0,1] = v_p_downWalking;
View[0,0] = v_p_leftWalking;
Step//:
Code:
/// @description
 
if (keyboard_check(vk_up) || keyboard_check(vk_right) || keyboard_check(vk_down) || keyboard_check(vk_left)) {
    Action = walking;
    if (keyboard_check(vk_up)) {Direction = up;}
    if (keyboard_check(vk_right)) {Direction = right;}
    if (keyboard_check(vk_down)) {Direction = down;}
    if (keyboard_check(vk_left)) {Direction = left;}
} else {Action = idle}

sprite_index = View[Action, Direction];


speed = 0;  //this doesn't work if you have acceleration, but you don't have acceleration probably
if (keyboard_check(vk_up) || keyboard_check(vk_right) || keyboard_check(vk_down) || keyboard_check(vk_left)) {
   Action = walking;
   if (keyboard_check(vk_up)) {Direction = up; vspeed = -2;}
   if (keyboard_check(vk_right)) {Direction = right; hspeed = 2;}
   if (keyboard_check(vk_down)) {Direction = down; vspeed = 2;}
   if (keyboard_check(vk_left)) {Direction = left; hspeed = -2;}
} else {Action = idle;}
here is a video of what I have right now

https://photos.app.goo.gl/8tfhcUpk4cZaizSZ8

and i want it to look like what i have in the video
 

2Dcube

Member
create:
Code:
key[0] = vk_right;
key[1] = vk_up;
key[2] = vk_left;
key[3] = vk_down;

idle = 1;
walking = 0;
Action = idle;

Direction = 3; // down (3 * 90 = 270)

View[1,0] = v_p_rightIdle;
View[1,1] = v_p_upIdle;
View[1,2] = v_p_leftIdle;
View[1,3] = v_p_downIdle;

View[0,0] = v_p_rightWalking;
View[0,1] = v_p_upWalking;
View[0,2] = v_p_leftWalking;
View[0,3] = v_p_downWalking;
step:
Code:
speed = 0;
Action = idle;
sprite_index = View[Action, Direction];

for(var i=0; i<4; i++)
{
  if keyboard_check(key[i])
  {
    hspeed = lengthdir_x(2,i * 90);
    vspeed = lengthdir_y(2,i * 90);

    Action = walking;
    Direction = i;
    sprite_index = View[Action, Direction];
  }
}
Something like this? I simplified all the keyboard checks you did.
 
Last edited:

Sam04

Member
is there any way i can optimize this movement code to save space

OBJ_Player

Crate//:
Code:
idle = 1;
walking = 0;

up = 3;
right = 2;
down = 1;
left = 0;

Action = idle;
Direction = down;

View[1,3] = v_p_upIdle;
View[1,2] = v_p_rightIdle;
View[1,1] = v_p_downIdle;
View[1,0] = v_p_leftIdle;

View[0,3] = v_p_upWalking;
View[0,2] = v_p_rightWalking;
View[0,1] = v_p_downWalking;
View[0,0] = v_p_leftWalking;
Step//:
Code:
/// @description
 
if (keyboard_check(vk_up) || keyboard_check(vk_right) || keyboard_check(vk_down) || keyboard_check(vk_left)) {
    Action = walking;
    if (keyboard_check(vk_up)) {Direction = up;}
    if (keyboard_check(vk_right)) {Direction = right;}
    if (keyboard_check(vk_down)) {Direction = down;}
    if (keyboard_check(vk_left)) {Direction = left;}
} else {Action = idle}

sprite_index = View[Action, Direction];


speed = 0;  //this doesn't work if you have acceleration, but you don't have acceleration probably
if (keyboard_check(vk_up) || keyboard_check(vk_right) || keyboard_check(vk_down) || keyboard_check(vk_left)) {
   Action = walking;
   if (keyboard_check(vk_up)) {Direction = up; vspeed = -2;}
   if (keyboard_check(vk_right)) {Direction = right; hspeed = 2;}
   if (keyboard_check(vk_down)) {Direction = down; vspeed = 2;}
   if (keyboard_check(vk_left)) {Direction = left; hspeed = -2;}
} else {Action = idle;}
here is a video of what I have right now

https://photos.app.goo.gl/8tfhcUpk4cZaizSZ8

and i want it to look like what i have in the video
I would recommend using enumerators and you dont really need the first check on all the keys if you are doing the same thing in the second call. It would be something like this:

Create:
Code:
enum actionList {
    walking,
    idle
}

enum directions {
    right,
    up,
    left,
    down
}

currentAction = actionList.idle;
currentDirection = directions.down;
hkey = 0;
vkey = 0;

View[1,3] = v_p_downIdle;
View[1,2] = v_p_leftIdle;
View[1,1] = v_p_upIdle;
View[1,0] = v_p_rightIdle;

View[0,3] = v_p_downWalking;
View[0,2] = v_p_leftWalking;
View[0,1] = v_p_upWalking;
View[0,0] = v_p_rightWalking;
I changed the sprites order in the array to match the angles I'm going to use.

Step:
Code:
hkey = keyboard_check(vk_right) - keyboard_check(vk_left);
vkey = keyboard_check(vk_down) - keyboard_check(vk_up);

if (hkey == 0 && vkey == 0) {
    currentAction = actionList.idle;
    speed = 0;
} else {
    currentAction = actionList.walking
    currentDirection = point_direction(0,0, hkey,vkey) div 90;
    hspeed = sign(lengthdir_x(1,currentDirection*90))*2;
    vspeed = sign(lengthdir_y(1,currentDirection*90))*2;
}
sprite_index = View[currentAction, currentDirection];
 
Top