• 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 Why isn't my script running correctly

Q

quill

Guest
I'm teaching myself game maker with a top down RPG. Problem is, i'm trying to use a script but its saying that the variable which is set in the script doesn't exist when its ran in the step event. Ill paste what I think may be relevant to the code here:

Script: scr_keyinput

///scr_keyinput

var right = (keyboard_check(ord('D')));
var left = (keyboard_check(ord('A')));
var up = (keyboard_check(ord('W')));
var down = (keyboard_check(ord('S')));


Create

phy_fixed_rotation = true;
player_spd = 4;
image_speed = 0

scr_keyinput();


Step


scr_keyinput();

//Move right
if (right) {
phy_position_x += player_spd;
sprite_index = spr_player_right;
image_speed = 0.2;
}

//Move left
if (left) {
phy_position_x -= player_spd;
sprite_index = spr_player_left;
image_speed = 0.2;
}

//Move up
if (up) {
phy_position_y -= player_spd;
sprite_index = spr_player_up;
image_speed = 0.2;
}

//Move down
if (down) {
phy_position_y += player_spd;
sprite_index = spr_player_down;
image_speed = 0.2;
}

//Stop animating
if (!down and !up and !right and !left) {
image_speed = 0;
image_index = 0;
}





As long as the reason isn't something simple i just missed, a short explanation of why the thing i did wrong needs to be as it would be when right would be appreciated, as of course I am trying to educate myself.

Thanks!

Edit: SOLVED! The problem was the "var", removed that and it worked fine! Thanks a lot!
 
Last edited by a moderator:
L

Linkruler

Guest
From my knowledge, using var creates a temporary variable. If you use var outside of where it's established, then I don't think GameMaker can find it. Try removing var from your key inputs (ie right = (keyboard_check(ord("D"))) and I believe that should fix it
 
L

Lars Karlsson

Guest
When you declare new variables the variables are being dumped/thrown away as soon as the script ends.

What you'll have to do is declare the vars in the create event and access them from inside the script.

Code:
[Create Event]
var right, left, up, down;

[Script]
right = (keyboard_check(ord('D')));
left = (keyboard_check(ord('A')));
up = (keyboard_check(ord('W')));
down = (keyboard_check(ord('S')));
 
Top