Defer is a control flow expression that executes a block of code only after the current scope exits. For example, Defer is used in a function so that its code block will only execute after the function completes or is canceled.
Defer is useful for resetting variables and cleaning up used resources. It's also a good option for placing code that needs to be executed at the end of a long function with many different termination locations. This way, the code doesn't need to be repeated everywhere.
In the example below, PropMove is a creative_prop and InitialTransform is a transform (position, rotation and scale).
defer:
if ( PropToMove.TeleportTo[ InitialTransform ] ) {}
The TeleportTo function doesn't execute immediately. It only executes when the current scope exits. The goal is to return the creative prop to its initial position.
The if expression is necessary because the TeleportTo function is a failable expression.
Let's create an example to illustrate this use of the Defer expression. In this example, a creative prop will move toward the player. The player must activate a trigger to stop the pursuit. The creative prop will return to its starting position when deactivated.
In any UEFN project, open Verse Explorer, right-click on the project name and choose the Add new Verse file to project option.
In Device Name put defer_device and click the Create Empty button.
Copy the Verse code below into the defer_device file:
using { /Fortnite.com/Devices }
using { /Fortnite.com/Characters }
using { /Fortnite.com/Playspaces }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /UnrealEngine.com/Temporary/SpatialMath }
defer_device := class(creative_device):
@editable
PropToMove : creative_prop = creative_prop{}
@editable
Trigger : trigger_device = trigger_device{}
OnBegin<override>()<suspends>:void=
race:
FollowPlayer()
Trigger.TriggeredEvent.Await()
FollowPlayer()<suspends>:void=
if:
Player := GetPlayspace().GetPlayers()[0]
Character := Player.GetFortCharacter[]
then:
InitialTransform := PropToMove.GetTransform()
PropRotation := PropToMove.GetTransform().Rotation
defer:
# Return to starting position when exiting scope (cancel or complete)
if ( PropToMove.TeleportTo[ InitialTransform ] ) {}
loop:
NewTranslation := Character.GetTransform().Translation
PropToMove.MoveTo( NewTranslation, PropRotation, 5.0 )
The OnBegin function has only one race expression with two concurrent expressions. When the trigger is activated, the FollowPlayer() function will be canceled and the expression associated with defer will be executed.
In the FollowPlayer() function, we need to get the reference to the first player's FortCharacter instance to get its current position. The expression GetPlayspace().GetPlayers() returns an array with the players. Since we only want the first player, the index [0] was used to get the first element of the array returned by the GetPlayers() function.
Save the file and compile the Verse code using the Verse > Build Verse Code option from the UEFN menu.
Access the Content Drawer and add our defer_device to the level. Add a Trigger Device away from the player's starting position. We need a creative_prop that will be used to track the player. In my example, I used Gnome02, but you can choose any.
Select the defer_device in the level. In the Details tab, add the creative prop and trigger references.
Save the level and click the Launch Session button to load it into Fortnite. Watch the creative prop move toward your character. Step over the trigger to stop the chase and return the creative prop to its starting position.