Programming Error: Unknown Variable

N

Nek0Slime

Guest
I've been programming a movement system, and there is a problem with the variables that I'm baffled with

___________________________________________
ERROR in
action number 1
of Step Event
for object obj_player:

Error in code at line 16:
hspeed = lengthdir_x(len, dir)

at position 23: Unknown variable len


but I'm certain I've already had len as a variable.
here is the full code:
///scr_move_state
scr_get_input() ;
scr_key_input() ;



// Move
x += hspeed;
y += vspeed;

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

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

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

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

if anyone can help that would be great! I'm using a mac to do this if it helps.
 
J

JTC

Guest
You must make sure that 'len' is defined in the current object, BEFORE the above code takes place. That will rectify your issue.
 

Yal

🐧 *penguin noises*
GMC Elder
Found the error.

hspeed = lengthdir_x(len, dir)<----------USAGE ATTEMPT
vspeed = lengthdir_y(len, dir)

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

//Get the length
if (xaxis == 0 and yaxis = 0) {
len = 0; <----------DEFINITION
}else {
len = spd;
}
I'd put a "len = 0" in the Create event as well. (It's generally a good habit to define everything in the create event even if it's redefined elsewhere)
 

FrostyCat

Redemption Seeker
I'd put a "len = 0" in the Create event as well. (It's generally a good habit to define everything in the create event even if it's redefined elsewhere)
No, it isn't always a good habit. If you look at the context of this piece of code, both len and dir are temporary intermediate values. They should have been set up at the beginning of the piece of code with var, not in the Create event as instance variables.
 
N

Nek0Slime

Guest
ah that works, but my speed is twice as fast than what is intended of my character!
 
G

guadalcanal

Guest
X and Y will be automatically incremented by the variables hspeed and vspeed. You should either set hspeed and vspeed to 0 after incrementing x and y manually or use different variables.
 
N

Nek0Slime

Guest
although putting var makes it an "unknown variable" despite adding it to the code already. removing the var fixes it but i hope nothing too drastic will be affected
 
N

Nek0Slime

Guest
once again, more problems show up the more i code. I'm trying to make an attack state, everything should be working fine, but when i press the key, nothing happens, i don't understand it!

///scr_attack_state
image_speed=1;

switch (sprite_index) {
case spr_player_down:
sprite_index = spr_player_attack_down;
break;

case spr_player_up:
sprite_index = spr_player_attack_up;
break;

case spr_player_right:
sprite_index = spr_player_attack_right;
break;

case spr_player_left:
sprite_index = spr_player_attack_left;
break;
}
is my attack state


and an excerpt from my changed move state (basically the same as above but this added)


if (keyboard_check_pressed(vk_space))
sprite_index = 0;
state = scr_attack_state



i dont understand whats wrong!
 

Yal

🐧 *penguin noises*
GMC Elder
Do you actually run the state script somewhere, or do you just change the state variable?
 
N

Nek0Slime

Guest
Its whats on the screen, im pretty new, sorry. how do you make it run then?
 

Yal

🐧 *penguin noises*
GMC Elder
Something like this:
upload_2016-7-15_18-12-37.png

(For a lot of my objects, that's the entire step event :p)


In your case, the script_execute line should be enough.
 
N

Nek0Slime

Guest
hmm, I've moved it to the input area, so now it actually responds to the space bar key, but it just changes the sprite to 0, making a blank space where the character is at instead of running the attack script
 
N

Nek0Slime

Guest
if (keyboard_check(vk_space))
script_execute(scr_attack_state)
sprite_index = 0;

I've tried changing it like this, is this how it works?
 
N

Nek0Slime

Guest
if i remove the sprite index, it works, but only after i let go of the space bar, and after i move the character in a direction :L how do you make the character attack with just pressing in the direction your facing?
 

Yal

🐧 *penguin noises*
GMC Elder
You should run the state script ( execute_script(state) ) in the step event, but change the contents of the state variable when you press the attack button. (You'd always have a script value in the state variable so that you can always execute it)
 
Top