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

test global variable before testing another[SOLVED]

♛W♛

Guest
Is it possible to test if a variable = value then test if another variable = value before doing an action? if so could someone show how it would be done?
Basically, what i'm saying is i want to check if a global variable = something and if it does check if another global variable = something before an action is performed.
 
Last edited by a moderator:

Jabbers

Member
Hello! This is a key concept in programming, I think you are describing the if statement.

The basic format is that:

Code:
if (expression is true)
{
     //Code executes here
}
For example, if you wanted to test a variable called global.money and you wanted to remove $5 if the person has more than $5 you could write something like this.

Code:
if (global.money >= 5)
{
     global.money -= 5;
}
else
{
     show_message("You are broke. Sorry.");
}
That symbol >= is a comparison, and it means "more than or equal to". If you simply wanted to see if it was equal to, you would use the symbols ==. See here for a list of expressions. You can also see I added "else" at the end, which is the code that runs when the expression in the if statement is untrue. In this case, the code is saying "if your money is more than 5, remove 5 from money, otherwise show a message saying they are broke".

If you want to test two variables at once, like money and food, you could do this:

Code:
if (global.money == 5 && global.food = 0)
{
     //Code
}
This uses && to check that both are true. If money is not 5, or is food is not 0, then it will not run. This is covered under "Combining" in the manual.

Understanding statements and expressions is basically a crucial minimum when you learn programming. Now is a great time to start reading up on it!
 
L

Lars Karlsson

Guest
To me it sounds like you want something like:

Code:
if(global.value1 == value1 && global.value2 == value2)
{
    // Do something if both are true
}
or, maybe:
Code:
if (global.value1 == value1)
{
    // Do something if first statement is true

    if (global.value2 == value2)
    {
        // Also do this if second is true
    }
}
 
Top