Hello @Hans_Schrodder how are you?
To do this, you will need to create a new custom device. The device will store a series of Switch Devices in an editable array, and the order in which you populate that array will determine the order in which the player will need to activate each Switch to be teleported.
Internally, the Switch Devices will also be stored in a “map” to link them to an ID (an int in this case). This needs to be done so we can track which devices has already been activated and store that state in a new “map”, which will be accessed using that ID. We will also use a “Current Step” variable counter, which will increase if the player activates the right Switch. If the player activates the wrong switch, all the progress will be reset.
Lastly, we will utilize a Teleporter Device to specify the location to which the player will be teleported if all the switches are activated in the right order.
Here is the detailed explanation with everything you will need:
- First of all, as shown in this video (I highly recommend you to watch the whole video first), create a new Verse file with this code:
using { /Verse.org/Simulation }
# Code from Unreal documentation
(Listenable : listenable(agent)).SubscribeAgent(OutputFunc : tuple(agent, t)->void, ExtraData : t where t:type) : cancelable =
Wrapper := wrapper_agent(t){ExtraData := ExtraData, OutputFunc := OutputFunc}
Listenable.Subscribe(Wrapper.InputFunc)
wrapper_agent(t : type) := class():
ExtraData : t;
OutputFunc : tuple(agent, t) -> void
InputFunc(Agent : agent):void = OutputFunc(Agent, ExtraData)
(Listenable : listenable(tuple())).SubscribeEmpty(OutputFunc : t -> void, ExtraData : t where t:type) : cancelable =
Wrapper := wrapper_empty(t) {ExtraData := ExtraData, OutputFunc := OutputFunc}
Listenable.Subscribe(Wrapper.InputFunc)
wrapper_empty(t : type) := class():
ExtraData : t;
OutputFunc : t -> void
InputFunc():void = OutputFunc(ExtraData)
- Then you can start with the main logic for the feature, this is the full commented code:
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /Verse.org/Random }
using { /UnrealEngine.com/Temporary/Diagnostics }
# Device that handles the sequential switch system
teleport_switch_sequence := class(creative_device):
# Array of all the switches in the scene (assigned in the editor)
@editable
AllSwitches : []switch_device = array{}
# Map to link the corresponding Switch Devices to an ID (int)
var SwitchesMap : [int]switch_device = map{}
# Map to track the ON/OFF state of each switch
var SwitchStates : [int]logic = map{}
# Auxiliary counter for assigning IDs to switches
var mapCounter : int = 0
# Current expected switch index in the sequence
var CurrentStep : int = 0
# Teleporter device where the player will be teleported upon completing the code
@editable
TeleportDevice : teleporter_device = teleporter_device{}
OnBegin<override>()<suspends> : void =
# Populate the SwitchesMap with switches from AllSwitches
InitializeSwitchesMap()
# Initialize all switches states to OFF (false)
InitializeSwitchStates()
# Adds all switches from AllSwitches into SwitchesMap and subscribes to their events
InitializeSwitchesMap():void=
for ( Element : AllSwitches , SwitchDevice := switch_device[Element] ):
if (set SwitchesMap[mapCounter] = SwitchDevice , CurrentSwitch := SwitchesMap[mapCounter]):
# Subscribe to TurnedOn event, passing the switch ID as context
CurrentSwitch.TurnedOnEvent.SubscribeAgent(OnSwitchTurnedOn, mapCounter)
# Subscribe to TurnedOff event, passing the switch ID as context
CurrentSwitch.TurnedOffEvent.SubscribeAgent(OnSwitchTurnedOff, mapCounter)
# Increment the counter for next switch ID
set mapCounter = mapCounter+1
# Sets all switch states to false (OFF)
InitializeSwitchStates() : void =
for (I:= 0..SwitchesMap.Length-1):
if (set SwitchStates[I] = false):
# Handler called when a switch is turned ON
# Agent is the triggering agent, ActivatedSwitch is the switch ID
OnSwitchTurnedOn(Agent : agent, ActivatedSwitch : int) : void =
# Check if the activated switch matches the expected step in the sequence
if (ActivatedSwitch = CurrentStep):
# Advance to the next step
set CurrentStep = CurrentStep + 1
# If the sequence is complete
if (CurrentStep >= SwitchesMap.Length):
OnSequenceCompleted(Agent)
else:
# Reset the sequence if the wrong switch was activated
ResetSequence(Agent)
# Handler called when a switch is turned OFF
OnSwitchTurnedOff(Agent : agent, ActivatedSwitch : int) : void =
# Update the switch state to false
if (set SwitchStates[ActivatedSwitch] = false):
# Reset the entire sequence
ResetSequence(Agent)
# Called when the full sequence is successfully completed
OnSequenceCompleted(Agent : agent) : void =
# Attempt to teleport the agent using the TeleportDevice
TeleportDevice.Teleport(Agent)
# Reset the sequence after completion
ResetSequence(Agent)
# Resets the sequence progress and turns off all switches
ResetSequence(Agent : agent) : void =
# Reset the expected step to the start
set CurrentStep = 0
# Iterate over all switches and turn them off
for (Index -> SwitchDevice : AllSwitches):
SwitchDevice.TurnOff(Agent)
if(set SwitchStates[Index] = false):
-
Place the new device on the Island.
-
Place all the Switch Devices on the Island.
-
Place the Teleporter Device on the destination.
-
Add the Switch Devices and Teleport Device to the custom device.
And that’s it! This should work as you wanted. Remember that you can add as many Switch Devices as you want and that the order you add them to the custom device determines the order the player needs to activate them!
Here is a video of how it works:
Let me know if this helps you!