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

Legacy GM keyboard_check stuff

H

Heat4Life

Guest
So, I created this Script and the code inside of It is:
Code:
/// scr_get_input
var right_key = keyboard_check(vk_right);
var left_key = keyboard_check(vk_left);
var up_key = keyboard_check(vk_up);
var down_key = keyboard_check(vk_down);
and after that, I created an another script and the code inside of It is:
Code:
/// scr_move_state

scr_get_input();

// Get the axis
var xaxis = (right_key - left_key);
var yaxis = (down_key - up_key);

// Get direction
var 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;

// Control the sprite
image_speed = 0.2;
if(len == 0) image_index = 0;

// Vertical sprites
if(vspd > 0) {
    sprite_index = spr_player_down;
} else if(vspd < 0) {
    sprite_index = spr_player_up;
}

// Horizontal sprites
if(hspd > 0) {
    sprite_index = spr_player_right;
} else if(hspd < 0) {
    sprite_index = spr_player_left;
}
and After that I inserted the scr_move_state on the Step event of my Player Object and after that I tested my Game and It gives me this error:
Code:
ERROR!!! :: ############################################################################################
FATAL ERROR in
action number 1
of  Step Event0
for object obj_player:


Variable obj_player.right_key(100002, -2147483648) not set before reading it.
at gml_Script_scr_move_state (line 6) - var xaxis = (right_key - left_key);
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_scr_move_state (line 6)
called from - gml_Object_obj_player_StepNormalEvent_1 (line 1) - script_execute(state);
Any help will be greatly appreciated! :D
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
By declaring the variables as local using VAR you are saying to gamemaker that at the end of the script they are to be disposed of. So you are getting the directions then throwing them away, meaning that the next script hasn't got a clue what those variables actually are... So, declare the variables as instance variables (remove var before right_key, left_key, etc... and also declare them in the create event). Alternatively, have the get input script create an array and then pass that back as a return to use in the next script. Like this:

Code:
/// scr_get_input
var key;
key[0] = keyboard_check(vk_right);
key[1] = keyboard_check(vk_left);
key[2] = keyboard_check(vk_up);
key[3] = keyboard_check(vk_down);
return key;
Code:
/// scr_move_state
var input;
input = scr_get_input();

// Get the axis
var xaxis = (input[0] - input[1]);
var yaxis = (input[3] - input[2]);
// etc....
 
Top