# How do I grab the instigator of AgentEntersEvent in a function?

**URL:** <https://forums.unrealengine.com/t/how-do-i-grab-the-instigator-of-agententersevent-in-a-function/1163212>\
**Category:** Verse\
**Tags:** unreal-engine, unreal-editor-for-fortnite, fortnite-creative, characters, question, fortnite\
**Created:** [May 12, 2023, 4:42am UTC](https://forums.unrealengine.com/t/how-do-i-grab-the-instigator-of-agententersevent-in-a-function/1163212 "2023-05-12T04:42:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Krosis](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/krosis/32/699183_2.png) [@Krosis](https://forums.unrealengine.com/u/Krosis)\
**Post date:** [May 12, 2023, 4:42am UTC](https://forums.unrealengine.com/t/how-do-i-grab-the-instigator-of-agententersevent-in-a-function/1163212/1 "2023-05-12T04:42:45Z")

</div>

Plz no judgement i have very little experience with code, I can stutter my way through html and css, and the best I can do is a console calculator in c++…

What I’m trying to make is Basically a mutator zone, which, every 2 seconds heals the player inside it with 45 health. What I’m struggling with is the last part of the code (what any experienced programmer would probably notice). I originally wanted to write this like this:

```auto
Heal(Agent : agent) : void =
Instigator : agent = HealingZone.GetInstigatorAgent()
        HealthPowerup.Pickup(Instigator)

```

but then I found some simmilar forum question, and i tried applying the responses to it in my own code, but it still doesnt work.

```auto
Heal(Agent : agent) : void =
        if (Agent := Heal.GetInstigatorAgent[]):
            HealthPowerup.Pickup(Agent)

```

I tried every possible combination i could think of haha (like HealingZone.AgentEntersEvent.GetInstigatorAgent() and so on), but I honestly have no idea what I’m doing. I took the time to learn the very basics of this language, and I got that, but only that.

**Here’s my full code for more context:**

```auto
using { /Fortnite.com/Playspaces }
using { /Fortnite.com/Characters }
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
# TODO: Heal players inside the zone 45 health per 2 seconds, excluding hero classes
        

medical_droid_device := class(creative_device):
    #A zone, within all Players will receive health
    @editable
    HealingZone : mutator_zone_device = mutator_zone_device{}
    #the poweup, which wil be activated for players inside the zone
    @editable
    HealthPowerup : health_powerup_device = health_powerup_device{}

    OnBegin<override>()<suspends>:void=
        HealingZone.AgentEntersEvent.Subscribe(Heal)
    
    Heal(Agent : agent) : void =
        if (Agent : agent := HealingZone.GetInstigatorAgent[]):
            HealthPowerup.Pickup(Agent)

```

**Here’s my errors**

 ![errors](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/4/c/f4cbf1f191da5ef1333be73d9ddffa9d3882ccdf.png)

and if anyone would be kind enough, I also need to make a loop with sleep 2, to make the every 2 seconds healing thing, but I have no idea how can I make the AgentExitsEvent break the loop? do i cal it in the loop? On begin? (this came to my mind as of writing this) can I write:

```auto
loop:
HealingZone.AgentExitsEvent.Subscribe(break)
#heal
#sleep

```

?  
I’m asking because I cant check it right now. thanks to anyone who takes the time to read this and responds. I just really wanna learn this language

---

<div class="post-metadata">

**Author:** ![Knight\_Breaker](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/knight_breaker/32/1194711_2.png) [@Knight\_Breaker](https://forums.unrealengine.com/u/Knight_Breaker)\
**Post date:** [May 12, 2023, 7:05am UTC](https://forums.unrealengine.com/t/how-do-i-grab-the-instigator-of-agententersevent-in-a-function/1163212/2 "2023-05-12T07:05:57Z")

</div>

Not sure what I’m overlooking in your main question here.

Re: instigator:

The docs says this:

```auto
# Signaled when an `agent` enters this zone.
# Sends the `agent` entering this zone.
AgentEntersEvent<public>:listenable(agent) = external {}

```

The important part is “Sends the `agent` entering this zone.”.

Therefore here’s your instigator `agent`.

```auto
Heal(Agent: agent): void = { ... }
   # ^ ~~~~~~~~~~~

```

Re: healing.

- Create a custom data structure that tracks it per instigator (probably `fort_character` after you obtained it from the `agent`). That could be `var TaskMap: [fort_character]?task() = map {}`
- there’s no explicit cancellation in verse yet, so you have to workaround it, add another map that tracks the active healing state `var FlagMap: [fort_character]logic = map {}`
- when your player enter the zone, spawn a new async task which would loop the healing, but you also have to add a way to break it

Quick pseudocode:

```auto
Heal(Agent: agent): void = {
  ForChar := # obtain `fort_character` from `agent`
  # set the healing flag
  if (set FlagMap[ForChar] = true) {}

  # create a new task, NOTE, the previous task must be cancelled by now, that's
  # up to you to guarantee, or you will double heal
  Task := spawn { HealLoopForChar(ForChar) }
  # save it
  if (set TaskMap[FortChar] = option { Task }) {}
}

HealLoopForChar(FortChar: fort_character)<suspends>: void = {
  loop {
    # check if the flag is rest, to break the loop
    if (ShouldHeal := FlagMap[FortChar], not ShouldHeal?) {
      break
    }

    # heal `FortChar`
   
    # sleep 2 seconds until next iteration
    Sleep(2.0)
  }
}

```

- when your player exists the zone just reset `FlagMap` for your instigator player to `false`

```auto
StopHeal(Agent: agent): void = {
  FortChar := # obtain `fort_character` from `agent`
  # reset the healing flag
  if (set FlagMap[FortChar] = false) {}

  # Throw away the task
  if (set TaskMap[FortChar] = false) {}
}

```

Note: All of this is written from memory, not tested, not compiled, could contain typos. You’ll figure out the rest. 😉
