Running code gets canceled after getting triggered multiple times

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?

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

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.
2 Likes

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

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. )

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

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

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

2 Likes

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

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:

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

2 Likes

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.

2 Likes


you can try with this

I would love to see it :slight_smile:

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?

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

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

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

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

ok , in that case you can start a timer on overlap and runs a condition for less then 5 sec
basically what pezzott1 said earlier .

OverlapRun.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.

2 Likes

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

Here its a 2 second delay:
ExplodingWithTimer2s

Hope it helps.

1 Like