Jump to content

Map Editor Tutorial - Using Ifs and Loops


R4bitF00t

Recommended Posts

logo.png.2daaf617449a0c9e1c637ec4ce05d03a.png

Hello once again!

We are near the end of advanced scripting principles.
Ifs and for loops are the last step between you and an official Skylords Reborn Advanced Scripting Degree! (jk)

Let's look at them now, shall we?

I will create two script files for this chapter, you can download them here.
Don't forget to define the example.lua script in _scriptlist.lua!

 

_globalvars.luaexample.lua

 

If you encounter any issues, don't hesitate to ask in Skylords Reborn Map Making Discord.

 

 Content

splitter.png.b88958376f2b49606f513de34d9a3733.png

  • If, Elseif, Else
  • For Loops
  • Using Ifs in BF Scripts
  • Using Loops in BF Scripts
  • Next Chapter

 

 If, Elseif, Else

splitter.png.b88958376f2b49606f513de34d9a3733.png

First, I will use if, elseif and else conditions in a _globalvars.lua script.
There, we can most easily see how to work with these conditions.

image.png

In Lua, using the if condition is very simple.
There are three basic keywords you need to know:

  • if - starts the if condition block, is followed by the actual condition
  • then - defines the actions that should happen if the condition is true
  • end  - closes the if condition block

Let's look at the _globalvars.lua example above.

I've declared a difficulty variable and used the GetDifficulty() function to get the difficulty we are playing the map on.
The GetDifficulty() function returns the numbers 1, 2 or 3 based on the difficulty.

  • 1 - standard difficulty
  • 2 - advanced difficulty
  • 3 - expert difficulty
     
Quote

Note: The GetDifficulty() function can be found in the Notepad Snippets under actions.

image.png

 

Then I use the if condition to check if the difficulty is equal to one of these numbers.
To compare values, we use the following characters.

  • == - checks if the value on the left is equal to the value on the right, be sure to use double =
  • > - checks if the value on the left is bigger than the value on the right
  • < - checks if the value on the left is smaller than the value on the right
  • >= - checks if the value on the left is larger or equal to the value on the right
  • <= - checks if the value on the left is smaller or equal to the value on the right

As you can see, I've used the else keywords in the code.
If you want to check another condition for the same if condition, use the following keywords.

  • elseif - starts another if condition block, if the previous if conditions return false, is followed by the new condition to check
    needs the then keyword after the condition
  • else - defines the actions that will happen if ALL the above ifs and elseifs return false, does not need then keyword

In my example - if the difficulty is not standard, I use the ELSEIF condition to check if the difficulty is advanced.
If the difficulty is not standard or advanced, then it must be expert, so I use the ELSE keyword - no further condition needed, as the ELSE actions get executed if all the other conditions fail.

If you don't want to deal with the ELSE and ELSEIF keywords, you can separate the check into three separate IF conditions.
Keep in mind that you then need to
end every if condition with the end keyword.

image.png

 

 For Loops

splitter.png.b88958376f2b49606f513de34d9a3733.png

I am going to show you how to declare for loops outside of BF scripts at first. 

For loops are used to execute code multiple times, until you reach the loop count you specify.

If you were to use for loops in general Lua scripts, you would go about it like this.

image.png

So these are the keywords you need to remember.

  • for - declares the for loop, is followed by definition how many times the loop should run
  • do - defines the actions that should happen for every run of the loop
  • end - ends the for loop

After the for, we need to declare a variable for that loop, that we will increase by one every time the loop finishes, and the range the variable should start and end at. Like this.

i = 1,2

This tells the loop to define the i variable that starts at the value of 1, and the loop will stop once the i reaches a value of 2.
So the loop will run two times.

  • first time when the loop starts at i = 1
  • second time when i = 2
  • the loop will not start a third time, as the i has reached the specified value of 2

This for loop would do absolutely nothing, but if we were to add an equation into the for loop...

image.png

With this loop, it would increase our difficulty variable by 1 every time the loop runs. So in the end, if we started the map on standard difficulty, the difficulty variable would return 3 - as if we were playing expert, and it would return 5 if we were actually playing on expert.
(GetDifficulty() returns 1 on standard difficulty and 3 on expert difficulty, then we add 1 for every loop -> 1+1+1 = 3 or 3+1+1 = 5 )

For now, let's just declare a variable number_of_spawners in _globalvars.lua.

image.png

 

 

 Using Ifs in BF Scripts

splitter.png.b88958376f2b49606f513de34d9a3733.png

To show you how to use ifs and loops in BF scripts, I am going to create an example.lua script.

I want the game to give me an outcry that the script was successfully loaded, so I add a state (remember, every script needs at least one state) with a OnOneTimeEvent that sends out an outcry.

image.png

Now if we define the example.lua into _scriptlist.lua, upon map start, we should get the outcry "EXAMPLE LOADED".

 

To actually use the ifs and for loops in the script, we will need to define them OUTSIDE OF ANY STATE.
The events we declare in ifs and for loops will be added to the next state after them.

I've added an if difficulty == 3 condition above my INIT state.

image.png

This condition will use the difficulty variable I've declared in _globalvars.lua and check if we are playing on expert difficulty.
If the condition is true, it will add the declared OnOneTimeEvent to the INIT state below the if condition.

Quote

Note: There is a way to check the mission difficulty via BF script conditions. But for the sake of this tutorial, I've opted to check the difficulty this way.

You probably didn't notice this, but I've added something special to the MissionOutcry. A "..difficulty" after the outcry text.

image (63).jpg

Believe it or not, but we can actually use the variables in BF actions and conditions. The double dot (..) tells the script to add whatever comes after the double dot to the text before it.
In our case, it will add the difficulty variable to the outcry text, like this.

image.png

Awesome!

Also notice that the event will switch the script to the EXPERT state via the GotoState. Note that, it is important in the following topic.

 

 

 Using Loops in BF Scripts

splitter.png.b88958376f2b49606f513de34d9a3733.png

I've prepared a little scenario to showcase the usage of for loops.

I have two fire spawners, one tagged spawn_point1 and the other spawn_point2.

New Project (3).jpg

I want to spawn a squad next to each of them on expert difficulty.
To do that, I will
add the EXPERT state, to which the if condition from before switches to.

And above the EXPERT state, I will define my for loop.

image.png

I have two spawners, and I want to spawn one squad next to each of them. So I will declare the i = 1,2.
I use the
SquadSpawn action to spawn the squad and in the TargetTag, I use the good old double dot (..) to spawn the squad next to each spawner.

image.png

The SquadSpawn action takes the "spawn_point" tag we specified and the for loop adds the number of the i after it.

Quote

Important Note: Your events need to end with the semicolon ; after the closing bracket to be used in ifs and for loops.

OnOneTimeEvent
  {
 EventName = "NOTICE THE SEMICOLON AFTER CLOSING BRACKET",
     Conditions =
       {
       },
       Actions =
        {
        },
   };

 

So what this for loop does is, it adds two OnOneTimeEvents with the SquadSpawn action to the EXPERT state. And the TargetTag for the first event is spawn_point1 and for the second event it is spawn_point2.

 

Now, the number of spawners I have on the map can change as I work further on the map, for easy access to the number of spawners, I have declared the number_of_spawners variable in _globalvars.lua.
We can use that variable in our for loop, now we won't need to sift through all the scripts to adjust the number of spawners we have, we can only change the variable in _globalvars.lua.

image.png

 

Let's check our script in game.
When we start the map on standard or advanced difficulty, the squads will not spawn.

image.png

The EXAMPLE LOADED outcry tells us that the script works as it should, we just didn't select the expert difficulty.


But when we start the map on expert (remember - we switch to the EXPERT state in our event in the first if condition), we will get a squad of Sunstriders next to each spawner, alongside the DIFFICULTY 3 outcry.

New Project (6).jpg

YAY!

Before we leave thic topic behind us, I just want to reiterate that the contents of the ifs and for loops get added to the next state that comes after them.

New Project (8).jpg

 

 Next Chapter

splitter.png.b88958376f2b49606f513de34d9a3733.png

I know this chapter was a bit... ify.
But you successfully got through the advanced scripting principles. Don't worry, a bit of practice and some help from your fellow map making skylords will carry you a long way.

In the next chapter, we will look at the very complex topic of Warfare Patterns. Using them can be really great and easy if you know what you're doing, but they include some weird behaviours that might confuse you.

Don't dread, I will be there to guide you on your Warfare Pattern jouney to victory.

 

 Warfare Patterns (Coming Soon)

 

Middle_splitter.png.a770f452e94ed383c001e9d7af974010.png

 

Edited by R4bitF00t
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Terms of Use