Supertux2 Level Editor - Squirrel "If"

This topic was published by and viewed 511 times since "". The last page revision was "".

Viewing 1 post (of 1 total)
  • Author
    Posts
  • Jarret Buse
    Jarret Buse
    Participant
    • Topics - 25
    • @jbuse1

    The Squirrel language has a flow control statement called an If statement. The If statement is used to check a condition and perform certain blocks of statements.

    If the condition is true, then the block of statements are executed. The If statement is based on the following:

    if (condition){
        # Perform statements when condition is true
    }

    There is also an If-Else statement which allows you to run statement in the case that the condition is true and another block if the condition is false.

    The If-Else statement is based on the following:

    if (condition){
        # Perform statements when condition is true
    }else{
        # Perform statements when condition is false
    }

    So, let's look at a portion of code from the "Haunted Castle 2.stl".

    if(candle1a.get_burning()==true) {
        candle2a.set_burning(true);
        candle2b.set_burning(true);
        candle2c.set_burning(true);
    }else{
        candle1a.set_burning(true);
        candle1b.set_burning(true);
        candle1c.set_burning(true);
    }

    The condition we are testing is "candle1a.get_burning()==true". The object, "candle1a", is being queried to see if it is "burning". If the value is "true", then the first block of code is executed where all the candles in the 2nd column are turned on. If "candle1a" is not burning, then the candles in the 1st column are turned on.

    NOTE: When comparing values for being equal, use a double equal sign (==). One equal sign is used to assign a value.

    When the level starts, Tux is by column 1 which are lit when the level begins. As he walks to the 2nd column, the ScriptTrigger is activated and passed the value of "1". The above code is checked. All candles are turned off. The Switch Case equal to "1" is run. The If statement is run. If the first column is lit, then we know Tux is going toward the 2nd column. The 2nd column is now lit. If Tux returns and hist the ScriptTrigger again, it will check that the 1st column is not on and so the 1st column will be turned back on. If Tux stops inside a ScriptTrigger and turns around, the code will fail and the wrong candles will ignite. Once Tux hits another ScriptTrigger, all will be fine again.

    The code is a basic use of a Function, Switch-Case and If-Else coding. Practice with the statements a bit in SuperTux to get a better understanding of how they work.

    Further Reading

Viewing 1 post (of 1 total)