Skip to reading
Lua Scripting

Run your first Lua record

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

LESSON 01

Give the game a line of dialogue and give yourself a way to tell whether the script ran.

Open a test project. Select the Lua workspace, create a script and give it the unique ID first_lua.

Leave Enabled on and Global Script off. Replace the entire source with the record below.

Code example
local Script = {}
Code example
function Script.run(context)
    Log("The archive bell rang.")
    await(ShowMessage({ text = "Someone is still in the archive.",
                        duration = 1.0 }))
    return 42
end
Code example
return Script

Choose Validate, then Apply Changes and save the project. Start Test Scene or Play. Use Run Script

against the running preview, or attach the script to an interaction. Look for the log line and the

on-screen message. The number 42 is available to a caller that captures the return value.

Read the four moving parts#

Script is a local table. The function declaration puts run in that table. PCA calls it and supplies the

caller context. The final return gives the whole table to PCA. Put other exported functions above

that last line. A top-level return of 42 would hand PCA a number where it needs a module.

Each boxed Lua record in the lessons can replace an entire record unless labeled as a fragment. Use a

fresh ID per lesson to keep earlier exercises available.

Page 19 of 133Permalink
Go to page