Interactions 105: Locking a door

This series of interaction tutorials have focused on a door that we can open to take us to another scene:

Let's make things a little more complex now by locking it - so that the door won't open unless we find a key.

We'll cover the key part later, but for now - let's just lock the door.

To keep track of the door's "locked" state, we'll need a Bool variable. Bools can be either True or False - so one named "Is locked?" would be perfect for this situation.

Global (game-wide) and Local (scene-wide) variables can be defined in the Variables Manager, but we want this variable to be associated with the door itself. This'll also make it easier if we later wanted to prefab it.

Select the door and attach a Variables component to it. This can be found under Adventure Creator → Logic → Variables in the "Add Component" Inspector menu:

Click Create new Component variable, and a properties box for the new variable will appear. Set its Label to Is locked?, Type to Boolean, and Initial value to True.

We'll also want this variable's value to be stored in save-game files. To do that, just attach the Remember Variables component as well. That can be found in Adventure Creator → Save system → Remember Variables.

We now just need to stop the door from opening if it's locked. As part of the previous tutorials, we have a Hotspot that - when interacted with - runs the Red button_Use ActionList, which looks like this:

Viewing the Actions in the ActionList Editor, right-click in some empty space and add a new Variable: Check Action.

Unlike the other Actions here, this Action has two output sockets. Which socket is used will be based on the conditions we set. Set the Source to Component, and set the Component to the door. Since we only have one Variable defined, the rest will be filled out automatically:

The "condition" will be met if the Is locked? Variable is True. In this case, we don't want the door to open - so set the If condition is met field to Stop.

Conversely, if the variable is False, the door can open - so connect the If condition is not met socket up to the Object: Transform Action:

We now just need to make this new Action the one that runs first. To do this, click the cog icon to the top-right of it, and choose Move to front.

Things might get a little squeezed in the top-left corner, but we can neaten everything out again with the Auto-arrange button in the ActionList Editor's top toolbar. Things should now look like this:

Try running the scene now and interacting with the Hotspot. The door will no longer open. If you exit Play mode and update the Is locked? value to False, then you'll find that the door can open once again.

In the next tutorial, we'll create a simple puzzle that has us unlocking the door with an inventory item.