# I want to increment based on subscribe event

**URL:** <https://forums.unrealengine.com/t/i-want-to-increment-based-on-subscribe-event/2321458>\
**Category:** Verse\
**Tags:** unreal-editor-for-fortnite, fortnite-creative, question, fortnite\
**Created:** [February 7, 2025, 11:34am UTC](https://forums.unrealengine.com/t/i-want-to-increment-based-on-subscribe-event/2321458 "2025-02-07T11:34:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DogOnAKeyboard13](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DogOnAKeyboard13](https://forums.unrealengine.com/u/DogOnAKeyboard13)\
**Post date:** [February 7, 2025, 11:34am UTC](https://forums.unrealengine.com/t/i-want-to-increment-based-on-subscribe-event/2321458/1 "2025-02-07T11:34:50Z")

</div>

Hi all, back with another late night coding question~

I’m trying to figure out a way to track eliminations of NPC characters, and I thought to use a subscribe event on the npc\_spawner\_device. Like this:

> if (npc\_currentSpawnDevice := npc\_spawn\_listShuffled[index\_npcDevice]):  
> npc\_currentSpawnDevice.Spawn()  
> npc\_currentSpawnDevice.EliminatedEvent.Subscribe(OnNPCEliminated)

(pretend the two lines after IF are indented, the block quote formatting removed spaces)

then later after OnBegin function ends

> OnNPCEliminated(idkWhatThisThingIs : device\_ai\_interaction\_result) : void =  
> # increment number of NPCs that are eliminated this wave  
> Print(“NPC has been eliminated”)  
> npc\_eliminated\_this\_wave += 1 # would want to increment a variable from outside the function here

(lines after function OnNPCEliminated are indented)

I’m unsure how to increment or collect a value from the function that gets called in the Subscribe event. Is there a way to return a result? Would Handlers help at all here or do they only pass information into the function and not out from the results of the function?

Maybe something with [cancelable results](https://dev.epicgames.com/documentation/en-us/uefn/coding-device-interactions-in-verse) would help?

Or maybe some other approach?

Any help would be appreciated!

---

<div class="post-metadata">

**Author:** ![Loups.Idlez](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Loups.Idlez](https://forums.unrealengine.com/u/Loups.Idlez)\
**Post date:** [February 7, 2025, 3:00pm UTC](https://forums.unrealengine.com/t/i-want-to-increment-based-on-subscribe-event/2321458/2 "2025-02-07T15:00:00Z")

</div>

```
 ## Hey, well you can try something like this:

```

npc\_spawn\_listShuffled : []npc\_spawner\_device = array{}  
var npc\_eliminated\_this\_wave : int = 0

OnBegin():void=  
– set npc\_eliminated\_this\_wave : int = 0  
– for (npcDevice : index\_npcDevice):  
– - - if (npc\_currentSpawnDevice := npc\_spawn\_listShuffled[npcDevice]):  
- - — - npc\_currentSpawnDevice.Spawn()  
- - — - npc\_currentSpawnDevice.SpawnedEvent.Subscribe(OnNPCSpawned)  
- - — - npc\_currentSpawnDevice.EliminatedEvent.Subscribe(EliminateNPC)

EliminateNPC(result : device\_ai\_interaction\_result): void=  
– Print(“NPC has been eliminated”)  
– set npc\_eliminated\_this\_wave += 1

OnNPCSpawned(NPCAgent : agent): void=  
– Print(“NPC Spawned”)

```
    ## Here you have device_event_ai_interaction info from documentation, you can get for 
           instance killer info from this or specific npc

```

#Payload of `device_event_ai_interaction`.  
device\_ai\_interaction\_result := struct\<epic\_internal\>:  
# Optional agent that triggered the interaction  
Source:?agent

#Optional agent targeted by `Source`.  
Target:?agent

```
## Cancelables are used to cancel your Subscriptions

```

---

<div class="post-metadata">

**Author:** ![DogOnAKeyboard13](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DogOnAKeyboard13](https://forums.unrealengine.com/u/DogOnAKeyboard13)\
**Post date:** [February 8, 2025, 6:20am UTC](https://forums.unrealengine.com/t/i-want-to-increment-based-on-subscribe-event/2321458/3 "2025-02-08T06:20:03Z")

</div>

Hi thanks for the response!

I think I found how to reference a variable, just as you have `set npc_eliminated_this_wave += 1` and with the variable created outside of OnBegin.

I think I was wondering also, this seems like we make the variable global in some scope so the function can access it without having it passed as a parameter, if that makes sense? I didn’t know if this was bad practice, usually I was taught to avoid setting up global variables. At least we can consider it global in the scope of our creative device verse script, right?

I’m still trying to wrap my head around the canceling subscription thing. Is it like we should aim to do that after every subscribe, for example if we have a game that continues round after round and we want to reset those subscribed events?

My code is working for now, in incrementing and decrementing the NPC number. 🙂

---

<div class="post-metadata">

**Author:** ![Loups.Idlez](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Loups.Idlez](https://forums.unrealengine.com/u/Loups.Idlez)\
**Post date:** [February 9, 2025, 10:44am UTC](https://forums.unrealengine.com/t/i-want-to-increment-based-on-subscribe-event/2321458/4 "2025-02-09T10:44:51Z")

</div>

Hey,  
I’m not professional myself, but I think that’s okey to set variables in that way.  
Since I’ve started my journey with uefn and verse I had to use cancelables only once. In your case I don’t think that you need it. You will have to use them to avoid double subscriptions, I’m not sure how ot works in “Rounds” but I think that they are restarted. You can always check with prints.

---

<div class="post-metadata">

**Author:** ![im\_a\_lama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/im_a_lama/32/995063_2.png) [@im\_a\_lama](https://forums.unrealengine.com/u/im_a_lama)\
**Post date:** [February 10, 2025, 9:16am UTC](https://forums.unrealengine.com/t/i-want-to-increment-based-on-subscribe-event/2321458/5 "2025-02-10T09:16:08Z")

</div>

Exactly, the variable is defined at class scope (so creative\_device scope) which is a good practice, in fact if you were to define the variable at function scope, it would get erased upon leaving that function, which is not what you want  
Defining a variable at module scope (e.g. above the creative\_device) is tricky to do, because it will require you to use weak\_maps, but you won’t need it until you need it, you’ll know eventually

> [@DogOnAKeyboard13](#):
>
> this seems like we make the variable global in some scope so the function can access it without having it passed as a parameter

If you ever were to pass the variable as a parameter, only the value of the variable would be copied over to that parameter, so you wouldn’t be able to change the value of that variable if that makes sense

> [@DogOnAKeyboard13](#):
>
> I’m still trying to wrap my head around the canceling subscription thing. Is it like we should aim to do that after every subscribe, for example if we have a game that continues round after round and we want to reset those subscribed events?

If you’re using fortnite native rounds system, everything in your map will reset correctly, no need to clean anything. Also in your case you don’t want it to cancel during the round since you’d need to keep that elimination handling script going, so I guess it’s fine to keep it

---

<div class="post-metadata">

**Author:** ![DogOnAKeyboard13](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DogOnAKeyboard13](https://forums.unrealengine.com/u/DogOnAKeyboard13)\
**Post date:** [February 14, 2025, 5:52am UTC](https://forums.unrealengine.com/t/i-want-to-increment-based-on-subscribe-event/2321458/6 "2025-02-14T05:52:40Z")

</div>

Thanks so much! And yeah, I’m doing rounds but the structure of my lists are arrays for the most part (like arrays of teleporters, arrays of trigger devices, arrays of guard / npc spawners). So when I loop over them, they should only be subscribed to once to cover all of them. That’s good to know about the double subscription thought, I’ll keep that in mind!

---

<div class="post-metadata">

**Author:** ![DogOnAKeyboard13](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DogOnAKeyboard13](https://forums.unrealengine.com/u/DogOnAKeyboard13)\
**Post date:** [February 14, 2025, 5:57am UTC](https://forums.unrealengine.com/t/i-want-to-increment-based-on-subscribe-event/2321458/7 "2025-02-14T05:57:12Z")

</div>

Ah many thanks for the clarifying! That helps a lot 😃

Oh yeah I came from a C++ background back in the day, so I remember passing function parameters by reference (where changing that parameter actually changes the original value where it’s stored). It’s honestly been a while since I picked up coding and messed around with it. Right, normally passing a parameter makes a copy of the value of the variable being passed into the parameter variable, which is separate.

> If you’re using fortnite native rounds system, everything in your map will reset correctly, no need to clean anything. Also in your case you don’t want it to cancel during the round since you’d need to keep that elimination handling script going, so I guess it’s fine to keep it

And ok gotcha! This is all really helpful to know, thank you so much!
