Switches and buttons are common elements in games. Typically these are used to open doors, call elevators, or turn on machines. They can also be used to set off a more complex chain of events or a scripted sequence. Leadwerks provides a moveable switch object that can be used in your games. By attaching a script to the object and connecting it in the flow graph, we can provide a mechanism in our game level the player can interact with.
The SwitchThe game starts with the FPS character. We want to open the gate via the switch hanging on the wall. The handle of the switch rotates when the switch is used.
Locate the switch in the scene tab. You can see that the switch exists out of a base and a handle.
The SwingingDoor script is attached to the handle. When we go to the script tab you can see that the handle rotates on a specific axis. The door has the sliding door script attached.
Let's PlayWhen we start the game we walk towards the switch. When we are close to the, the hand icon appears.
Press E to interact with it. The gate opens and the switch handle rotates 90 degrees.
Interact again and door closes.
Flowgraph EditorGo to the flowgraph editor. As you can see there are two nodes: SlidingDoor and SwingingDoor. The swingingdoor is used for the switch and the SlidingDoor for the Gate/Door.
We can also use toggle which executes the action that is needed to revert its current state.
It will do do the exact same thing.
You can see the following connections are set up:
- We are closing the switch, we close the door.
- We are opening the switch, open the door.