Lua
Lua is used as the scripting language for a variety of applications, including Roblox, Sonic-Pi, Pico-8, and Norns. In other words, it is a an ideal creative coding language to learn as a software engineer using high level tools.
Loops
Today we’re going to talk about loops in Lua. A loop is a block of code which repeats until a desired condition is met. Let’s look at how a for
loop is implemented.
//Luafor i=0,5 do print(i) end// 1
// 2
// 3
// 4
// 5
for
is a reserved word in Lua which tells the compiler that either a numeric or generic loop is to follow. In this case we have numeric loop which is printing a number.
i=0
is our variable declaration. It states that the variable i
is a number (data types in Lua can be numbers, boolean, objects, or string), and that its initial value is 0
.
,5
is the setting of the condition for the loop. It creates the end condition for the loop; when i
is equal to the value 5
the loop should end.
It is important to state here that the implied numeric operation is i = i+1
; each loop i
is incremented by 1
.
do
is another reserved word in Lua; which sets the beginning of the functional scope of the loop. The block of code which is listed between do
and end
runs each time i
is incremented. In this case, we are printing the value of i
to the terminal.
So that is a breakdown of how a loop works on its own using the terminal. How do we use it in a game framework like Roblox, which features 3D objects?
Getting started in Roblox; The “Part” object
Roblox is a game framework which offers a large variety of objects in its local library. One such object, Part
, is useful in demonstrating code concepts.
In this project example we’re going to show how during runtime, we can instantiate (create an…