DE EN FR ES IT CZ

4. Accessing the game

Accessing the game

Pia pulls out a drawer of a large cabinet with symbol labels

Everything you create in the editor is an object for Lua: scenes, characters, items, conditions, values, interfaces. Objects of the same kind sit in tables that are always available.

TableContainsExample
ScenesScenesScenes["cellar"]
CharactersCharactersCharacters["pia"]
ConditionsConditionsConditions["grate_open"]
ValuesValuesValues["failed_attempts"]
InterfacesInterfacesInterfaces["inventory"]

The square brackets contain the internal name from the editor, not the name the player sees in the game.

Reading and setting fields

-- condition: field Value (true or false)
if Conditions["grate_open"].Value then
  print("The grate is open")
end
Conditions["grate_open"].Value = true

-- value: field Int for the number, field String for the text
local n = Values["failed_attempts"].Int
Values["failed_attempts"].Int = n + 1

-- the game object is always there
print(game.CurrentScene.name)       -- internal name of the current scene
print(game.CurrentCharacter.name)   -- internal name of the controlled character

Not every field may be changed by a script. Which fields exist and which are writable is listed in the wiki under Data structures.

When a name exists twice

If two characters have a value called coins, Values["coins"] is not unique. Then you go through the object path:

local c = Characters["pia"].Values["coins"].Int

You don't have to type the path: the object lists of the editor have the menu item "Copy Lua Path".

Rule to remember
Give unique internal names in lower case. Then every access stays short and readable.

Your task
Print the name of the current scene with print and set a condition by script that makes an object visible.