Difference between revisions of "Lua Language"

From Cheat Engine
Jump to navigation Jump to search
Line 29: Line 29:
 
==Variables==
 
==Variables==
  
In Lua variables are polymorphic (can change the type during theirs lifes). By default a variable is global at Lua Engine level unless the declaration begins with <code><nowiki>local</nowiki></code> modifier.
+
In Lua variables are polymorphic (can change the type during theirs lifes). By default a variable is global at Lua Engine level unless the declaration begins with <code>local</code> modifier.
  
 
Any variable can host:
 
Any variable can host:
Line 35: Line 35:
 
* a numerical value (no difference between integer or float values, nor between different precisions)
 
* a numerical value (no difference between integer or float values, nor between different precisions)
 
* a boolean value (true/false)
 
* a boolean value (true/false)
* a string
+
* a string (constants can be surrounded by single <code>'string'</code>or double quotes <code>"string"</code> or doubles brackets <code><nowiki>[[string]]</nowiki></code>; the last case accepts new lines inside)
 
* a table
 
* a table
 
* a function
 
* a function
Line 51: Line 51:
 
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
 
MyVar2 = (10 > 7.55)  -- now MyVar2 becomes a boolean equal to true
 +
MyVar7 = print        -- global function variable MyVar7 which can be used as an alias of print function
 
</pre>
 
</pre>
  
Line 62: Line 63:
 
printf(MyVar1)              -- prints an empty line because MyVal1 is nil
 
printf(MyVar1)              -- prints an empty line because MyVal1 is nil
 
printf('MyVar3 = ',MyVar3)  -- prints the line 'MyVar3 = 2'
 
printf('MyVar3 = ',MyVar3)  -- prints the line 'MyVar3 = 2'
printf('MyVar2 = ',MyVar2)  -- prints just the line 'MyVar2 = ' because MyVar2 is not a numerical nor a string
+
printf("MyVar2 = ",MyVar2)  -- prints just the line 'MyVar2 = ' because MyVar2 is not a numerical nor a string
 +
MyVar7("9.9+0.1=",9.9+0.1)  -- prints the line '9.9+0.1=10.0'
 +
MyVar7('1/0=',1/0)          -- prints the line '1/0=inf'
 
</pre>
 
</pre>
  
Line 146: Line 149:
  
 
==Functions==
 
==Functions==
 +
 +
Functions are just some special variables, and can be declared at the global level or inside a block or another function.
 +
 +
<pre>
 +
-- first syntax version
 +
function MyFunction(arg1, arg2, ...)  -- takes 0, 1 to several arguments or undefined number of arguments
 +
  -- statements
 +
  return result1, result2              -- returns 0, 1 to several variables
 +
end
 +
 +
-- second syntax version
 +
MyFunction = function(arg1, arg2, ...) -- takes 0, 1 to several arguments or undefined number of arguments
 +
  -- statements
 +
  return result1, result2              -- returns 0, 1 to several variables
 +
end
 +
</pre>
 +
 +
Any of preceding 2 declaration versions creates a function variable named <code>MyFunction</code>. Then the user can call it, use it or assign it a new value.
 +
<pre>
 +
MyFunction(MyVar4, MyVar2, MyVar7)    -- calling MyFunction with 3 arguments
 +
MyVar1 = MyFunction( MyVar2, MyVar7)  -- calling MyFunction with 2 arguments and keep the first result in MyVar1
 +
print = MyFunction                    -- now the official print function will be MyFunction
 +
MyFunction = nil                      -- MyFunction will be unavailable now (but print still keeps original MyFunction)
 +
MyFunction = MyVar7                    -- now MyFunction is an alias of the original print function
 +
</pre>
  
 
==Tables==
 
==Tables==

Revision as of 15:32, 25 April 2022

Introduction

Lua is a lightweight, scripting programming language based on an interpreter generating compiled bytecodes (code executing in a virtual machine).

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. The CE Lua Engine is a main component of Cheat Engine.

This wiki page is written for Lua beginners. If you know the basic Lua syntax then you can go to the next page.

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 at Lua Engine level 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 (constants can be surrounded by single 'string'or double quotes "string" or doubles brackets [[string]]; the last case accepts new lines inside)
  • 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
MyVar7 = print         -- global function variable MyVar7 which can be used as an alias of print function

Console output

Lua uses the print function to output numericals and strings to the console (use the menu to open the Lua Engine console). The print function has a variable number of arguments. Each argument (string or numerical) will be printed to the console (with no separation) and a new line will be added at the end. The default format for numericals is the decimal base with automatic use of decimal point if necessary.

Examples:

printf(MyVar1)              -- prints an empty line because MyVal1 is nil
printf('MyVar3 = ',MyVar3)  -- prints the line 'MyVar3 = 2'
printf("MyVar2 = ",MyVar2)  -- prints just the line 'MyVar2 = ' because MyVar2 is not a numerical nor a string
MyVar7("9.9+0.1=",9.9+0.1)  -- prints the line '9.9+0.1=10.0'
MyVar7('1/0=',1/0)          -- prints the line '1/0=inf'

Flow control

Lua provides a conditional test with multiple, 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 becomes true

for i = Start, Stop, Step do  -- any numerical value for Start, Stop and Step (Step is optional and defaults to 1)
  --statements
end                           -- loop while i <= Stop (for positive Step) or i >= Stop (for negative Step)

for Key, Val in pairs(Tab) do -- it loops over all the table Tab using standard iterator function pairs
  --statements                -- key and val available inside the for loop
end

Lua has a special unconditional block mainly used to isolate some local variables:

do
  --statements
end

Examples:

i=5
while i<80 do                 -- it loops for i equal to 5, 10, 20, and 40
  i=i*2
end

i=5
repeat                        -- it loops for i equal to 10, 20, 40, 80
  i=i*2
until i==80

i=5
repeat
 i=i*2
until i==82                   -- warning: this loop will never ending and will block the Lua Engine & Cheat Engine

for i = 10, 14 do             -- it loops for i equal to 10, 11, 12, 13, and 14
end

for i = 10, 14, 2 do          -- it loops for i equal to 10, 12, and 14
end

for i = 10, 14, 2.1 do        -- it loops for i equal to 10 and 12.1
end

for i = 10, 14, -2.1 do       -- it never loops because the start value 10<14 and step is negative
end

for i = 14, 10, -2.1 do       -- it loops for i equal to 14 and 11.8
end

do
  local MyVar=314;
  print(MyVar)                -- prints '314' and new line
end
printf(MyVar)                 -- prints nothing but a new line

Functions

Functions are just some special variables, and can be declared at the global level or inside a block or another function.

-- first syntax version
function MyFunction(arg1, arg2, ...)   -- takes 0, 1 to several arguments or undefined number of arguments
  -- statements
  return result1, result2              -- returns 0, 1 to several variables
end

-- second syntax version
MyFunction = function(arg1, arg2, ...) -- takes 0, 1 to several arguments or undefined number of arguments
  -- statements
  return result1, result2              -- returns 0, 1 to several variables
end

Any of preceding 2 declaration versions creates a function variable named MyFunction. Then the user can call it, use it or assign it a new value.

MyFunction(MyVar4, MyVar2, MyVar7)     -- calling MyFunction with 3 arguments
MyVar1 = MyFunction( MyVar2, MyVar7)   -- calling MyFunction with 2 arguments and keep the first result in MyVar1
print = MyFunction                     -- now the official print function will be MyFunction
MyFunction = nil                       -- MyFunction will be unavailable now (but print still keeps original MyFunction)
MyFunction = MyVar7                    -- now MyFunction is an alias of the original print function

Tables

External links