How to hold an agent variable in class variable?

Hi folks,

I explain what I want to achieve at first. I have a volume in the level. When the player enters into it, it will start a timer. When the timer is completed, I would like to let the player win the game.

Firstly, I define a class to hold the volume and timer

gameflow_management_device := class(creative_device):
    @editable
    EvacuationTimer : timer_device = timer_device{}

    @editable
    EvacuationZone : mutator_zone_device = mutator_zone_device{}

And this class subscribes volume’s AgentEntersEvent. When the event is triggered, I tried to hold the agent for the future use. But It seems that I can not hold it in gameflow_management_device class because there is no pointer or reference of C++ in verse, and agent is epic internal class, so I can not declare an agent variable in class either.

Then I tried alternative way. I use agent to start the timer. When timer’s complete event is triggered, so I can get the agent from handler’s argument:

HandleEvacuationTimerComplete(Agent:?agent):void=

But I do not know how to convert ?agent to an agent with failable context.

I am not sure if I implement the idea in the right way. Any idea?

Cheers

Instead of using Agent, maybe convert to/from the fort_character type and store that, this definitely works. You can use Agent.GetFortCharacter[] and FortCharacter.GetAgent[].

Hi @cherlix,

The ?agent type represents an “optional agent”. In regular verse code variables must have a value. For example, if the type is agent you can assume that value is always a valid agent. The optional type means that this could be an agent or it could be false. To check this you can write this:

# Check if Agent is valid. If it is, assign it to ValidAgent.
if (ValidAgent:agent = Agent?):
    DoThingWithAgent(ValidAgent)

# Shorter version of the same code. ValidAgent being type :agent is 
# automatically done by the Verse compiler
if (ValidAgent := Agent?):
    DoThingWithAgent(ValidAgent)
1 Like

Thanks for the information. I will try it out.

“The left hand side of this definition is an expression that cannot be defined” i got that error