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

GML Why isn't my instance variables working?

L

Littlesticks

Guest
My code isn't working but the game can run how is it so or am I just retarded? The game will run but I can't move my character? Am I doing something wrong?

Create Event -
Code:
//For Movement
jump = keyboard_check_pressed(vk_space);
down = keyboard_check(ord("S"));
left = keyboard_check(ord("A"));
right = keyboard_check(ord("D"));
sprint_key = keyboard_check(vk_shift);

move_speed = 10;
jump_speed = 10;

sprint = 10;

state = scr_player_move;
Step Event -
Code:
script_execute(state);


scr_player_move -
Code:
if (left || right) {
    
    x -= (left-right)*move_speed;
    if (sprint_key){
        move_speed += sprint;
    }
    
}


//Gravity

if !place_meeting(x,y+1,obj_floor) {
    y += 1;
}

if (jump) {
    y -= jump_speed;
}

If you're wondering. Yes, the Gravity section of the code is working perfectly.
 

jo-thijs

Member
My code isn't working but the game can run how is it so or am I just retarded? The game will run but I can't move my character? Am I doing something wrong?

Create Event -
Code:
//For Movement
jump = keyboard_check_pressed(vk_space);
down = keyboard_check(ord("S"));
left = keyboard_check(ord("A"));
right = keyboard_check(ord("D"));
sprint_key = keyboard_check(vk_shift);

move_speed = 10;
jump_speed = 10;

sprint = 10;

state = scr_player_move;
Step Event -
Code:
script_execute(state);


scr_player_move -
Code:
if (left || right) {
   
    x -= (left-right)*move_speed;
    if (sprint_key){
        move_speed += sprint;
    }
   
}


//Gravity

if !place_meeting(x,y+1,obj_floor) {
    y += 1;
}

if (jump) {
    y -= jump_speed;
}

If you're wondering. Yes, the Gravity section of the code is working perfectly.
Hi and welcome to the GMC!

This part:
Code:
//For Movement
jump = keyboard_check_pressed(vk_space);
down = keyboard_check(ord("S"));
left = keyboard_check(ord("A"));
right = keyboard_check(ord("D"));
sprint_key = keyboard_check(vk_shift);
should be at the start of your step event or in the begin step event.
 
T

TimothyAllen

Guest
keyboard_check functions only check the button states for that particular step.

Edit: doh ninja'd
 
L

Littlesticks

Guest
Thanks guys, wasn't aware that some functions can only be done in certain events. Great to know.
 

jo-thijs

Member
Thanks guys, wasn't aware that some functions can only be done in certain events. Great to know.
Every function can be used in every event.
However, keyboard_check(_pressed) only hecks if a key is held down / pressed once.
If you put it in the create event, it is checked only once, when the instance is created.
If you put it in the step event, the keys are constantly checked and your variables get updated.
 
L

Littlesticks

Guest
Every function can be used in every event.
However, keyboard_check(_pressed) only hecks if a key is held down / pressed once.
If you put it in the create event, it is checked only once, when the instance is created.
If you put it in the step event, the keys are constantly checked and your variables get updated.
So it's just a matter of using events. Got it!

Thanks for the help =)
 
Top