Get started with Defold. Lua. (UPD. 21.09.2023)

This is the first part of “Get started with Defold” articles. Let’s start with the most important links to useful Lua resources.

1. Get started with Defold. Lua.
2. Get started with Defold. Engine. (only in Russian, at the moment)
3. Get started with Defold. Community. (only in Russian, at the moment)


Lua

Lua – is a scripting programming language. What is more important for us is that it is used to write game logic in the Defold engine.

The engine uses LuaJIT and Lua 5.1 where LuaJIT isn’t supported. LuaJIT is based on Lua 5.1 with some improvements. Always make sure that the manuals and Lua libraries you are using are compatible with Lua 5.1.

For beginners

A few links about Lua I would recommend for all beginners:


It’s also possible to try Lua right in a browser, which may be convenient while learning:

There are a few points that are important to know when you start with Lua in Defold:

  • In Lua, arrays start at 1 instead of 0.
  • Array is a table with integer indices in order.
  • The # operator for a table (the keys are explicit, for example, tbl or tbl2) returns a sequence from 1 to an interruption of the sequence. For an array (the indices are not explicitly specified as in array and array2), any nil may interrupt a sequence. That means that any use of sequences with “holes” (nil) may lead to unpredictable results with operator #. Examples:
local tbl = {[1] = "one", [2]="two",[3]="three", [4]=nil, [5]="five", six = "six"}
print(#tbl) -- 3 because indices in order are only 1,2,3, and 4 is nil

local tbl2 = {["a"] = 1, ["b"] = 2}
print(#tbl2) -- 0

local array = {"one", "two", "three", nil, "five", "six"}
print(#array) -- 6

local array2 = {"one", "two", "three", nil}
print(#array2) --3
  • If you forget to write local before a variable or a function, then it will be global
  • The global table is accessible by _G
  • Functions in Lua should be created before the place you call them
  • A local variable in * .script or * .gui_script is available in all instances of this script. It is a static class variable in comparison with other languages. If you use the same script on several objects, then changes of such variables in one script will be available in the other objects.
  • self is used to write unique data for each instance
  • To iterate from larger to smaller, remember to specify the third step parameter:
-- a regular for loop from 1 to 10
for i = 1, 10 do
    -- do things
end

-- a for loop from 10 to 1
for i = 10, 1, -1 do
    -- do things
end

Editors

In addition to the built-in code editor in Defold Editor itself, plugins are available for the following editors:

For advanced users

A few links for a more in-depth study:


And finally, a large collection of links to various materials about Lua.


Feel free to share in the comments any interesting materials on the topic.

If you have found a spelling error, please, notify us by selecting that text and pressing Ctrl+Enter.

Spelling error report

The following text will be sent to our editors: