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

Declaring Variables #dumbquestion day 45(joke huehue)

R

RealsLife

Guest
I've written code that works but for some reason I don't need to declare a variable I used but I need to declare another one that is used for somthing similar, let's watch some code.

create event:
wacht_tijd = 0;

step event:
///menu

scr_get_input();

//Selection & arrow movement

scr_direction_timers();
if(keyboard_check_pressed(up)||(UpTime > 10))
{
wacht_tijd += 1;
if(wacht_tijd > 4){selection -= up; wacht_tijd = 0;}
}
else
{
wacht_tijd = 0;
}

timer script:
/////////////////////////////////////////////////////////////scr_direction_timers();
//Sprite work on keys pushed
if(up){UpTime += 1;}else{UpTime = 0;}
if(left){LeftTime += 1;}else{LeftTime = 0;}
if(down){DownTime += 1;}else{DownTime = 0;}
if(right){RightTime += 1;}else{RightTime = 0;}

Why do I need to declare wacht_tijd = 0; but not UpTime =0;? I first did declare it because i'm used to C++ having trash in the variables so to have clean start but i'm still a noob overal so meeehhhh...

I hope you can help me folks <3
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
You really should declare it on Create... If it's working then it probably shouldn't unless you have a line of code that sets the value of the variable elsewhere in the object/game and it's being set before it's being read... however the GMS VM is a bit more forgiving and will sometimes not pick up on things like this. If you try to run the game using the YYC instead, you'll probably get an error if the variable isn't set as it's much more sensitive to these kinds of things (and also clear the compiler cache as sometimes a stale cache can give odd issues like this too).

Basically, ALL instance variables should be declared before being accessed anywhere else...
 
R

RealsLife

Guest
You really should declare it on Create... If it's working then it probably shouldn't unless you have a line of code that sets the value of the variable elsewhere in the object/game and it's being set before it's being read... however the GMS VM is a bit more forgiving and will sometimes not pick up on things like this. If you try to run the game using the YYC instead, you'll probably get an error if the variable isn't set as it's much more sensitive to these kinds of things (and also clear the compiler cache as sometimes a stale cache can give odd issues like this too).

Basically, ALL instance variables should be declared before being accessed anywhere else...
Okay understood :) thx sir!
 
Top