HandleItemPickedUp(Agent:agent):void=
if ( Player := player[Agent] ):
After casting, the player reference allows access to fields and methods that are specific to the player class.
Para ver o casting em ação, vamos criar um dispositivo que referencia um trigger device. Quando o trigger for acionado, vamos usar casting para testar se foi um player que ativou o trigger ou NPC. Se for um jogador, será ligado uma luz verde. Se for um NPC, será uma luz vermelha.
Open Verse Explorer, right-click on the project name, and select the option Add new Verse file to project.
In Device Name put agent_check_device and click the Create Empty button.
Copy the Verse code below into the agent_check_device file:
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
agent_check_device := class(creative_device):
@editable
TriggerDevice : trigger_device = trigger_device{}
@editable
GreenLight : customizable_light_device = customizable_light_device{}
@editable
RedLight : customizable_light_device = customizable_light_device{}
OnBegin<override>()<suspends>:void =
TriggerDevice.TriggeredEvent.Subscribe(OnTriggered)
OnTriggered(MaybeAgent : ?agent) : void =
if (Agent := MaybeAgent?):
GreenLight.TurnOff()
RedLight.TurnOff()
if (Player := player[Agent]):
GreenLight.TurnOn()
else:
RedLight.TurnOn()
The agent_check_device contains one trigger_device and two customizable_light_device.
In the OnTriggered function, the parameter is of type ?agent (option agent), since the TriggerDevice can be triggered by Verse code without any agent interaction. First, we check if it was triggered by an agent, and then determine whether the agent is a player or an NPC.
Save the file and compile the Verse code using the Verse > Compile Verse Code option from the UEFN menu.
Acess the Content Drawer e add the agent_check_device to the level. Then add one Trigger Device, two Customizable Light Devices and one Wildlife Spawner Device.
Select the Trigger Device at the level and check the option Triggered by Creatures.
Select one of the Customizable Light Devices, rename it to RedLight, set Light Color to red, and uncheck Initial State so the light starts off. Then rename the other light device to GreenLight and apply similar settings using green.
Select the agent_check_device in the level. In the Details panel, select the references to the devices added to the level.
We will use the Wildlife Spawner Device to spawn creatures in the level that can activate the Trigger Device when they pass over it. Select the device and, in Type, choose Random to spawn different types of creatures. Set Spawn Count to 10.
Save the level and click Launch Session to load it in Fortnite. Walk over the Trigger Device to activate the green light. Then try to lure a creature to walk over it and activate the red light.




