How do you get "fort_character" from type "?agent"

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:```

Solved this by doing the following…

  • Created a new const “Agent” of type “agent”
  • Set “Agent” equal to “QAgent?”
  • Called “GetFortCharacter[]” on the new const “Agent”

Looks like the question mark at the end handles the “false” check

OnTriggered(QAgent: ?agent): void=
  if (QAgent <> false, Agent: agent = QAgent?):
  
      if (FortChar := Agent.GetFortCharacter[]) {
          Print("Killing player")
          FortChar.Damage(100.0)
      }
1 Like

Hey @SushiAi,

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[]):
6 Likes

@SushiAi how do you get an agent type from a fort_character? i tried this but it wont work.

OnPlayerDamageChair(DamageResult : damage_result)<suspends>: void =
        Print("OnPlayerDamageChair activated")

        if:
            Instigator := DamageResult.Instigator?
            Agent := Instigator.GetInstigatorAgent[]  
            ChairGun.IsHoldingItem[Agent]
            Target := fort_character[DamageResult.Target]


        then:
            Print("then activated")

            if(TargetAgent:agent = agent[Target]):
                MyChair.Seat(Agent)
                OtherChair.Seat(TargetAgent)