Get Player From Agent
get player from agent
A diver steps on the sunken trigger device and a giant pearl flies into their inventory. But when a friendly reef dolphin tries it, absolutely nothing happens.
The trigger device hands us a generic agent, which could be a player or an NPC. We use a failable type cast to verify they are a real human player before granting any loot.
First, we subscribe to the trigger and unwrap the optional agent. Then, we use the player bracket syntax to safely narrow that agent down to a true player.
Reach for this failable cast whenever a device gives you an agent, but you need to access player-only data. It is the safest way to filter out bots and NPCs from your game logic.
reef_device := class(creative_device):
@editable
PearlPlate : trigger_device = trigger_device{}
OnStepped(MaybeAgent : ?agent) : void =
if (Agent := MaybeAgent?):
if (Player := player[Agent]):
Print("Pearl granted!")
OnBegin<override>()<suspends>:void =
PearlPlate.TriggeredEvent.Subscribe(OnStepped)
Build your own lesson with get-player-from-agent
Generate a personalized, compile-checked lesson grounded in this exact reference.
Build a lesson →