Escape Simulator Wiki

One stop place for all information about features in Escape Simulator


Project maintained by SuperJura Hosted on GitHub Pages — Theme by mattgraham

Lua Scripting Intro In the Escape Simulator’s Room Editor, you can add dynamic behaviors to your rooms in 2 ways. The default way of connecting objects, and the new Scripting way. You can even mix the two approaches if you need to. The code you will write is Lua 5.2 and is interpreted using Moonsharp interpreter.

Script Object

In Room Editor, Scripting is exposed using a Logic element “Script”. When you place the object on the scene, you will have to attach a .lua script to it. In the Properties UI you can create a script if you don’t have one. When you have selected a script, you can click the folder icon to open it in a text editor.

Script Selector Script Inspector

The object has the same attributes as any other, most importantly, visibility. While the Script object is visible, the code will run. The moment it turns invisible, the code stops executing. This allows more flexibility which code should run and when.

Script organization

The code is written in a .lua file and has to have a specific organization. The game fires events when something happens and executes the whole lua file. Because of that, we have to catch specific events and execute the code for them.

lua Example In the code example, you can see the way the code is organized. When the game fires an event, callType holds the event ID. So we can check the callType and execute specific code for its type.

Variables & Events

Room object references

To start scripting, you need references to the object in your room. To do that, simply name the variable that will hold the reference to the object. It will automatically be linked to every Script and you can use it in the code.

Script Variable Name

Here we can see a key with variable name “MainKey”.

Arrays

You can create arrays of room objects. To do that, after the name, add {x} where x is number in the array. E.g. 3 items with “Script variable name” defined as “Key{1}”, “Key{2}”, “Key{3}” will create an array (or Table in Lua) of 3 items. “Key{1}” will be the first element in the array and “Key{3}” will be the third. In the code, access the elements using indexer “[]” e.g. “Key[1]”, “Key[2]” and “Key[3]” will return “Key{1}”, “Key{2}” and “Key{3}” respectively.

Custom lua variables

Sometimes you will have to define a variable in the code and use it throughout the playtime. To define such a variable, simply name the variable and set its value in the LuaCallType.Init chunk of code. Then you can use it in other places as well.

Custom variables

Here we can see a time variable that is incremented every time an “Arrow” switch is done. When the variable is 2, it activates another switch.

Built-in variables & Events

When an event is fired from the game and before the lua script is called, additional variables are set. Most of the variables are the same for every event, but there are some differences. We have next events and variables:

Switch & Lock

Most of the logic items have Switch or Lock in them, but are masked so it is easier for the creators to use them.

The objects that are actually Switch and can be used in SwitchStarted and SwitchDone are:

The objects that are actually Lock and can be used in Unlock are:

api object

At any time you have access to a special object: api. It is a bridge between our internal code and your lua code. It holds utility functions that can be used to help you write code and to add more features to your level.

Functions of api object:

Types

In the code, you can acces all the variables of any object. Here are the variables of exposed types:

Switch

Lock

Slot

Trigger

Element

Pointer

ActivatorComponent