Script Movers Part 8 – Revisiting IF



Introduction
Let's take the script that we made in part 7 and convert it over to use the much easier and more efficient IF statement that we implemented in DeGeneration. I think that the IF statement makes things so much more logical. For me it speeds up my script creation by at least 50% and really makes using accums much easier.

In this tutorial we're just going to work with the "==" operator, but there are a lot of others which is where the IF statement gets its great strength.

You'll need to download the Level Designer's Kit for the files included in this tutorial.



How do we attack this
I would normally have just done it with the IF statement from the beginning, but I used the old way in the last couple tutorials for two reasons.

1. You might not be using the DeGeneration mod (if this is the case what are you thinking?!?! lol)
2. I want to show you how much easier making the script with the IF statement is as opposed to the old way.

Let's open up the script we used for the last part (script_mover_tutorial_7c.script) and take a look at it. We're going to focus on the "door_control" routine of the script. We shouldn't have to touch anything else. Here's what we have now:

door_control
{
    spawn
    {
        accum 1 set 1
        wait 200
    }

    trigger toggle
    {
        accum 1 abort_if_equal 2
        trigger door_control open
        wait 50
        trigger door_control close
    }

    trigger open
    {
        accum 1 abort_if_not_equal 1
        accum 1 set 2
        trigger switch_1 open
        trigger switch_2 open
        wait 500
        trigger door_3 open
        trigger door_4 open
        trigger door_5 open
        trigger door_6 open
        wait 7000
        trigger door_1 open
        trigger door_2 open
        wait 7000
        accum 1 set 0
    }

    trigger close
    {
        accum 1 abort_if_not_equal 0
        accum 1 set 2
        trigger switch_1 close
        trigger switch_2 close
        wait 500
        trigger door_1 close
        trigger door_2 close
        wait 7000
        trigger door_3 close
        trigger door_4 close
        trigger door_5 close
        trigger door_6 close
        wait 7000
        accum 1 set 1
    }
}

Let's look at the "trigger toggle", "trigger open", and "trigger close" functions. We're going to combine them all under the "trigger toggle" function.

• Remove all the lines inside the "trigger toggle" function so that it looks like what I have below. We're going to nest our IF statements in there since that's the function we've given control to. We're not going to have to test to find out if the door is moving since we'll now have to only test to see if the door is open or closed.


trigger toggle
{
}

• Let's look at the "trigger open" function now.

trigger open
{
    accum 1 abort_if_not_equal 1
    accum 1 set 2
    trigger switch_1 open
    trigger switch_2 open
    wait 500
    trigger door_3 open
    trigger door_4 open
    trigger door_5 open
    trigger door_6 open
    wait 7000
    trigger door_1 open
    trigger door_2 open
    wait 7000
    accum 1 set 0
}

• We're going to copy the instructions from "trigger open" to "trigger toggle" once we put our IF statement in place.
• With our brackets "{" and "}" we're going to add the line "if accum 1 == 1" which is stating that if the #1 accum is equal to 1 then do what's below it.

This is what we should have:

trigger toggle
{
    if accum 1 == 1
{

Now if accum 1 is set to 1 what do we do? We want to give it the same instructions we gave it earlier with a slight change.

• After the "{" line add everything from the "trigger open" function, but take out the line "accum 1 abort_if_not_equal 1" since we don't need that anymore.
• Before this section is over simply add the line "abort" to the end. This will make it so that one IF statement doesn't run into the next. You always want to conclude an IF statement with the "abort" command.

Below is what we should have at this point.

trigger toggle
{
    if accum 1 == 1
    {
    accum 1 set 2
    trigger switch_1 open
    trigger switch_2 open
    wait 500
    trigger door_3 open
    trigger door_4 open
    trigger door_5 open
    trigger door_6 open
    wait 7000
    trigger door_1 open
    trigger door_2 open
    wait 7000
    accum 1 set 0
    abort
}

Now let's do the other part which is the close part. This is what to do when the accum 1 is set to 0.

• Below the "}" in what we have so far let's add the next IF statement by putting "if accum 1 == 0" with a "}" under it.
• Then copy the contents of the "trigger close" function below it and remove the line "accum 1 abort_if_not_equal 0".
• Add the "abort" command to the end of what we have so it looks like what I have below.
 

if accum 1 == 0
{
    accum 1 set 2
    trigger switch_1 close
    trigger switch_2 close
    wait 500
    trigger door_1 close
    trigger door_2 close
    wait 7000
    trigger door_3 close
    trigger door_4 close
    trigger door_5 close
    trigger door_6 close
    wait 7000
    accum 1 set 1
    abort
}


Your "trigger toggle" section should look like this:
 

trigger toggle
{
    if accum 1 == 1
    {
    accum 1 set 2
    trigger switch_1 open
    trigger switch_2 open
    wait 500
    trigger door_3 open
    trigger door_4 open
    trigger door_5 open
    trigger door_6 open
    wait 7000
    trigger door_1 open
    trigger door_2 open
    wait 7000
    accum 1 set 0
    abort
    }

    if accum 1 == 0
    {
    accum 1 set 2
    trigger switch_1 close
    trigger switch_2 close
    wait 500
    trigger door_1 close
    trigger door_2 close
    wait 7000
    trigger door_3 close
    trigger door_4 close
    trigger door_5 close
    trigger door_6 close
    wait 7000
    accum 1 set 1
    abort
    }
}
 

• Now let's remove the "trigger open" and "trigger close" functions from the script since we don't need them anymore. Below is what you should have for your "door_control" section.


door_control
{
    spawn
    {
        accum 1 set 1 //set accum 1 to 1 so it's initial state is closed.
        wait 200 //wait for things to spawn.
    }

    trigger toggle
    {
        if accum 1 == 1
    {
        accum 1 set 2
        trigger switch_1 open
        trigger switch_2 open
        wait 500
        trigger door_3 open
        trigger door_4 open
        trigger door_5 open
        trigger door_6 open
        wait 7000
        trigger door_1 open
        trigger door_2 open
        wait 7000
        accum 1 set 0
        abort
    }

    if accum 1 == 0
    {
        accum 1 set 2
        trigger switch_1 close
        trigger switch_2 close
        wait 500
        trigger door_1 close
        trigger door_2 close
        wait 7000
        trigger door_3 close
        trigger door_4 close
        trigger door_5 close
        trigger door_6 close
        wait 7000
        accum 1 set 1
        abort
        }
    }
}


NOTE: At this step you're more than welcome to take a look at the map and script I've included (script_mover_tutorial_8.map and script_mover_tutorial_8.script) which will bring you to this point.

Your script is complete. Go ahead and test it out.


Summary
For me I find that the IF statement makes it a lot easier. I find that I can write a whole script without ever referencing other scripts because the syntax is easy to remember and it's just more logical.

However keep in mind that this script doesn't point out the power of the IF statement and only focuses on the simplicity of it. If you consider that the most popular programming languages have IF statements (C++) then common sense will tell you that it's valuable and powerful.

You can get very complex with the IF statement when using it to compare accum values to see if they're equal, less than, less than or equal etc. These are things that could have never been done before and if by chance they could have it would cause you a headache!

Hope these tutorials have been a help to you and if you have any questions make sure to stop by TRAM Design forums and ask away!
 

Copyright © Tram design - All Rights Reserved Legal Notices