Minecraft Blogs / Article

The Lua programming language (basics)

  • 518 views, 2 today
  • 2
  • 1
RobotDoggie's Avatar RobotDoggie
Level 8 : Apprentice Miner
0
The core of making the OpenComputers mod do things for you is executing programs written in the Lua programming language As mentioned in my last blog post, once you have a computer running, you can use the 'edit' command on a file with a '.lua' suffix, and after saving with ctrl-S and exiting the editor with ctrl-W, you can run the program simply by typing the name of the program (without the .lua at the end)

The details in the rest of this blog may be somewhat technical if you are not already familiar with programming in general, please let me know in the comments if you'd like anything explained in more detail...

Lua is a weakly typed programming language with only one variable construct, and a small but complete set of flow control mechanisms. By and large, you are limited to one command per line.

The variable construct in Lua is capable of holding any type of object; scalars (strings, booleans, and numbers), tables (equivalent to arrays and dictionaries in other languages), and function pointers are all stored in the same variable definition, and in point of fact any variable can actually be used for more than one of these! Only good programming practices stand between you and absolute chaos in Lua.


A variable is defined by assigning to it the first time, thus:
myvalue = 3
mystringvalue = "hello world"

Here are some special values of variables that you will find useful, as well as how to comment your code:
mytruevalue = true
myfalsevalue = false
-- this is a comment, ignored by the interpreter
-- the nil value is a 'none of the above' property, equivalent to C and Java's null or Python's None
mypointerthingy = nil


By default, all variables created this way are 'global'. To make a variable 'local', limiting it to only the block of code in which it was created, simply preface it with 'local'.

local mylocalvariable = "only my local scope can see this"
Math operators work much the same way they do in other languages, '+', '-', '*', and '/' being plus, minus, times, and divide. String concatenation is done with the '..' operator:

local concat = "Hello" .. " " .. "World" -- produces the string "Hello World"
There is no equivalent of the C/Java ++ or -- operator:

local increment = 3
increment = increment + 1 -- increment now has the value 4

Boolean expressions are pretty normal as well, with two exceptions: Boolean 'and' and 'or' are the words 'and' and 'or', instead of symbols like '&&' and '||' found in many other languages. Also "not equals" is written '~=' Lua also has the unary boolean operator 'not'.

local isItCold = not temperature > 60 -- also could be written thus:
local isItCold = temperature < 60
local isItSoup = servingCourse ~= "nuts" -- it's not nuts, so it must be soup!

When a Lua file is loaded, all the lua commands in the file are executed from top to bottom, at which time the program exits. There are the usual set of flow control constructs:

local function addTwo(num1, num2)
return num1 + num2
end

print("Adding: " .. addTwo(3,8)) -- will print "Adding: 11"

if a < b then
print("A is less than B")
elseif a > b then
print("A is greater than B")
else
print("A equals B")
end

-- or you can do only the if part:
if iAmReady then
print("I am Ready!")
end


while iWantToKeepLooping do
-- do stuff in here, but don't even enter loop if boolean test starts false
end


repeat
-- do stuff in here, at least once
until iWantToStopLooping

-- two different kinds of for loops:
for value=1,4 do
print(value)
end
-- will print 1 2 3 4

-- this construct will print all (key,value) pairs in a table (described later)
for k,v in pairs(table) do
print(k,v)
end

-- all sorts of loops allow the 'break' command to immediately break out of the loop;
for value=1,4 do
if value == 3 then
break
end
print(value)
end
-- this will print 1 2 (loop is broken when value is 3, before value is printed)

-- some languages also allow the 'continue' keyword, which skips to the end of the current loop
-- lua doesn't exactly have this, but you can recreate it:
for value=1,4 do
if value == 3 then
goto continue
end
print(value)
::continue::
end
-- this will print 1 2 4, since if value == 3, the goto will skip past the print




I think that this is enough for the moment. I'll write up a second post on Lua with some more advanced topics:
* The 'table' construct, allowing you to make complex data structures
* variable length argument lists
* accessing the command line arguments
* breaking your code into multiple files

Again, please let me know in the comments if this format is useful as it stands, or whether you'd like more details
Tags

Create an account or sign in to comment.

Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome