Docs Quick code
Checking and debugging
Check's mistakes and warnings, print and watch, breakpoints and stepping, the profiler, and catching errors.
#Check
The Check button on a Code node (and the problems line above the graph) lists:
Mistakes — the code won't run as written:
- something that doesn't read as code (a missing
end,thenor), text missing its closing quote,if x = 1instead of==); - an unknown function (
explode()) or unknown name (a variable nothing gives a value); - a command objects don't have (
self.fly()); break/continueoutside a loop.
Warnings (warning:) — it runs, but probably not as you meant:
| Warning | Means |
|---|---|
| this loop never ends | while true (or until false) with no break and no wait inside. |
| nothing inside this loop changes x | The condition's variables never change in the loop. |
| this loop never runs | The condition is always false, or for i = 10, 1 without a negative step. |
| a step of 0 never gets anywhere | for i = 1, 10, 0. |
| local x is never used | A local that's set but never read (start its name with _ to say that's on purpose). |
| this line never runs (it comes after return) | Code after return, break or continue in the same block. |
| f() takes 1 value, but gets 2 | Calling your own function with too many values. |
| wait() doesn't wait here | A wait in dialogue code, which runs all at once. |
| x is the name of a game function; a local with that name hides it here | local popup = 1 would stop popup() working in that block. |
Each message starts with its line number. A name only counts as known if it's a game or world variable in the Explorer, set somewhere in the project's code, local to this code, or one of the game's or the language's own.
#While the game runs
Errors in a running game go to the editor's Output panel and the game carries on:
[script] error in "Boss" line 12: that's nil (nothing), so it has no .hp. Does it have a value yet? (in damage(), called from line 30)
They say the line, the function it happened in and where that was called from. Click the error to open that Code node.
| Tool | How |
|---|---|
print(a, b, …) |
Writes to the Output panel. |
watch(name, value) |
Shows a value live: in the Output panel's Watch line, and in the game's F3 panel. Call it as often as you like (only changes are sent). |
warn(message) |
A warning line in the Output (the code goes on). |
breakpoint() |
Pauses the game on that line and shows this code's local values. F5 carries on; F10 runs the next line and pauses again (stepping). Only in games started from the editor (▶ Play); an exported game just carries on. |
| F3 in the game | The debug panel: FPS, position, variables, switches, items, timers, watched values, and the busiest scripts (how often they ran, how many steps, how long they took). |
for i, e in objects("enemy") do
watch("enemy " .. i, e.hp)
end
if self.hp < 0 then breakpoint() end -- stop and look when something's wrong
#Catching errors in code
local ok, result = pcall(function()
return data("Enemies")["Dragon"].hp
end)
if not ok then
warn("No dragon in the data: " .. result)
end
assert(#objects("spawn_point") > 0, "this level needs a spawn_point")
error("stop here with this message")
#Speed
Each Code node is compiled once, and conditions too, so code that runs every frame is cheap —
roughly a million simple steps per second in the game. If something feels slow, F3 shows which
script is busy. Loops that run hundreds of times every frame are the usual cause: wait a little
(wait(0.1)) or do the work once and keep the result in a variable.