Aros/Developer/Docs/LUA

Lua
Lua is already install for use in Icaros Desktop but other distributions may not have it so

The Lua directory can be place anywhere but some assigns need to be added

assign lua: drive:directory/Lua e.g. assign lua: sys:dev/Lua

Write you lua code and save as filename.lua

lua filename.lua

see next section for AROS specific features and amilua

Lua consists part


 * Functional
 * Object-orientated

There are other aspects of Lua programming than functions, but functions are easy to understand, an example...

As you see, it's easy to declare a function, you might use this function elsewhere by calling:

Within a function, you can declare local values. Local values have a scope which is limited to the function where they are declared. Any function may return a value, here's an example:

As you may have guessed, this function performs an addition, you can call:

and the value of "c" will be 3.

Lua has some common controls like "if...then" or "while...do". Here is a short example:

And another one:

To compare numeric values, you can use the operators:

== (equals) ~= (different) >(greater) <(lower) >= (greater or equal) <= (lower or equal)

Starters Tutorial

Another Tutorial

Learn how to use userdata and what custom classes are (use calling methods and do not use reference indexes).

AmiLua
AmiLua extends Lua with some functions for handling intuition windows, gadgets, menus and graphics functions.

There are 3 Lua tables:
 * Siamiga: Windows, creating of gadgets and menues, message handling, drawing functions
 * Sigadget: setting and getting of gadget parameters
 * Sipicture: loading, grabbing and blitting of bitmaps.

.alua is the specific "dialect" of lua that includes zulu and siamiga. If you are going to write some zulu or siamiga stuff you need to use the .alua extension and to point to amilua.

Sipicture
A complex drawing utility, should be written it in C, as you will soon reach the limits of what LUA offers.

Zulu
Zulu allows you to create MUI applications from Lua scripts.

You'll find it in the current nightly build in the directory "Extras/Development/Lua". You need an assign named "lua" to this directory. The binary Amilua is build with the MUI binding. Some examples are in the subdir "test-mui.Documentation is in "doc/zulu.txt". If you haven't programmed MUI before you should download mui38dev from Aminet and study the documentation.

Groups are MUI/Zune classes, too. Of course, you have to look in the documentation what attributes are possible. The Group class is a subclass of the Area class, this makes it possible to use its attributes like MUIA_Disabled.

ASL requesters
To get multiple files from an ASL requester through Zulu function mui.filerequest, you have to call the function within curly brackets in order to get multiple results

files = { mui.filerequest(.....) }

mui.ASLFR_DoMultiSelect, true Below there's a snipped of my Lua code (based on Mazze's example)

Lua supports currently only static libraries. zulu and siamiga a static libraries. So you have to create a static library and then re-link AmiLua with that library. IIRC there is needed a header file for the connection between Lua and the library.

Libraries
LUA.Filesystem (not implemented yet)

LUA.Socket

lua.sqlite3

db = sqlite3.open("filename")

db = sqlite3.open_memory

db:close

db:exec

db:irows db:rows db:cols db:first_irows db:first_rows db:first_cols

db:prepare db:set_function

LuaSQL frontend docs.

Lua use of first-class functions allow the employment of many powerful techniques from functional programming;

and full lexical scoping allows fine-grained information hiding to enforce the principle of least privilege.

Lua allows programmers to implement namespaces, classes, and other related features using its single table implementation;

Lua does not contain explicit support for inheritance, but allows it to be implemented relatively easily with metatables.

Examples
Sample Code

Shell variables are files which are stored in ENV:.

So you can either use output redirection:

lua:lua checkspace.lua >env:myvariable

Or you can use the file handling functions of Lua to write to ENV:myvariable.

This example lets you enable/disable the group with the two text fields.

How to declare variable located in external file? I need config text file with some variables. How to declare them in main file?

example:

config.cfg:

v = 5

main.lua:

a = 5 v = ?????????

a = a + v

loadfile is what you need.

Issues
Floattext.mui class & Lua Is it possible use this class in Zulu? How to use it to read and show text in window? Could not get it working nicely for WHD_Menu. Ended up using a list object. You can download WHD_Menu from Aros Archive to have a look on how it was achieved. This object is used to display the readme file (variable is CurrentReadMe).

Is it possible to use in this class a function to search requested word in readme file to get line number as result? Each line is an entry in the list. Just parse all entries and search for the string in the field. You'll get directly the line number this way.

This thread multiple file selection isn't working out of the box, you have to create your own class and this is not possible with Lua/Zulu, you have to do it in C.

If you download to other location having space in name still will be problem. How to fix it? Ensure that the path is enclosed in double quotes. From the AROS Exec Download Tool:

local retval = os.execute('c:run sys:utilities/multiview "' .. readme_target .. '"')

Here I have the double quotes only for readme_target because the path to MultiView doesn't have spaces.

changed line to:

local dir_opt = "\"" .. dir_str:getstr(mui.MUIA_String_Contents) .. "\""

Examples here and here.

How to internationalize Lua script? There is no interface to locale.library in Lua. Just create tables with strings.

strings = {} strings["deutsch"] = {"Anwendung", "Fenster"} strings["english"] = {"Application", "Window"}

language = "english"

print(strings[language][1])

So I need to add "choose language" gadget or is there any function to check locale used in system? Could I use external files deutsch.lang, polski.lang etc. or these strings must be in main file? Which way is easier and which is better for these small Lua scripts?

There is an environment variable named "language" and Lua has a function to query environment variables:

language = os.getenv ("language") if language = NIL language = "english" end

In MUI/ZUNE to read a string value from a ListObject the following code applies.

STRPTR *stringValue = NULL; DoMethod(list, MUIM_List_GetEntry, active, &stringValue);

Bear in mind that MUIM_List_GetEntry returns a pointer to a generic entry. A string pointer is only a special case for single column lists.

How does ZULU handle this in Lua, as according to zulu.txt none of the accessor methods (getstr, dostr, etc) appear to match this patten of passing a string pointer as an argument? I'm using local current = listobject:getint(mui.MUIA_List_Active) to query the index of the selected entry. So you could assign a StrArray to mui.MUIA_List_SourceArray then use the mui.MUIA_List_Active result as the index for that array.

LOVE nLOVE
http://love2d.org/

Wiki, IDE,

function love.draw love.graphics.print("Hello World", 400, 300) end

Getting Started


 * love.load
 * love.update
 * love.draw
 * love.mousepressed
 * love.mousereleased
 * love.keypressed
 * love.keyreleased
 * love.focus
 * love.quit

Debugging, Debug,

require("debug")

"scripts.library.enemy" instead of "scripts/library/enemy"

make a silly error that you didn't notice, like calling newImage, newImageData, newFont, etc. in love.update or love.draw?