Lua for GDScript Developers
Move from GDScript 4 to Lua 5.1: numeric rules, tables, functions, modules and metatable objects. Build a multi-file stock ledger and port inventory and room programs.
GDScript and Lua side by side
The same program in GDScript and in Lua, from the lessons of this course.
Print and Comment
A GDScript program normally belongs to a script class. These source examples use a small SceneTree script so they can run without a scene: _init() runs the statements and quit() finishes. A Lua file can execute statements directly, without extends, a method…
GDScript1extends SceneTree2 3func _init():4 var name = "Mira"5 # Preserve the space after the comma.6 print("Hello, " + name + "!")7 quit()Lua1local name = "Mira"2-- Preserve the space after the comma.3print("Hello, " .. name .. "!")null and Missing Fields
GDScript uses null for the absence of an object or a value in a Variant. Lua uses nil. A GDScript dictionary can store null under a key and still report that key through has(). Lua tables cannot retain a key whose value is nil: assigning it removes the entry.…
GDScript1extends SceneTree2 3func _init():4 var settings = {"enabled": false}5 settings["enabled"] = null6 print("present" if settings.has("enabled") else "absent")7 quit()Lua1local settings = {enabled = false}2settings.enabled = nil3if settings.enabled == nil then4 print("absent")5else6 print("present")7endRanges to Numeric for
GDScript's range(start, stop, step) excludes its stop value. Lua's numeric for index = start, stop, step do includes the stop when the step reaches it. To sum through a positive limit, a GDScript loop can use range(1, limit + 1); Lua can use for index = 1…
GDScript1extends SceneTree2 3func _init():4 var total = 05 for value in range(1, 4):6 total += value7 print(total)8 quit()Lua1local total = 02for value = 1, 4 do3 total = total + value4end5print(total)Functions and Returns
A GDScript helper is commonly declared as a script method with func, optional parameter types, and an optional return type. Lua declares a function with function ... end and has no parameter or return-type annotation syntax. Use local function subtotal(price…
GDScript1extends SceneTree2 3func subtotal(price: int, quantity: int) -> int:4 price = price * quantity5 return price6 7func _init():8 var unit_price = 89 print(subtotal(unit_price, 3))10 print(unit_price)11 quit()Lua1local function subtotal(price, quantity)2 price = price * quantity3 return price4end5local unit_price = 86print(subtotal(unit_price, 3))7print(unit_price)Arrays as Dense Tables
A GDScript array owns a contiguous sequence of slots starting at zero. Lua uses a table with consecutive integer keys starting at one to represent a sequence. Use table.insert(items, value) to append and table.remove(items, position) to remove an element…
GDScript1extends SceneTree2 3func _init():4 var labels = ["key", "map"]5 labels.remove_at(0)6 labels.append("done")7 print(",".join(labels))8 quit()Lua1local labels = {"key", "map"}2table.remove(labels, 1)3table.insert(labels, "done")4print(table.concat(labels, ","))Sorting and Comparators
GDScript Array.sort_custom accepts a callable that reports whether its first argument belongs before the second. Lua table.sort uses the same boolean comparator idea, but the sequence starts at index one. Both functions rearrange the existing container. To…
GDScript1extends SceneTree2func _init():3 var scores = [3, 9, 3]4 scores.sort_custom(func(a, b): return a > b)5 var text = PackedStringArray()6 for score in scores:7 text.append(str(score))8 print(",".join(text))9 quit()Lua1local scores = {3, 9, 3}2table.sort(scores, function(a, b) return a > b end)3local text = {}4for i, score in ipairs(scores) do text[i] = tostring(score) end5print(table.concat(text, ","))
Syllabus
10 chapters48 lessons1 project48 challenges99 quiz questionsFirst Lua Programs
6 lessonsValues and Decisions
5 lessonsIteration and Flow
5 lessonsFunctions and Closures
5 lessonsTables as Collections
5 lessonsOrdering, Modules and Errors
5 lessonsObjects and Owned State
5 lessonsComposition and Object Lookup
6 lessonsProject: Stock Ledger
4 lessonsFinal GDScript Ports
2 lessonsFrequently asked questions
Who is Lua for GDScript Developers for?
For people who already write GDScript and want to learn Lua. Each lesson starts from GDScript you know and shows how Lua does the same thing, so you skip the beginner basics.
Do I need to know Lua already?
No. The course starts with your first Lua program. You need GDScript basics: variables, conditions, loops, functions and collections.
How many lessons does Lua for GDScript Developers have?
Lua for GDScript Developers contains 48 interactive lessons you can complete at your own pace.
What does Lua for GDScript Developers cover?
The course is organized into chapters covering: First Lua Programs, Values and Decisions, Iteration and Flow, Functions and Closures, and more.
Is Lua for GDScript Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.