# Using same event handler for multiple events. How do I differantiate the event sources?

**URL:** <https://forums.unrealengine.com/t/using-same-event-handler-for-multiple-events-how-do-i-differantiate-the-event-sources/1504454>\
**Category:** Verse\
**Tags:** unreal-engine, unreal-editor-for-fortnite, fortnite-creative, question, fortnite\
**Created:** [December 8, 2023, 1:21am UTC](https://forums.unrealengine.com/t/using-same-event-handler-for-multiple-events-how-do-i-differantiate-the-event-sources/1504454 "2023-12-08T01:21:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Enebz](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/enebz/32/875411_2.png) [@Enebz](https://forums.unrealengine.com/u/Enebz)\
**Post date:** [December 8, 2023, 1:21am UTC](https://forums.unrealengine.com/t/using-same-event-handler-for-multiple-events-how-do-i-differantiate-the-event-sources/1504454/1 "2023-12-08T01:21:21Z")

</div>

I am trying to use several switches in one device. The amount of switches is dynamically set from UEFN.

How can I register all the switches to one specific handler function, and recognize WHICH SWICH got changed?

This code makes the switches use the same handler, but I would not know which switch that got it’s state changed.

```auto
settings_playerhealth_device := class(creative_device):

    @editable
    HealthSwitches: []switch_device = array{}

    OnBegin<override>()<suspends>:void=
        for (HealthSwitch : HealthSwitches):
            # Register event handler for each switch
            HealthSwitch.StateChangesEvent.Subscribe(HealthSwitchStateChange)

    HealthSwitchStateChange():void=
        Print("HealthSwitchStateChange for ???")

```

How can I do this?

---

<div class="post-metadata">

**Author:** ![Ep8Script](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ep8script/32/1170494_2.png) [@Ep8Script](https://forums.unrealengine.com/u/Ep8Script)\
**Post date:** [December 8, 2023, 5:23am UTC](https://forums.unrealengine.com/t/using-same-event-handler-for-multiple-events-how-do-i-differantiate-the-event-sources/1504454/2 "2023-12-08T05:23:25Z")

</div>

```auto
settings_playerhealth_device := class(creative_device):

    @editable
    HealthSwitches: []switch_device = array{}

    OnBegin<override>()<suspends>:void=
        for (Index->HealthSwitch : HealthSwitches):
            spawn {AwaitSwitchStateChange(HealthSwitch, Index)}

    AwaitSwitchStateChange(HealthSwitch:switch_device, Index:int)<suspends>:void=
        loop:
            HealthSwitch.StateChangesEvent.Await()
            Print("Health switch at index {Index} changed")

```

---

<div class="post-metadata">

**Author:** ![GraemeBB](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/graemebb/32/1106150_2.png) [@GraemeBB](https://forums.unrealengine.com/u/GraemeBB)\
**Post date:** [December 8, 2023, 5:25am UTC](https://forums.unrealengine.com/t/using-same-event-handler-for-multiple-events-how-do-i-differantiate-the-event-sources/1504454/3 "2023-12-08T05:25:41Z")

</div>

This is an option as well:

> **[Wrapping Subscribe() to pass additional data to listeners | Uefn Code Snippet](https://dev.epicgames.com/community/snippets/d8k/fortnite-wrapping-subscribe-to-pass-additional-data-to-listeners)**
>
> This is for when you want to Subscribe() to a listenable but you want to pass along your own data. This works for callbacks that take \`agent\` and those ...

---

<div class="post-metadata">

**Author:** ![funbbtv](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@funbbtv](https://forums.unrealengine.com/u/funbbtv)\
**Post date:** [December 20, 2023, 6:23pm UTC](https://forums.unrealengine.com/t/using-same-event-handler-for-multiple-events-how-do-i-differantiate-the-event-sources/1504454/4 "2023-12-20T18:23:14Z")

</div>

Thanks and how do you know which player/agent trigger it?

---

<div class="post-metadata">

**Author:** ![ManualPotato](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/manualpotato/32/1144902_2.png) [@ManualPotato](https://forums.unrealengine.com/u/ManualPotato)\
**Post date:** [December 20, 2023, 7:20pm UTC](https://forums.unrealengine.com/t/using-same-event-handler-for-multiple-events-how-do-i-differantiate-the-event-sources/1504454/5 "2023-12-20T19:20:50Z")

</div>

I was going to suggest modifying the above code slightly to use a value returned from `StateChangesEvent.Await()` but that seems to return an empty tuple instead of the `agent` I expected. So to modify it it needs a bit of an awkward workaround but I think something like below would work. This could technically not have exactly the same functionality as `StateChangesEvent` but might still be what you want.

```auto
settings_playerhealth_device := class(creative_device):

    @editable
    HealthSwitches: []switch_device = array{}

    OnBegin<override>()<suspends>:void=
        for (Index->HealthSwitch : HealthSwitches):
            spawn {AwaitSwitchStateChange(HealthSwitch, Index)}

    AwaitSwitchStateChange(HealthSwitch:switch_device, Index:int)<suspends>:void=
        loop:
            InstigatorAgent := race:
                HealthSwitch.TurnedOffEvent.Await()
                HealthSwitch.TurnedOnEvent.Await()

            Print("Health switch at index {Index} changed")

```

I used the `TurnedOffEvent` and `TurnedOnEvent` which do return the instigator.

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [January 19, 2024, 7:21pm UTC](https://forums.unrealengine.com/t/using-same-event-handler-for-multiple-events-how-do-i-differantiate-the-event-sources/1504454/6 "2024-01-19T19:21:42Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
