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

values and changing them.

M

Marko Kajtazi

Guest
Hey everyone,
I am still new with game maker can you help me understand how the variables work?. For example when I change a value in an if statement, the stays changed even if the statement if over and false.
Example
Code:
if(hp == 1) {
    jumpspeed =    8;
    movespeed = 30;
    acc = 2;
    sprite_index = sPlayer_1health;
}
if ''hp'' isn't 1 than I want the variables to go back to the previous value.
 

Simon Gust

Member
Hey everyone,
I am still new with game maker can you help me understand how the variables work?. For example when I change a value in an if statement, the stays changed even if the statement if over and false.
Example
Code:
if(hp == 1) {
    jumpspeed =    8;
    movespeed = 30;
    acc = 2;
    sprite_index = sPlayer_1health;
}
if ''hp'' isn't 1 than I want the variables to go back to the previous value.
Variables are "dumb", they don't remember what they were at some point, they only know what they are right now.
So if you change player attributes like you did, either you have to tell them to what they change or make another variable that tells what jumpspeed, movespeed etc. normally is.

Create event
Code:
my_jumpspeed = 10;
jumpspeed = my_jumpspeed; // jumpspeed will also now be 10
my_movespeed = 50;
movespeed = my_movespeed; // movespeed will also now be 50
my_acc = 5;
acc = my_acc; // acc will also now be 5
Step event
Code:
if(hp == 1) {
   jumpspeed =    8;
   movespeed = 30;
   acc = 2;
   sprite_index = sPlayer_1health;
}
else // if hp is not 1, run this instead
{
   jumpspeed = my_jumpspeed;
   movespeed = my_movespeed;
   acc = my_acc;
   sprite_index = sPlayer_not1_health;
}
or what you also can do is this
Code:
jumpspeed = my_jumpspeed;
movespeed = my_movespeed;
acc = my_acc;
sprite_index = sPlayer_not1_health;
if(hp == 1) {
   jumpspeed =    8;
   movespeed = 30;
   acc = 2;
   sprite_index = sPlayer_1health;
}
Due to the sequence the code will run, at the end of these lines, if hp is 1, jumpspeed will be 8, movespeed will be 30.
If hp is not 1, the if statement wont run and jumpspeed will still be my_jumpspeed (10), movespeed still my_movespeed (50) and so on.
 
S

Sabrina Stoakes

Guest
I'm not sure if I understand your question entirely, but a better way to do a statement like this would be to use an ELSE statement! :D

Example:

Code:
if hp = 1
{
///What you want it to do if hp is 1
}
else
{
///What you want it to do if hp is NOT 1
}
Lemme know if this is what you're looking for or not.
 
Top