Skip to reading
Lua Scripting

Read a return value

Edition 0.86.4.1 HF1

0.86.4.1 Hotfix 1 edition. Earlier lessons and screenshots retain their original version context; revised references and supplements describe the newer behavior.

Download PDFView PDF page
On this page

LESSON 21

A return value is an answer sent back to the code that asked a question. It can be a word, a number, true, false or another Lua value.

Code example
local Script = {}
Code example
local function can_pay(coins, price)
    return coins >= price
end
Code example
function Script.run(context)
    local allowed = can_pay(4, 3)
    Log(tostring(allowed))
    return allowed
end
Code example
return Script

Follow the call#

Lua passes 4 as coins and 3 as price. The comparison succeeds, so can_pay returns true. The caller stores that answer in allowed. Log turns the value into readable output. The next line returns the same answer to whoever started run.

What happens at the boundary?#

Source table from Read a return value, page 39
can_pay(3, 3)Returns true. The >= comparison includes an exact match.
can_pay(2, 3)Returns false. The function ran successfully and answered the question.
No explicit returnAn ordinary function with no returned values yields nil when its caller assigns the result to a single local.

For a function in another PCA record, CallScript accepts the record ID and function name. Programming page 19 shows how to capture that answer across records.

Page 39 of 133Permalink
Go to page