DE EN FR ES IT CZ

5. First scripts for Pia's cellar

First scripts for Pia's cellar

Pia at the ventilation grate, a click counter on the wall next to her, the cat watches

Now it gets practical. The cellar has a value failed_attempts with the initial value 0 and the condition grate_open from the first guide.

The function

Create a definition script called cellar:

-- Counts a wrong attempt at the grate.
-- Returns true as soon as there were three or more attempts.
function failedAttempt()
  local n = Values["failed_attempts"].Int + 1
  Values["failed_attempts"].Int = n
  return n >= 3
end

The action

The grate has an action for wrong items. In it you build:

If lua result:     return failedAttempt()
    Display text:  "The screws! I need something to turn them with."
Else
    Display text:  "Hm. Not like that."
End if

The first two wrong attempts get a short answer, from the third on a clear hint. You can reuse the function anywhere else without building anything again.

One check, many places

Whether Pia can leave the cellar depends on two things. Instead of repeating this check in every action:

function canLeave()
  return Conditions["grate_open"].Value
     and Values["failed_attempts"].Int < 10
end

Every action then only contains return canLeave(). If the rule changes, you change it in exactly one place.

For pure combinations of conditions there is also the "Combined condition" without Lua. More on that in the wiki under Structuring game states.

Rule to remember
Scripts decide, actions show. Texts, animations and sounds stay in the editor.

Your task
Rebuild failedAttempt() and test a wrong item on the grate three times in a row.