This page will introduce you to Lua 5.2
Some links that may be interesting for you:
This Guide is very basic and WIP
Lets start on how to define a type:
local nameHere = valueHere
Local variables are "local" (only available to the programming level they were defined)
Valid Levels:
e.g.
local localToFile = valueHere
function thisIsAFunction()
local localToFunction = valueHere
if conditionHere then
local localToStatement = valueHere
end
end
This example also shows you how to define functions. Open up with function close the function with end. We will return to this later.
Globals are variables which are accessible everywhere, they are defined without the local statement:
THIS_IS_GLOBAL = valueHere
-- Other file:
print(THIS_IS_GLOBAL)
-- Console Output: value
Valid Types
We can define those types as shown before:
local number = 1
local string = "string"
local table = {}
local boolean = true
local thisIsNil
-- an empty variable will always default to nil
Tables are the core of Lua. So you should definitely know how to handle them.
Tables look like this (don´t forget the comma before every new entry):
local table = {
[Key] = [Value],
[Key2] = [Value2]
...
}
The key is representative of the value. So if you want to access/modify a value, you will have to access the key of a table.
Keys are defined either as a "list" in rising numbers or as a string.
local list = {
[1] = firstValue,
[2] = secondValue,
...
}
-- or
local list2 = {firstValue, secondValue, ...)
-- The numerical keys will get assigned automatically in this case
local stringKeyTable = {
["StringKey1"] = myValue,
["StringKey2"] = myValue2,
...
}
-- or
local stringKeyTable2 = {}
stringKeyTable.stringKey1 = "Hello World"
stringKeyTable.stringKey2 = false
print(stringKeyTable.stringKey1) -- output: Hello World
Another way of adding or removing values from a table is by using the lua native table function:
-- Automatic Key: table.insert(affectedTable, value)
-- or Manual Key: table.insert(affectedTable, key, value1)
local myTable = {}
table.insert(myTable, 15)
table.insert(myTable, 1, "AString")
-- table.concat will read out the table in correct order (if list) with the provided seperator:
-- table.concat(table, seperator)
print(table.concat(myTable, ", "))
-- Console Output: AString, 15
Depending on they key type, tables can be accessed in a couple of ways:
Let´s modify this list by accessing the table via the key:
(Use -- for comments)
local list = {
[1] = true,
[2] = "IamAString",
}
list[keyYouWantToAccess] = setNewValueHere
-- list[2] = "NewValue"
print(list[2])
-- print() will print out the value provided to the console
-- Console Output: NewValue
The same will work with string keys:
local stringKeyTable = {
["StringKey1"] = 2,
["StringKey2"] = 10,
}
stringKeyTable["StringKey2"] = "NewValue"
-- or
stringKeyTable.StringKey2 = "NewValue"
print(stringKeyTable["StringKey2"])
-- Console Output: NewValue
Another way of accessing a table is via a for-loop. A for-loop will iterate through every available value in a table or a given numeric range.
local list = {
[1] = true,
[2] = "IamAString",
}
local stringKeyTable = {
["StringKey1"] = 2,
["StringKey2"] = 10,
}
-- use ipairs if using a "list" or order matters
-- use pairs if keys are not order in a list
-- use tostring() if you want to print out non-string values
for Index, Value in ipairs(list) do
print(tostring(Value))
end
-- Console Output 1: true
-- Console Output 2: IamAString
-- print will also automatically apply tostring() if there is only one type provided
for Key, Value in pairs(stringKeyTable) do
print(Value)
end
-- Console Output 1: 2
-- Console Output 2: 10
Functions are programmable processes. You can define a function like this:
function myFunction(Parameter1, Parameter2, ...)
-- do something here
end
Any logic inside the function will be executed if called.
Let´s define a small math function:
function sum(a, b)
-- return will return a set value to the call function
return a + b
end
-- lets call this function
print(sum(5, 9))
-- Console Output: 14
Another example:
function isString(a)
local valueIsString = type(a) == "string"
-- Short explaination: type() will check the type of the provided value and return it as a string (this works with any other type)
-- == serves as an if-statement here (if is .. then true, else false)
-- written out this code would be: "If 'a' is "string" then return true"
return valueIsString
end
print(isString(2))
-- Console Output: false
print(isString("string"))
-- Console Output: true
To perform some tasks we need operators. Available operators are:
<
>
<=
>=
==
~=
and
or
not
Practical Usecase:
local value1 = 1
local value2 = 2
local value3 = 2
function isLargerOrEqual(a, b)
if a > b or a == b then -- can also be shortened with a >= b
return true
else
return false
end
end
print(tostring(isLargerOrEqual(value1, value2)))
-- Console Output: false
print(tostring(isLargerOrEqual(value2, value1)))
-- Console Output: true
print(tostring(isLargerOrEqual(value2, value3)))
-- Console Output: true
You can also define multiple if-statements by using elseif:
-- you can concatenate strings by using: ..
function isLargerOrEqual(a, b)
local ReturnString = "Value A "
if a > b then
return ReturnString .. "is Larger"
elseif a == b then
return ReturnString .. "is Equal"
else
return ReturnString .. "is Smaller"
end
end
print(isLargerOrEqual(2, 3))
-- Console Output: Value A is Larger
print(isLargerOrEqual(2, 2))
-- Console Output: Value A is Equal
print(isLargerOrEqual(3, 2))
-- Console Output: Value A is Smaller
You can use Lua Demo to try it out for now. But i advise you to write your code in an external text editor and only run it on the demo site so your code doesn´t get deleted by mistake.
Create a Table:
Create a function:
Print out the prices for:
Create a function: