Skip to reading
Lua Scripting

Give a clue time to land

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

TIME, FILES AND SMALL EXPERIMENTS

Two lines in a row can run so quickly that the player never sees the first result. A wait gives the game time to draw another frame.

Code example
Log("First chime")
await(Wait(0.5))
Log("Second chime")

Put this fragment inside run. The log gets two entries about half a second apart. await lets the current job pause while the game continues. Programming page 13 shows how to start two jobs together and wait for both.

Keep a written clue between sessions#

Code example
local path = "user://journal/note.txt"
local previous = ReadTextFile(path, "")
local next_note = previous .. "The dial points west.\n"
local written = WriteTextFile(path, next_note)
Log("Wrote note: " .. tostring(written))

On the first run there is no old note, so ReadTextFile supplies empty text. On the second run it reads the first line before writing both lines back. Start at user:// so PCA chooses the game’s writable location on each device.

Inspect the worked file example#

The complete Lua record on programming page 14 shows what happens when it runs twice. It writes one dated journal entry per run and sets a JSON preference called hints to false. The function returns the stored preference. For this script, a return value of false is the expected result of a successful read.

Choose a distinct filename for each purpose. WriteTextFile replaces the file contents. The code above preserves the old text by reading it first.

Lua scripting   /   Illustrated guide                                                          W15
Page 15 of 133Permalink
Go to page