I’m subscribing to a “trigger_device” TriggeredEvent and it asks for type “?agent” in params. How do I get a “fort_character” from this?
VSCode is giving me a squiggly under the “GetFortCharacter” square brackets saying this:
“This function parameter expects a value of type tuple(agent,tuple(),tuple()), but this argument is an incompatible value of type tuple(?agent,tuple(),tuple()).(3509)”
@editable
Trigger: trigger_device = trigger_device{}
# Runs when the device is started in a running game
OnBegin<override>()<suspends>:void=
Trigger.TriggeredEvent.Subscribe(OnTriggered)
OnTriggered(Agent: ?agent): void=
if (Agent <> false, FortChar: fort_character = Agent.GetFortCharacter[]):
block:```
You don’t actually need the QAgent <> false check in here. The second part of your code already performs that part of the check with the ? operator.
There are also a couple tricks that you can use to shrink the code more if you wanted. Posting them here to spread some knowledge.
# You can roll up the two ifs into one to save some indentation
if (Agent := QAgent?; FortChar := Agent.GetFortCharacter[])
# You can also use the ? with the GetFortCharacter call. This will only pass if QAgent has an agent and GetFortCharacter succeeds.
if (FortChar := QAgent?.GetFortCharacter[]):