Skip to reading
Lua Scripting

Choose where to store a 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 20

How long should the game remember something? Answer that first. The right place to store it follows from that decision.

A value for one calculation#

Code example
local price = 4
local quantity = 3
local total = price * quantity

Inside run, these names exist while that function is running. A later call can calculate a different total without carrying over an old purchase.

A value for the current session#

A local declared above the functions belongs to the script instance. With Keep global instance alive selected, the same instance can count room arrivals. Programming page 15 shows that counter and the matching event handler.

A value other scripts and save slots need#

Code example
await(SetVariable({ name = "met_keeper", value = true }))
if IfVariable("met_keeper", true) then
    Log("The keeper recognizes the player.")
end

This fragment assumes a Boolean game variable with ID met_keeper and a starting value of false. Another record can read the same flag later, even after the player changes rooms. Load a game slot to restore the progress captured when that slot was saved.

A displayed name, a line of dialogue and a saved flag can all describe the same event. Decide which stored value tells the game what actually happened.

Page 38 of 133Permalink
Go to page