Lua/tutorial

From Unreal Software English Wiki
Jump to: navigation, search

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: The syntax of defining a function is the following:

function MyFunction()
	print("Hello world!")
	return 0
end

myFunction()

Parameters: With the parameters it is possible to pass data that that can be used within the function.

function debugMessage(text)
	print("Lua Message: "..text)
end

debugMessage("This is a test")

Return: the return command returns a value. If the function below was called, it would return 20, as the variable myVar was doubled (see [1])

myVar = 10

function Doubler(x)
	return x+x
end

myDoubler = Doubler(myVar)

Variables

Variables can be defined either globally or locally. Lua doesn't allow static variables.

There are different types of variables (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}

String Formats

It is also possible to add a format to the string to print different things, for example

Hour = 15
Minutes = 37
TextFormat = "It's %s Hours and %s Minutes"

print(string.format(TextFormat,Hour,Minutes))

Will print to the screen: It's 15 Hours and 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 function)
	print(y)
end

-- The variable (y) is does practically not exist outside of the function, (can't be used outside the function)
printDoubler(var)


Conditions

Conditions are another important part in scripting, they use the following structure:

if (condition) then
	-- Our script
end

For example, it is possible to 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 conditions are:

x > y (when X is greater than Y) (Numbers)
x < y (when X is smaller than Y) (Numbers)
x == y (when X is the same as Y) (Numbers, strings and booleans)
x ~= y (when X is different than Y) (Numbers, strings and booleans)
x >= y (when X is the same or greater than Y) (Numbers)
x <= y (when X is the same or smaller than Y) (Numbers)

or: the "or" keyword can be used for multiple conditions of which only one has to be true:

value1 = 60
value2 = 20
value3 = 30
if (value1 - value2 == 70 or value1 + value3 == 80 or value3 + value2 == value1 + 10) then
	print("Some of our conditions worked")
end

and: the "and" keyword can be used to add multiple conditions:

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 conditions are alternative

Arrays

An array is a variable that allows you to set multiple variables inside it (by sorting). In Lua, arrays 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, however the must not contain spaces.

WeaponDamage = {}
WeaponDamage["M4A1"] = 22

Or

WeaponDamage = {}
WeaponDamage.M4A1 = 22

Or

WeaponDamage = {
	M4A1 = 22
}

Why arrays? Arrays are better and more efficient than creating an great amount of variables, they are a way to "optimize" code. So cases such as the following can be prevented:

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

The code can be optimised as such:

playerValue = {}

function setPlayer(id,value)
	playerValue[id] = value
end

Loops

while: the "while" loop uses a format like that of a condition, but repeats executing its code as long as the condition is true.

a = 0
while (a < 10) do
	a = a + 1
end

for: the "for" loop is usually used to count, the format used is the initialization, finalization, step. If the step is not specified, it's automatically switch to 1.

for i = 1, 32 do
	print(i)
end

In that case, the loop starts at 1 and ends at 32 by step 1, meaning the function print is called 32 times.

repeat-until: This loop is like the while loop, but doesn't freeze the program and only runs when the condition is false.

a = 0
repeat
	a = a + 1
until a >= 5

First script

Thisis a sample script tutorial that will show a joining player a welcome message.
The first resource to be checked is a hook that calls a function when someone joins.

addhook("join","player_join")

The function is called "player_join" here.

function player_join(id)
end

Up to now, the script does not do anything, so the function must be filled with commands (in this case a message command)

function player_join(id)
	msg2(id, string.format("Welcome to my server, %name !", player(id,"name")))
end

The full code looks like this:

addhook("join","player_join")
function player_join(id)
	msg2(id, string.format("Welcome to my server, %name !", player(id,"name")))
end

It is also possible to tell if a certaing player has joined:

-- Create the hook
addhook("join","player_join")

-- Create the function
function player_join(id)
	-- Check the USGN id of the player that has joined
	if player(id, "usgn") == 1 then

		-- The USGN id of this user is 1
		-- Throw a message warning everyone that DC is in that server
		msg("WARNING: DC has joined the server!!")

	-- stop condition
	end

-- stop function
end
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox