Basic Lua

From ArcEmu-Wiki
Jump to: navigation, search

Contents

General Lua

Hello World!" in Lua:

print "Hello World"

This will print "Hello World" on your screen.

Factorial in Lua:

function factorial(n)
if n == 0 then
return 1
else
return n* factorial(n-1)
end
end

Variables in Lua:

global V_e_2
local E_2
V_e_2 = nil
E_2 = 123

The global variable V_e_2 is being detroyed and E_2 becomes 123. Variables in Lua are case-sensitive and can only contain letter, numbers, operators, colons & semicolons and underscores!

Comments

Basic comments can be written by using two hyphens e.g.

if mycode == true then --"comment here"

For multi-line comments, then use

--[[ ''comment''
''here'' ]]--


Operators

Relational

You can use basic operators with Lua.

< --Less Than
> --Greater Than
<= --Less Than or Equal To
>= --Greater Than or Equal To
== --Equal To
~= --Not Equal To
+ --Addition
- --Subtraction
* --Multiplication
/ --Division

Examples of relation expressions:

1 == 1 --Returns true
1 < 5 --Returns true
6 >= 1 --Returns False
-- should above be "1 >= 6 --Returns false"? otherwise explanation is needed.

--Strings
"abc" < "def"  --Returns true
"abc" == "a".."bc" --Returns true
"abc" > "def" --Returns false

Tables are created by the { and } signs. Tables can be used to hold clusters of values. Objects will not be equal if the types are different, or if they refer to different objects:

{} == "table" --false
{} == {} --false, two different tables are being referenced.
t == {}
{} == t2
t == t2 --true, we are referencing the same tables as defined earlier

Coercion does not work here, the types must be converted explicitly.

"10" == 10 --false
tonumber ("10") == 10 --true

Logical Operators

Lua uses the logical operators 'and', 'or', and 'not'. In Lua, 'nil' and the boolean value 'false' both represent false in a logical expression. Anything that is not false is true.

true/false

false == nil --false, even though they represent the same, they are not equal.
true == false --false
true ~= false --true
1 == 0 --false
example_variable --test to see if the variable 'example_variable' exists, since it is not yet defined, it is nil (or false)

not The 'not' keyword inverts a logical expression value:

true --true
false --false
not true --false
not false --true
not nil --true, nil represents false
not not true --true, not can be used twice to negate itself (although not needed)
not "abc" --false, anything not false or nil is true

and The binary operator 'and' does not necessarily return a boolean value 'true' and 'false' to the expression 'x and y'. In some languages the 'and' operator returns a boolean dependent on the two inputs. In Lua, it returns the first argument if it is false or nil, and returns the second argument if the first is not false or nil. In short, a boolean value is only returned if the first argument is false or nil, or if the second argument is a boolean.

false and true --returns false since the first argument is false
nil and true --nil, same as above
nil and false --nil
nil and "hello" --nil
false and "hello" --false

All of the above expressions return the first argument. All of the following expressions return the second argument, as the first is true.

true and false --false, since the first argument isn't false it returns the second argument!
true and true --true
1 and "hello" --hello
"hello" and "there" --there
true and nil --nil

As you can see the logical expressions are still evaluated correctly but we have some interesting behaviour because of the values returned. or The 'or' binary operator also does not necessarily return a boolean value (see notes for 'and' above). If the first argument is not false or nil it is returned, otherwise the second argument is returned. In short, a boolean is only returned if the first argument is true or the second argument is a boolean.

true or false --true
true or nil --true
"hello" or "there" --hello
1 or 0 --1

All of the above expressions return the first argument. All of the following expressions return the second argument, as the first is false or nil

false or true --true
nil or true --true
nil or "hello" --hello

Basically, the 'or' operator does the opposite of the 'and' operator in terms of returning boolean values. This can be a very useful property. For example, setting default values in a function:

function abc(x)
local value = x or "default" --if argument x is false or nil, value becomes "default"
print (value, x)
end

abc() --no arguments, so x is nil
default nil
abc(1) --returns 1 and 1:

abc(true) --true and true

abc(hello) --hello and hello

Arithmetic

They can each be used in the obvious ways, and can also be used as unary negation and powers:

--Negation:
-(-10) --Returns 10
-(10) --Returns -10

--Powers:
7^2 --Returns 49
104^0 --Returns 1
2^8 --Returns 256

Ternary Operators

Ternary operators are a useful feature in C:

int value = x>3 ? 1 : 0;

This behavior can be partially emulated in Lua using the logical operators 'and' and 'or'. The C form:

value = test ? x : y;

roughly translates to the following Lua:

value = test and x or y

Example:

print( 3>1 and 1 or 0 ) --1
print( 3<1 and 1 or 0 ) --0
print( 3<1 and "True" or "False" ) --False
print( 3>1 and true or "false" ) --true

However, there is a caveat: This only works when the first return value is not 'nil' or 'false'.

print( 3>1 and 1 or "False" ) --works and returns 1
print( 3>1 and false or "oops" ) --failed, should return false, still returns oops
print( 3>1 and nil or "oops" ) --failed, should return nil, still returns oops
Personal tools