This script demonstrates how to keep a reference to a player.
Additionally it demonstrates how to use a function marked and spawn it as a coroutine. This is used in this case to work around an issue where calling functions during the PlayerSpawnedEvent causes the function to fail. In this example, SpawnItem() on a item_spawner_device. If done from a coroutine that waits one frame, it will work. This will be fixed in future releases.
using { /Fortnite.com/Devices }
using { /Verse.org/Native }
using { /EpicGames.com/Temporary/Diagnostics }
using { /Verse.org/Simulation }
#var ScriptPlayerReference
# This is a new Verse based class that is used with the ScriptDevice in UEFN
#
# Getting started: <link to UEFN docs>
player_reference := class<concrete>(creative_device):
@editable
MyPlayerSpawnPad:player_spawner_device := player_spawner_device {}
@editable
MyItemSpawner:item_spawner_device := item_spawner_device{}
# Getting a player reference
var PlayerScriptReference:?player = false
# Runs when this device_script is started in a running game
OnBegin<override>()<suspends>:void=
MyPlayerSpawnPad.PlayerSpawnedEvent.Subscribe(HandlePlayerSpawned)
HandlePlayerSpawned(SpawnedPlayer:player):void=
set PlayerScriptReference = option { SpawnedPlayer }
# If you call this here, the Item Spawner will not spawn the item.
# MyItemSpawner.SpawnItem(SpawnedPlayer)
# The following is a TEMPORARY workaround and you will not need to use this in the future
# Delayed Spawn
spawn { HandlePlayerSpawnedAsync() }
HandlePlayerSpawnedAsync()<suspends>:void=
# This is putting this function to sleep for at least 1 frame
Sleep(0.0)
if (ValidatedPlayerRef := PlayerScriptReference?):
MyItemSpawner.SpawnItem(ValidatedPlayerRef)