We have been studying the section called function LevelSetup()
, which deals with the creation of the level and all the initial conditions.
Now it's time to have a look at the function LevelLogic()
section. This section lets you make things happen during play, such as scripted events.34.
To begin with, lets make a Message Box appear after 5 seconds of game time has passed.
The messagebox could contain a greeting, and instructions for the player.Add this code:
-- Greet the player
Timer = GetGameTime() + 5
while GetGameTime() < Timer do
MessageBox("Take the asteroid to the east to win. Don't lose yours.")
Load up the level to test it out:35.
So what happened there? Lets think about this for a minute before we move on.a.
Well, first we created a variable called Timer
We set it to Game Time + 5, and since it is the first thing the game evaluates once it's running, Timer
must have a value of 0:05 seconds.b.
Then we used something called a While
loop to create a delay.
We told the game, while the game time is less than Timer, just keep repeating these commands.c.
The part which tells it the loop has ended is the coroutine.yield()
This command tells the game the loop has ended and the game should go back up and check if the conditions of the While
statement are still being met.
We don't have any commands in between our While
statement and our coroutine.yield()
. So basically the game just does nothing for 5 seconds. :>d.
Once the 5 seconds are up, the While
loop exits at the end
Then next thing that happens is that the game pauses, a Message Box appears, and when the player clicks OK, the game unpauses again.
Look at the commands and read over this part a few times if you need to. It is helpful if you can conceptualise what is going on here.36.
The level is a bit easy at the moment. Lets add a scripted event where a bunch of enemy seedlings spawn on Asteroid 1.
I'll make mine 20 seconds after the Message Box appears, and I guess I'll add about... 400 seedlings.
You might notice I've started adding comments as well, to remind myself what each part does later on.
Did you notice we used the :AddSeedlings
command in LevelSetup()
too? Most of the commands used there are also usable here. In this case, we weren't able to refer to it by a name like "a" because "a" had been replaced with a different asteroid, so instead we just tell the game which asteroid we mean with GetAsteroid(ID)
. Hence, GetAsteroid(1):AddSeedlings(400)
is the command to add 400 seedlings to asteroid 1.37.
Lets save the level and see if the the enemies spawn correctly. :>38.
If you would like to make it even more interesting, you can add the following line after:GetAsteroid(1):SendSeedlingsToTarget(2,400,GetAsteroid(0))
This sends seedlings from Asteroid 1
, belonging to player 2
... it sends 400
of them, to Asteroid 0
At the moment, if the player takes over all the asteroids, the game just continues running. There is no way to "win".
We will create a winning condition and a losing condition, so that the game can actually be won or lost by the player.Add this code:
gamewon = 0
while gamewon == 0 do
if gamewon == 1 then
MessageBox("You have won.")
if gamewon == 2 then
MessageBox("You have lost.")
There are three new things here.Quit(true)
means the game ends and the player won.Quit(false)
means the game ends and the player lost.If
statements. These check if something is true, and if so, does something.
So if the variable gamewon
is equal to 1, then the player is told they have won and the game ends in victory.
But if the variable gamewon
is equal to 2, it skips the first If
statement and proceeds to the second If
statement... resulting in the player being told they have lost, and the game ending in defeat.
You might also notice that in some places we use a single "=", and in other places we use the double: "==".
A single equals sign "=" is used to set values
, whereas the double "==" is used to compare values
Now that we have created the winning and losing code, we need to have some way to trigger the victory or defeat.
For this example, we will say that if Player 1 gets asteroid 1, the player wins (gamewon = 1). And if Player 2 gets Asteroid 0, the player loses (gamewon = 2).
The easiest way to check this is to use an entirely new function - seperate entirely from function LevelLogic
- this new one is called function OnAsteroidTaken
, and we place it below everything else
.Add this code:
function OnAsteroidTaken(id, owner)
if id == 1 and owner == 1 then
gamewon = 1
if id == 0 and owner == 2 then
gamewon = 2
is triggered every time an asteroid changes owner.
We can put If
statements in there to check the asteroid and the owner, and if appropriate, change the gamewon
variable to either 1 or 2.
Once we change the gamewon
variable to something other than 0, the While
loop that we made in our function LevelLogic()
for the victory/defeat code ends. Straight after that loop ends, the gamewon
variable is evaluated to see whether the player lost or won. Go and look again at the code in step 39
and make sure you understand how this works.
Now you should understand the mechanism of our victory/defeat detection system.
Lets give it a try:42.
Now you know enough to begin making your own levels.
I hope you will read the LUA Scripting Reference
as well and see all the crazy things it's possible to change in this game, and let your imagination run wild :>