Lua Tutorial

From GTA Connected
Jump to navigation Jump to search

This page shows some of the functionality in Lua. Be sure to reference the official Lua manual when you can.

Comments

-- this is a single line comment, which can appear at the end of any line


--[[
this is a multi line comment,
which can appear anywhere
]]

Functions

someFunction() -- this is a call to a function named someFunction, the code for that function will run when the function is called


someFunction(5, 400, 5000) -- this is a call to a function, sending 3 parameters to it. a parameter is an input value for a function


-- this is a function definition, which is a portion of code, don't forget the word end to end the function definition
function someFunction()
end


-- this is a function definition, which takes 3 parameters. parameters are also called arguments
function someFunction(arg1, arg2, arg3)
end


-- this is a function without a name, also known as a lambda function
function() end


-- this is a function which adds 2 numbers
-- the word return is used to set the output of the function, as well as to stop the function running at the expression after the word return
-- the result of add(..) is sent straight to the print function, print is a function provided by Lua
function add(a, b)
    return a + b
end
print(add(40, 20))

Data Types

string

'this is a string'
"this is a string"
[[this is a string]]


number

20
40.0
99e5
-20
-40.0


boolean

true
false


nil

nil

someVariable = nil -- delete a variable

if someVariable == nil then -- comparison with nil
end


table

{} -- an empty table

{20, 50, 80} -- a table with 3 elements, keys are 1 2 and 3, values are 20 50 and 80, respectively.

{aa=5, bb=10} -- a table with 2 elements, element with key aa has value 5, and element with key bb has value 10

{[5]='aa', [10]='bb'} -- a table with 2 elements, element with key 5 has value 'aa', and element with key 10 has value 'bb'


function

function() end -- an empty function with no name

function someFuncName() -- an empty function with a name
end

someFuncName = function() -- an empty function with a name
end


There are also data types userdata and thread.

Operators

Math Operators

+ - * / % -- these are mathematical operators

5 * 4 -- results in 20

5 * 4 + 2 -- results in 22

5 * (4 + 2) -- results in 30

Variables

local someVariableName = 'some string value' -- this creates a local variable and assigns it a string value

someGlobalVariable = 50 -- this either creates a global variable, or updates an existing variable, and assigns it a number value

_G.someGlobalVariable = 50 -- this is the same as the global variable above, however we are referencing the global namespace using _G, so the variable will definitely be global

Conditions

local someVariable = 500

-- if
if someVariable < 1000 then
    print('value is less than 1000')
end

-- if and else
if someVariable < 200 then
    print('value is less than 200')
else
    print('value is 200 or more')
end

-- if, elseif and else
if someVariable < 100 then
    print('value is less than 100')
elseif someVariable < 300 then
    print('value is less than 300')
elseif someVariable < 500 then
    print('value is less than 500')
else
    print('value is 500 or more')
end

Loops

-- integer loop
for i=1, 20 do
    print(i)
end

-- table loop
for i,v in ipairs({20,50,80}) do
    print(v)
end