States
auto state Closed
on enter
business.open = false
end
end
state Open
on enter
business.open = true
end
on building.visitorEntered
business.visitors += 1
end
on exit
log("closing, {business.visitors} visitors today")
end
end
- A state holds event handlers, timer handlers,
on enterandon exit. auto stateis the starting state; a script has at most one. In a new instance itson enterruns in the start transaction (Lifecycle). This way a business that starts closed hasbusiness.open == false, whatever the contract's default.- Handlers outside any state belong to the empty state. A script with no states at all is valid.
- An event goes to the handler in the current state, or else to the one in the empty state; if neither exists, it is ignored. An instance takes the same event at most once.
enterandexitare written only inside a state;readyandmigrateonly at the script's level.
goto
goto Namemoves to another state. Agototo the current state does nothing: a repeated decision does not run the entry again.- Moving on runs, in order: the old state's
on exit→ the move → the new state'son enter. All of it is in the current transaction, and the handler goes on in the new state after thegoto. gotois not allowed insideon enterandon exit, nor in anything they call; the compiler follows the calls to check.- If
exitorenterfails, the whole transaction is undone and the state stays as it was; then the error policy applies.