# Running code gets canceled after getting triggered multiple times

**URL:** <https://forums.unrealengine.com/t/running-code-gets-canceled-after-getting-triggered-multiple-times/684616>\
**Category:** Blueprint\
**Tags:** UE4-21, unreal-engine, UE4, question, Blueprint\
**Created:** [November 5, 2022, 2:40pm UTC](https://forums.unrealengine.com/t/running-code-gets-canceled-after-getting-triggered-multiple-times/684616 "2022-11-05T14:40:36Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![DarkAt26](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/darkat26/32/590464_2.png) [@DarkAt26](https://forums.unrealengine.com/u/DarkAt26)\
**Post date:** [November 5, 2022, 2:40pm UTC](https://forums.unrealengine.com/t/running-code-gets-canceled-after-getting-triggered-multiple-times/684616/1 "2022-11-05T14:40:36Z")

</div>

![Unbsfsfsfssfdsfsenannt](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/2/8/f288cbaa028db5dbf4e65dd14f35926ec1a4049f.png)

What is this code intended to do?  
I want this code to change the material of every object after 5 seconds that overlaps with the box collision.

Issue:  
If only one object overlaps it will get its material changed without any issues. But if another object overlaps before the code with the 5 second delay is fully run it will just stop running the code for the first object and instead start from the start with the second object. → **if multiple objects overlap at once only the material of the last object that overlapped will get its material changed.**

Question: How can i fix this issue?

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [November 5, 2022, 2:58pm UTC](https://forums.unrealengine.com/t/running-code-gets-canceled-after-getting-triggered-multiple-times/684616/2 "2022-11-05T14:58:19Z")

</div>

You need to add a stop guard in the form of a boolean.

- Just before the delay add a true / false branch and set it’s bool to a new boolean variable (you can name it isSettingMaterial).
- Have the bool isSettingMaterial start off as false.
- connect the branch to the beginning of of the overlay and then connect the delay to the false outcome.
- directly after the branch set the bool to true
- Once the set material is complete set isSettingMaterial to false

 ![lock](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/6/d/46d9a4d2209569d321de7869adbefecf79d62255.png)

---

<div class="post-metadata">

**Author:** ![pezzott1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/pezzott1/32/1215068_2.png) [@pezzott1](https://forums.unrealengine.com/u/pezzott1)\
**Post date:** [November 5, 2022, 3:04pm UTC](https://forums.unrealengine.com/t/running-code-gets-canceled-after-getting-triggered-multiple-times/684616/3 "2022-11-05T15:04:19Z")

</div>

Imo:

1. Create another actor that spawns when something overlaps. This new actor can store the timer and destroy itself when applies the material or…
2. Add overlapping actors to an array with the game time it overlapped. Loop every second or so if array is not empty, when game time \> added time + 5 apply material and remove from array.

---

<div class="post-metadata">

**Author:** ![DarkAt26](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/darkat26/32/590464_2.png) [@DarkAt26](https://forums.unrealengine.com/u/DarkAt26)\
**Post date:** [November 5, 2022, 3:14pm UTC](https://forums.unrealengine.com/t/running-code-gets-canceled-after-getting-triggered-multiple-times/684616/4 "2022-11-05T15:14:42Z")

</div>

Thanks for the reply but sadly it did not work. It will still only change the last object that overlapped.

---

<div class="post-metadata">

**Author:** ![DarkAt26](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/darkat26/32/590464_2.png) [@DarkAt26](https://forums.unrealengine.com/u/DarkAt26)\
**Post date:** [November 5, 2022, 3:31pm UTC](https://forums.unrealengine.com/t/running-code-gets-canceled-after-getting-triggered-multiple-times/684616/5 "2022-11-05T15:31:39Z")

</div>

Thanks for your reply too, yes that works to avoid that issue. I will wait a bit and see if anyone has a better solution before i change it to solved. ( While this method works its not that much of a satisfying solution. )

---

<div class="post-metadata">

**Author:** ![pezzott1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/pezzott1/32/1215068_2.png) [@pezzott1](https://forums.unrealengine.com/u/pezzott1)\
**Post date:** [November 5, 2022, 3:33pm UTC](https://forums.unrealengine.com/t/running-code-gets-canceled-after-getting-triggered-multiple-times/684616/6 "2022-11-05T15:33:14Z")

</div>

> [@DarkAt26](#):
>
> While this method works its not that much of a satisfying solution. )

In case I can be of further help: What would you consider a satisfying solution?

---

<div class="post-metadata">

**Author:** ![DarkAt26](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/darkat26/32/590464_2.png) [@DarkAt26](https://forums.unrealengine.com/u/DarkAt26)\
**Post date:** [November 5, 2022, 3:48pm UTC](https://forums.unrealengine.com/t/running-code-gets-canceled-after-getting-triggered-multiple-times/684616/7 "2022-11-05T15:48:04Z")

</div>

i would say a satisfying solution is one that (1.) doesnt spawn and deletes an actor everytime an object overlaps (the blueprint is for a vr game (PavlovVR) → must be really performant, there will be sometimes up to 100 objects overlapping at a time (the code i uploaded here is just the recreated issue im currently having. there is much more bp code in the actual bp i will be using in the end)) or a solution that (2.) doesnt run constantly loops even when not needed.

at least im expecting there to be something in ue4 that “fixes” these issues. like i could imagine: on overlap → create some event that runs async or something in that direction. i also saw that there are “Tasks” in ue4 but no idea how they work or what they do yet

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [November 5, 2022, 3:54pm UTC](https://forums.unrealengine.com/t/running-code-gets-canceled-after-getting-triggered-multiple-times/684616/8 "2022-11-05T15:54:52Z")

</div>

I miss-read that it was due to the code failing on second attempt. If you need fast and many calculations then yes an array approach would be good. But perhaps in the form of a static manager that would work through the array.  
It seems like a task for FRunnable

---

<div class="post-metadata">

**Author:** ![DarkAt26](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/darkat26/32/590464_2.png) [@DarkAt26](https://forums.unrealengine.com/u/DarkAt26)\
**Post date:** [November 5, 2022, 4:11pm UTC](https://forums.unrealengine.com/t/running-code-gets-canceled-after-getting-triggered-multiple-times/684616/9 "2022-11-05T16:11:13Z")

</div>

> [@3dRaven](#):
>
> yes an array approach would be good.

the main issue i see with doing that is that the delay time would add up.

> [@3dRaven](#):
>
> perhaps in the form of a static manager

what exactly do you mean with static manager? (i guess a whole blueprint itself that does it)

“FRunnable” i googled a bit and it seems like you have to use C++ to use it right? i cant use c++ / im only allowed to use blueprints so that would kick FRunnable out of my options.

i think the solution i will end up using is this one:

> [@pezzott1](#):
>
> Create another actor that spawns when something overlaps. This new actor can store the timer and destroy itself when applies the material or…

the main issue i could see with that is the spawning and destroying of the actors all the time (performance wise. im not sure how big the impact would be) but at least the delay time wouldnt add up.

i will wait a couple of hours and see if anyone has another idea and then close this question.

Thank you very much already. @3dRaven @pezzott1

---

<div class="post-metadata">

**Author:** ![pezzott1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/pezzott1/32/1215068_2.png) [@pezzott1](https://forums.unrealengine.com/u/pezzott1)\
**Post date:** [November 5, 2022, 4:12pm UTC](https://forums.unrealengine.com/t/running-code-gets-canceled-after-getting-triggered-multiple-times/684616/10 "2022-11-05T16:12:38Z")

</div>

There is a way we might be ably to with a single timer, not looping, have every random overlap execute a function 5 seconds after, but im not in front of my pc right now:

The idea is:

- On overlap store the pointer and game time.
- start the timer for 5 seconds
- if timer is active, on every overlap add pointer to array with game time + 5.
- when the timer executes, remove this index and check time of next element of array. If its \> current time, set new timer with difference.
- this will set a new timer until there are no more elements in array (meaning no more overlaps in last 5 seconds.

Each new timer should be the difference to execute in a total of 5 seconds of each overlap.

I can test and share BP in a few hours.

---

<div class="post-metadata">

**Author:** ![Naushad\_A](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Naushad\_A](https://forums.unrealengine.com/u/Naushad_A)\
**Post date:** [November 5, 2022, 4:24pm UTC](https://forums.unrealengine.com/t/running-code-gets-canceled-after-getting-triggered-multiple-times/684616/11 "2022-11-05T16:24:48Z")

</div>

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/b/f/6bf7f50ef715eb27023aaa5649638c577edd1780.png)  
you can try with this

---

<div class="post-metadata">

**Author:** ![DarkAt26](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/darkat26/32/590464_2.png) [@DarkAt26](https://forums.unrealengine.com/u/DarkAt26)\
**Post date:** [November 5, 2022, 5:46pm UTC](https://forums.unrealengine.com/t/running-code-gets-canceled-after-getting-triggered-multiple-times/684616/12 "2022-11-05T17:46:31Z")

</div>

I would love to see it 🙂

---

<div class="post-metadata">

**Author:** ![DarkAt26](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/darkat26/32/590464_2.png) [@DarkAt26](https://forums.unrealengine.com/u/DarkAt26)\
**Post date:** [November 5, 2022, 5:49pm UTC](https://forums.unrealengine.com/t/running-code-gets-canceled-after-getting-triggered-multiple-times/684616/13 "2022-11-05T17:49:31Z")

</div>

Im not sure what exactly you did there? I would assume that the “Change Material” Event/Function loops the “All Mesh” array to set the materials?

---

<div class="post-metadata">

**Author:** ![Naushad\_A](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Naushad\_A](https://forums.unrealengine.com/u/Naushad_A)\
**Post date:** [November 5, 2022, 5:52pm UTC](https://forums.unrealengine.com/t/running-code-gets-canceled-after-getting-triggered-multiple-times/684616/14 "2022-11-05T17:52:55Z")

</div>

yes a custom event which set material to all meshes through loop.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/6/7/667c163ddd69dcf1f8847323a0cf88a393139047.png)

---

<div class="post-metadata">

**Author:** ![DarkAt26](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/darkat26/32/590464_2.png) [@DarkAt26](https://forums.unrealengine.com/u/DarkAt26)\
**Post date:** [November 5, 2022, 5:57pm UTC](https://forums.unrealengine.com/t/running-code-gets-canceled-after-getting-triggered-multiple-times/684616/15 "2022-11-05T17:57:07Z")

</div>

This would only work once nothing new overlaps which would sadly create large delays.

---

<div class="post-metadata">

**Author:** ![Naushad\_A](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Naushad\_A](https://forums.unrealengine.com/u/Naushad_A)\
**Post date:** [November 5, 2022, 6:08pm UTC](https://forums.unrealengine.com/t/running-code-gets-canceled-after-getting-triggered-multiple-times/684616/16 "2022-11-05T18:08:36Z")

</div>

this is your case i assume  
yes it will take 5 sec of delay for new overlap which happen  
after 5 sec

[https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/4/3/f43ff5a51227c9cab4a9995ba70be843c1a6c0e4.mp4](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/4/3/f43ff5a51227c9cab4a9995ba70be843c1a6c0e4.mp4)

---

<div class="post-metadata">

**Author:** ![DarkAt26](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/darkat26/32/590464_2.png) [@DarkAt26](https://forums.unrealengine.com/u/DarkAt26)\
**Post date:** [November 5, 2022, 6:30pm UTC](https://forums.unrealengine.com/t/running-code-gets-canceled-after-getting-triggered-multiple-times/684616/17 "2022-11-05T18:30:11Z")

</div>

i mean that if a new overlap is happening and the code didnt fully run the delay will be “resetted” to 5 seconds. so if one new overlap happens every 4 seconds and idk 10 overlaps total for exampel it would take ~41 seconds for the first and last mesh to get its mesh changed

---

<div class="post-metadata">

**Author:** ![Naushad\_A](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Naushad\_A](https://forums.unrealengine.com/u/Naushad_A)\
**Post date:** [November 5, 2022, 6:41pm UTC](https://forums.unrealengine.com/t/running-code-gets-canceled-after-getting-triggered-multiple-times/684616/18 "2022-11-05T18:41:46Z")

</div>

ok , in that case you can start a timer on overlap and runs a condition for less then 5 sec  
basically what [pezzott1](https://forums.unrealengine.com/u/pezzott1) said earlier .

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [November 5, 2022, 11:45pm UTC](https://forums.unrealengine.com/t/running-code-gets-canceled-after-getting-triggered-multiple-times/684616/19 "2022-11-05T23:45:37Z")

</div>

[OverlapRun.zip](https://forums.unrealengine.com/uploads/short-url/xj8DBzShMEBLQg9X9dM7sU8LoDK.zip) (816.4 KB)

Made a version with threaded time testing / material replacement. Have to build from code.

Mouse scroll wheel makes the overlap sphere larger / smaller.

---

<div class="post-metadata">

**Author:** ![pezzott1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/pezzott1/32/1215068_2.png) [@pezzott1](https://forums.unrealengine.com/u/pezzott1)\
**Post date:** [November 6, 2022, 12:48am UTC](https://forums.unrealengine.com/t/running-code-gets-canceled-after-getting-triggered-multiple-times/684616/20 "2022-11-06T00:48:37Z")

</div>

> [@DarkAt26](#):
>
> I would love to see it 🙂

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/b/7/2b78d7198b6a137b7f7cee8737c6da9c6fdf9dcd.jpeg)

In this example only the actors that overlap explode after a 0.25 second delay controlled by the box actor, not a timer in each bomb:  
 ![ExplodingWithTimer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/3/7/5372f03d89cf6863860c14e009ffa4742c480ebe.gif)

Here its a 2 second delay:  
 ![ExplodingWithTimer2s](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/b/a/8baacce885f3cab6fecba53e7df9ca685c148f8d.gif)

Hope it helps.

[Next page](https://forums.unrealengine.com/t/running-code-gets-canceled-after-getting-triggered-multiple-times/684616.md?page=2)
