Lua/tutorial
This page is aimed to describe the main aspects of the Lua programming language.
Contents |
Essential elements
Functions
A function is basically a chunk of code which does something and can be told to do this thing from somewhere else. It can also pass data about what it did back to the place which told it to run (the place which "called" it).
print("Hello world!")
Defining: You are also able to write and call your own functions
function MyFunction()
print("Hello world!")
return 0
end
myFunction()
Parameters: With the parameters you can pass useful data that you'll use in this the next process
function debugMessage(text)
print("Lua Message: "..text)
end
debugMessage("This is a test")
Return: return functionality in functions, are used to return back some data produced by calling this function. Please, take a look at variables [1]
myVar = 10 function Doubler(x) return x+x end myDoubler = Doubler(myVar)
So the function Doubler will return a variable multiplied by 2
Variables
The variables can be defined normally either globally or locally. Lua doesn't allow static variables.
There are a few types of variable (string, number, boolean, table, userdata)
| Type | Example |
|---|---|
| String | myVariable = "This is a test string" |
| Number | myVariable = 12345 |
| Boolean | myVariable = true |
| Table |
myVariable = {"foo","bar",100}
|
Format in strings
You can also add format to the string to print different things, for example
Hour = 15 Minutes = 37 TextFormat = "It's %s Hours with %s Minutes" print(string.format(TextFormat,Hour,Minutes))
Will produce: It's 15 Hours with 37 Minutes
Local Variables
Usually, local variables are used in functions
var = 4 function printDoubler(x) local y = x+x -- Our variable (y) will be local. (Means that it can only be used inside this chunk) print(y) end -- The variable (y) is null outside the function, (can't be used outside the function) printDoubler(var)
Conditional
Conditionals are another important part at scripting, they use the next structure
if (condition) then -- Our script end
For example, we can check the result of substracting 2 values
value1 = 70
value2 = 20
if (value1 - value2 == 50) then
print("Our result is 50")
end
Other methods for conditionals are:
x > y (when X is higher than Y) (Numbers) x < y (when X is lower than Y) (Numbers) x == y (when X is the same of Y) (Numbers and strings) x ~= y (when X is different than Y) (Numbers and strings) x >= y (when X is the same or higher than Y) (Numbers) x <= y (when X is the same or lower than Y) (Numbers)
or: the "or" keyword can be used also when another condition happens in the same conditional, so the condition must work.
value1 = 60
value2 = 20
value3 = 30
if (value1 - value2 == 70 or value1 + value3 == 80 or value3 + value2 == value1 + 10) then
print("Some of our condition worked")
end
and: the "and" keyword can be used to force to check when 2 conditions or more, worked in a conditional
value1 = 60
value2 = 20
value3 = 30
if (value2 + value3 == 50 and value1 - value3 == 25) or (value1 + value3 == 90 and value2 - value3 == -10) then
print("Some of our conditionals worked")
end
Note: Parenthesis in conditionals are alternative
Table
A table is a variable that allows you to set multiple variables inside it (by sorting). In Lua, the tables don't need a specific size.
myArray = {}
You can store variables by using a specific id (slot) in it
myArray[slot] = "This is a test string inside of an array"
For example
myArray = {}
myArray[1] = "Hello"
myArray[2] = "World!"
That would be the same as if it was done like this:
myArray = {"Hello","World!"}
String Slots: String slots are also allowed to be used, but you may not use spaces in it.
WeaponDamage = {}
WeaponDamage["M4A1"] = 22
Or
WeaponDamage = {}
WeaponDamage.M4A1 = 22
Or
WeaponDamage = {
M4A1 = 22
}
Why tables? Tables are better and more efficient than creating an amount of variables, is a way to "optimize" our code. So we can prevent cases like this:
player1 = 0 player2 = 0 player3 = 0 function setPlayer(id,value) if id == 1 then player1 = value end if id == 2 then player2 = value end if id == 3 then player3 = value end end
You can optimize this code by doing this:
playerValue = {}
function setPlayer(id,value)
playerValue[id] = value
end
Loops
while: the loop "while" uses a format like of conditional, but this one repeats while the condition works
a = 0 while (a < 10) do a = a + 1 end
for: the loop "for" is usually used to count, the format used is the initialization, finalization, step. If step is not specified, it's automatically switch to 1.
for i = 1, 32 do print(i) end
In that case, we start from 1 and stop at 32 by step 1, means we called the function "print" 32 times
repeat-until: This loop is like the loop while, but it doesn't freeze the program and works only when the condition doesn't work
a = 0 repeat a = a + 1 until a >= 5
First script
So we seem to be ready, now we can start to create our first script, let's create a basic welcome message for Counter-Strike 2D
We have to check our first resource, a hook that calls a function when someone joins
addhook("join","player_join")
So we will call our function "player_join"
function player_join(id) end
But it will do nothing if we don't put the welcome message
function player_join(id)
msg2(id, string.format("Welcome to my server, %name !", player(id,"name")))
end
So, our full code would look like this
addhook("join","player_join")
function player_join(id)
msg2(id, string.format("Welcome to my server, %name !", player(id,"name")))
end
With Lua we can also create a detector that tells when someone important joins, for example if DC joins to your server
-- Create the hook
addhook("join","player_join")
-- Create the function
function player_join(id)
-- Check the USGN id of this player
if player(id, "usgn") == 1 then
-- The USGN id of this user is 1, means that is real DC
-- Throw a message warning everyone that DC is in that server
msg("WARNING: DC has joined to the server!!")
-- stop our condition
end
-- stop our function
end