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!
Computer programs perform computations just like a calculator, but also allow you to keep track of meaningful values that change. These labeled changing values are called variables.
A variable is a label paired with some data. Making one allows you to write code using that value throughout your program.
To create a variable, we use the special keyword
local, give the variable a name, and then use
= to assign it to a value.
local myCoolVariable = 13 local variableLabel = "variable data"
Variable names cannot have spaces, so in the above examples, we used capitalization to visual separate new words. This style of word separation is called CamelCase.
Besides spaces, you cannot have other punctuation (besides
_ which you would use if you want to use snake_case instead), and can’t use be of lua’s special keywords (like
local, for example).
Most of the time, variables will start with a lower-case letter, unless they are never supposed to change. Then people generally use ALLCAPITALLETTERS to name them.
Now that you know how to name your variables, you can start thinking about the kinds of information that they will label. There are three types of data that we will focus on in this lesson, numbers, strings, and Booleans.
Numbers are any of the numerical values you may want to save, from negative to positive to tiny decimal values to massive ones.
Lua recognizes numbers automatically, and they do not need to be surrounded by any special punctuation.
local aSmallNumber = 3 local aBiggerNumber = 9999999 local aTinyDecimal = 0.0000001 local aNegativeNumber = -700
Decimal numbers less than 1 will need to start with a
Many programming languages treat integer numbers (whole numbers without any decimals) as entirely different from floating point decimal numbers (any number with a decimal), and won’t let you just add or multiple them. Usually they are called ints and floats.
Lua does see these as distinct types, but will do the work of converting between them for you.
Strings are the data type that you use to save any word, sentence, or really anything that doesn’t fit in another category.
To make a string, surround the text in
local aString = "Hello, world!" local someonesName = "Elijah" local errorMessage = "Error: My program doesn't know what to do here"
Some characters are more difficult to add to strings than others. The
" character is a good example, because as soon as you use one, you end the string.
You can use the
\ to add those characters back in:
\"will let you write “
\\will let you write \
\nis a new line character, what you get when you press Enter.
Booleans are a data type that can only ever be either
false. They are very useful for scenarios where you need to flip between two states, like a light switch.
We will discuss Booleans in more depth in the next lesson when we talk about Conditionals.
Nil is the value you assign to variables that haven’t been assigned real values yet. It is a placeholder and often shows up in errors when a variable hasn’t been defined, or is misspelled, but you can also use it intentionally to only give something a value if something else happens.
local winner = nil -- We don't know who won yet, so we don't give the variable a value.
You can do any operation you could do with a calculator in code with Lua, but some use different characters than you might expect so that they can be easily typed.
Another useful operation is
% called modulus. This give you the remainder when you divide. You can use it to check if a number is divisible by another.
-- checking if a number is odd or even local myNumber = 13 print (myNumber%2) -- would be 0 if the number were even (divisible by 2), but it was 1 because 13 is odd.
Lua has a special set of functions (we will discuss these more later) that you can use for more complex math.
A few examples
math.piis the value of pi, 3.14…
math.floor(yourNumberHere)will round your number down. This is very useful when you just need to chop the decimal off of a number.
Core has a couple of math operations that were added to make your life easier.
CoreMath.Round(yourNumberHere)will round your number.
Strings don’t have as many operations as numbers, but can still be manipulated and changed in many different ways.
You can use
.. to combine two strings into one, called concatenation.
local newString = "Now is the time. " .. "You have the power"
Core also adds a few string operations, which you can read about on Core documentation.
You can change a string of numbers into a number data value, and a number into a string:
local someNumbers = "123" local numberVersion = tonumber(someNumbers) -- will be the number 123 local aNumber = 5678 local stringVersion = tostring(aNumber) -- will be "5678"