<GeeXLab Rootard Guide/>

## GeeXLab and Lua

Last Update: 2018.12.11 by JeGX

>> Back <<

The Lua language is one of the two languages ​​currently available in GeeXLab (the other language is Python). Lua is the preferred language of GeeXLab since it is the first language that has been supported and is part of the GeeXLab kernel (Python is available via an extension).

Lua is a really simple language to use while being powerful (power = features). This easily accessible power has attracted many developers because Lua is used in many applications and video games (VLC, Wireshark, World of Warcraft, follow CryEngine, this link for more examples). Its syntax is clear and flexible (we can encode complete instructions on a single line for example). But especially Lua is fast. It is one of the fastest scripting languages.

If you do not know with which language to use to start programming GeeXLab, do not hesitate, go on Lua. I occasionally program Python demos, but I mostly use Lua. The vast majority of demos, code samples and applications made with GeeXLab are Lua-coded. When I add new features in GeeXLab, they are first available in Lua.

Just to fix the ideas, here are some lines of Lua:
``````a = 5
b = 2
c = a + b
d = 0
if (c > 5) then
d = 1
else
d = -1
end

s = "Ceci est une chaine"
s = s .. " de caracteres"

x = 0
for i=0, 10 do
x = x + 1
end

function myKoolFunc(a, b, c)
-- Do something useful with a, b and c:
local sum = a+b+c
local avg= sum/3
return sum, avg
end

x, y = myKoolFunc(1, 2, 3)
``````

Lua is fast, very fast. But if its native speed is not enough, there is a solution: LuaJIT . Thanks to LuaJIT, it is possible to multiply the speed of Lua by 10 in some cases. LuaJIT is also supported by GeeXLab. See this article to know how to enable LuaJIT in GeeXLab.

GeeXLab is always (as far as possible, I do not have four arms!) up to date with the evolution of Lua. The latest version of Lua is 5.3.4 (wrong: 5.3.5!) and is the version supported by GeeXLab. This is not the case for LuaJIT which is limited for the moment to branch 5.1 of Lua (5.1.4 to be accurate). This small difference can cause some problems if your Lua code uses some specifics of Lua branch 5.2 or 5.3 as bitwise operators. So if you plan to use LuaJIT, it is advisable to limit yourself to Lua 5.1.

## Simple GeeXLab Demo in Lua

And finally, here's an example of a GeeXLab demo that uses Lua. The demo is composed of three files: the main XML, the INIT script and the FRAME script.

For comparison, the same Python demo is HERE.

#### The main XML file: main.xml

``````
<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<glsl_hacker>

<window name="win3d01" title="Hello World!" width="700" height="200" />

<script name="init_scene" run_mode="INIT" filename="init.lua" />

<script name="update_scene" run_mode="FRAME" filename="frame.lua" />

</glsl_hacker>
``````

#### The INIT script: init.lua

``````
font = gh_utils.font_create("Tahoma", 14)
``````

#### The FRAME script: frame.lua

``````
gh_renderer.clear_color_buffer(0.4, 0.4, 0.4, 1.0)

x = 10
y = 20
gh_utils.font_render(font, x, y, 1.0, 1.0, 0.0, 1.0, "Hello World from GeeXLab!")

x = 10
y = 40
elapsed_time = gh_utils.get_elapsed_time()
gh_utils.font_render(font, x, y, 1.0, 1.0, 1.0, 1.0, "Elapsed time: " .. elapsed_time)
``````