Difference between revisions of "Lua Language"
Line 37: | Line 37: | ||
* a function | * a function | ||
+ | '''Warning:''' the basic arithmetic operators like '''+''' or '''-''' are defined only for numericals. | ||
+ | |||
+ | '''Examples:''' | ||
<pre> | <pre> | ||
MyVar1 = nil -- global variable MyVar1 having nothing inside (no type neither) | MyVar1 = nil -- global variable MyVar1 having nothing inside (no type neither) | ||
MyVar2 = UnknownVar -- another global variable equal to nil (because UnknowVar is not yet defined) | MyVar2 = UnknownVar -- another global variable equal to nil (because UnknowVar is not yet defined) | ||
− | local MyVar3 = 2 -- local variable MyVar3 equal to 2 | + | local MyVar3 = 2 -- local numerical variable MyVar3 equal to 2 |
MyVar4 = 'Hello' -- global string variable MyVar4 | MyVar4 = 'Hello' -- global string variable MyVar4 | ||
local MyVar5 = "Hello" -- local string variable MyVar5 having the same string as MyVar3 | local MyVar5 = "Hello" -- local string variable MyVar5 having the same string as MyVar3 | ||
MyVar6 = [[First Line | MyVar6 = [[First Line | ||
Second Line.]] -- global multi-line string variable MyVar6 beginning with 'First' and ending with 'Line.' | Second Line.]] -- global multi-line string variable MyVar6 beginning with 'First' and ending with 'Line.' | ||
− | + | MyVar2 = (10 > 7.55) -- now MyVar2 becomes a boolean equal to true | |
+ | </pre> | ||
+ | |||
+ | ==Flow control== | ||
+ | |||
+ | Lua provides a conditional test with mutiple, optional branches: | ||
+ | |||
+ | <pre> | ||
+ | if BooleanExpression then | ||
+ | -- if statement(s) body | ||
+ | elseif BooleanExpression then -- optional | ||
+ | -- elseif statement(s) body | ||
+ | else -- optional | ||
+ | -- else/default statement(s) body | ||
+ | end | ||
+ | </pre> | ||
+ | |||
+ | and several conditional loops: | ||
+ | |||
+ | <pre> | ||
+ | while BooleanExpression do -- do statements while BooleanExpression is true | ||
+ | --statements | ||
+ | end | ||
+ | |||
+ | repeat -- repeat statements | ||
+ | --statements | ||
+ | until BooleanExpression -- until BooleanExpression is true | ||
+ | |||
+ | for i = start, stop, step do -- any numerical value for start, stop and step | ||
+ | --statements | ||
+ | end -- until i value equals stop value | ||
</pre> | </pre> | ||
− | |||
==Functions== | ==Functions== |
Revision as of 09:44, 25 April 2022
Contents
Introduction
Lua is a lightweight, scripting programming language based on an interpreter generating compiled bytecodes.
It was designed in Brasil by Roberto Ierusalimschy, Waldemar Celes and Luiz Henrique de Figueiredo in 1993.
CE Lua is a specific version of Lua interpreter adapted by Dark Byte for Cheat Engine.
Syntax
CE Lua interpreter respects traditional LUA syntax and provides some extra features. The Lua Script Editor provide a real-time colored syntax and a Syntax Check feature (Ctrl+Alt+C).
Comments
In Lua simple comments begin with --
and end at the end of line.
Multi-line comments begin with --[[
and end with ]]
.
-- This is a simple comment just to the end of this line This is not a comment but a future syntax error -- This is a new simple comment --[[ First line of a multi-line comment Second line of a multi-line comment Last line of a multi-line comment]]
Variables
In Lua variables are polymorphic (can change the type during theirs lifes). By default a variable is global unless the declaration begins with local
modifier.
Any variable can host:
- nothing (not defined, equal to nil)
- a numerical value (no difference between integer or float values, nor between different precisions)
- a boolean value (true/false)
- a string
- a table
- a function
Warning: the basic arithmetic operators like + or - are defined only for numericals.
Examples:
MyVar1 = nil -- global variable MyVar1 having nothing inside (no type neither) MyVar2 = UnknownVar -- another global variable equal to nil (because UnknowVar is not yet defined) local MyVar3 = 2 -- local numerical variable MyVar3 equal to 2 MyVar4 = 'Hello' -- global string variable MyVar4 local MyVar5 = "Hello" -- local string variable MyVar5 having the same string as MyVar3 MyVar6 = [[First Line Second Line.]] -- global multi-line string variable MyVar6 beginning with 'First' and ending with 'Line.' MyVar2 = (10 > 7.55) -- now MyVar2 becomes a boolean equal to true
Flow control
Lua provides a conditional test with mutiple, optional branches:
if BooleanExpression then -- if statement(s) body elseif BooleanExpression then -- optional -- elseif statement(s) body else -- optional -- else/default statement(s) body end
and several conditional loops:
while BooleanExpression do -- do statements while BooleanExpression is true --statements end repeat -- repeat statements --statements until BooleanExpression -- until BooleanExpression is true for i = start, stop, step do -- any numerical value for start, stop and step --statements end -- until i value equals stop value