A complete introduction to scripting for games using the versatile Lua programming language and the Core game development platform. Add an array of game coding skills to your toolbox and take your game development to new heights!
So far, you have done computations data, writing code that includes everything you want to do in order, executed as soon as you run the program. In this lesson you will learn how to write conditional code, that only executes when a condition you define is true.
We will be using Booleans for this, as well as other operations that evaluate to
One way to get a result of true or false is to test if two values are equal. You do this with
local a = 5 local b = 7 a == b -- the result is false
You may have noticed that we already used
= to create variables. Although they use the same punctuation, assigning a value to a variable and comparing a value are very different operations.
=is a command: Make this equal to this.
==is a question: Is this equal to this?
local aTruth = true -- assigns the value TRUE to a variable local aFalsehood = false -- assigns the value FALSE to a variable print(aTruth == aFalsehood) -- evaluates whether these variables have the same value. In this case it evaluates to FALSE. aTruth = aFalsehood -- reassigns the variable. Now aTruth is FALSE! print(aTruth == aFalsehood) -- this will be true! Because both aTruth and aFalsehood are assigned to the value FALSE.
You can also use
== to compare strings and numbers.
Strings will need to match precisely, including having the exact same capitalization. Numbers will never match strings.
local a = "a String" local b = "a string" print(a == b) -- prints false local stringNumber = "7" local actualNumber = 7 print(stringNumber == actualNumber) -- prints false
You can use the
~= operator to evaluate whether two values are NOT equivalent.
local aTruth = true local aFalsehood = false print(aTruth ~= aFalsehood) -- will print 'true'
Inequalities allow you to check if a numerical value is bigger or smaller than another. This can be useful when you are checking for a minimum number of things.
The greater than operator
> checks if the value on the left is greater, and the less than operator
< checks if the left value is less than the right one.
print(10 > 4) -- prints 'true', because 10 is greater. print(1 < -3) -- prints 'false' because 1 is not less than -3
You can also check if a value is greater than or equal to another value or less than or equal to one. This is great for scenarios where you need “at least” or “at most” of something.
local playersConnected = 3 print(playersConnected >= 3) -- prints 'true'. We want to know if there are AT LEAST 3 players connected. print(playersConnected <= 16) -- prints 'true'. Could use this to make sure that we haven't exceeded a maximum number of players
Often you will need to check more than one logical condition before running some code. Logical operators will allow you to do this.
Use the keyword
and to check if two logical statements are BOTH true.
This can be great to check if a number is between two other values.
local aTruth = true local aFalsehood = false print(aTruth and aFalsehood) -- prints false local a = 10 print (a > 2 and a < 20) -- prints true, because a is between 2 and 20
Use the keyword
or when at least one of the parts must be true.
If both side of an
or are true, it will also be true.
local iLikeCoffee = true local iLikeTea = false print(iLikeCoffee or iLikeTea) -- true, if you like coffee, tea, or both
You can use the keyword
not to get the opposite of the truth value.
local iLikeCoffee = true local iDontLikeCoffee = not iLikeCoffee print(iDontLikeCoffee) -- false, because I DO like coffee
If statements will allow you to specify what code to run when a condition is true, and when it isn’t.
Use the keyword
if with your condition, followed by the keyword
then. The conditional code will be indented, and finished with the keyword
if true then print("This code will always run") end local a = 9 if a < 10 then print("Less than 10!") end
You add an
else keyword before the
end of an if-statement to add code that only executes when the if condition is false.
if false then print("This will never run") else print("This will instead") end if 10 < 100 then print("This part does run") else print("So this part does not") end
else if keyword before
else lets you evaluate a second condition if the first if condition is false.
You can have as many else-ifs as you want. They will be checked in order, so the first one that is true is the one that runs, and no other part of the condition will run.
local iLikeCoffee = false local iLikeTea = true local iLikeJuice = true if iLikeCoffee then print("Have some coffee!") elseif iLikeTea then print("Have some tea!") -- This line will run, because it has the first condition that evaluates to TRUE elseif iLikeJuice then print("Have some juice!") else print("Try some water?") end