Supertux2 Level Editor - Squirrel Switch-Case

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

Viewing 1 post (of 1 total)
  • Author
    Posts

  • Jarret Buse
    Participant
    • Topics - 25
    • @jbuse1

    The Squirrel language has a flow control statement called a Switch-Case or Switch. The Switch is used to check the value of a variable against a list of possibilities and perform certain blocks of statements for a given value.

    If you read the previous article on Functions, then the following code would be familiar to you:

    function lighting(x){
        candle1a.set_burning(false);
        candle1b.set_burning(false);
        candle1c.set_burning(false);
        candle2a.set_burning(false);
        candle2b.set_burning(false);
        candle2c.set_burning(false);
        switch(x){
            case 1:
                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);
                }
                break;
        }
    }

    The Function is called and passed a value which is placed in the variable "x". This is given by the first line of the Function "function lighting(x){". Multiple values can be passed to the function if needed. The function is called "lighting" and a value is placed into the variable "x".

    Several statements are executed every time the Function is called. The eighth line "switch(x){" shows the beginning of a Switch flow control statement. The value being tested is stored in the variable "x".

    The following line "case 1:" tests the value of "x" to see if it is equal to "1". If the value is "1", then the block of code is executed until a "break;" statement is found. Once a "break;" statement is found, the Switch flow control statements are skipped. If the value of "x" is not equal to "1" then the next "case" statement is found and tested again.

    NOTE: If a "break;" statement is not placed at the end of the block, then all statements will be executed. If the value of "x" is "1", then the statements for "case 2:" could be executed as well without the "break;" statement.

    Once inside the Switch statement, the block of code can be as long or short as needed. Just remember the "break;" statement though. There can be numerous "case #:" statements for as many numbers as needed. Since the script is being imported from a file, the script appears once instead of being in each ScriptTrigger.

    The ScriptTrigger imports the script and then calls the Function with the number of the Candle column. For example, the script could be:

    import("levels/haunted/default.nut");
    lighting(1);

    Here, the first line imports the script from a specific folder and file. The folders and file must exist and are case-sensitive.

    The second line calls the function with the value of "1". The script then turns off all the named Candles. The Switch-Case is checked by testing the value of "x". In this case, "x" is equal to "1" so the block within "case 1:" is executed. The code being run is as follows:

    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);
        }
        break;

    Here, the block from the "if" statement to the "break" statement is being executed.

    Look over the next article to cover the "if" control flow statement.

    Further Reading

Viewing 1 post (of 1 total)