0
\$\begingroup\$

On obj_player I have a Create and Step event

Create:

/// Intialize Variables      
grav = 0.2;      
hsp = 0;     
vsp = 0;      
jumpspeed = 7;     
movepseed = 4;    
move = key_left + key_right    

Step:

//Get the player's input       
key_right = keyboard_check(vk_right);      
key_left = -keyboard_check(vk_left);    
key_jump = keyboard_check_pressed(vk_space);     

//React to inputs      
move = key_left + key_right;      
hsp = move * movespeed;      
if (vsp < 10) vsp += grav;      

if (place_meeting(x,y+1,obj_wall))        
{         
    vsp = key_jump * -jumpspeed        
}        

//Horizontal Collision     
if (place_meeting(x+hsp,y,obj_wall))         
{       
    while(!place_meeting(x+sign(hsp),y,obj_wall))       
    {          
        x += sign(hsp);        
    }       
    hsp = 0;         
}               
x += hsp;       

//Vertical Collision     
if (place_meeting(x,y+vsp,obj_wall))     
{        
    while(!place_meeting(x,y+sign(vsp),obj_wall))    
    {    
        y += sign(vsp);    
    }    
    vsp = 0;     
}       
y += vsp;    

Error Message:

FATAL ERROR in       
action number 1       
of Create Event        
for object obj_player:        

Variable obj_player.key_left(100007, -2147483648) not set before reading it.         
 at gml_Object_obj_player_CreateEvent_1 (line 7) - move = key_left + key_right     

Whats wrong with this code? Ive been stuck on it for a couple days now, just really confused as I have seen this code work in action and for other people in this video https://www.youtube.com/watch?v=IysShLIaosk

\$\endgroup\$

closed as off-topic by Jesse Dorsey Oct 13 '16 at 21:14

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – Jesse Dorsey
If this question can be reworded to fit the rules in the help center, please edit the question.

1
\$\begingroup\$

add

key_left = 0;
key_right = 0;

before

move = key_left + key_right;

in Create:

Both variables need to be initialized to some defined value before being read (key_left + key_right is a read of both) to set the move variable.

\$\endgroup\$
  • \$\begingroup\$ I added the move variable and key_left = 0; and key_right = 0; to my Create event. I still receive " Variable obj_player.movespeed(100011, -2147483648) not set before reading it. at gml_Object_obj_player_StepNormalEvent_1 (line 8) - hsp = move * movespeed; " \$\endgroup\$ – John Oct 13 '16 at 23:54
  • \$\begingroup\$ That's a different error: You have a typo: You're initializing it as movepseed, not movespeed. \$\endgroup\$ – Stephane Hockenhull Oct 14 '16 at 0:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.