Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

For Each Loop only working at random!

So as i'm trying to build a basic fighting system i came across this pretty random issue. It's set up so that the player character has a box collider (DamageBox) and when i press attack an animation is started and an anim notify calls the attached PerformAttack Event. (The boxes have an Any Damage event which should destroy them every time i hit)

you can pretty much ignore the booleans, nothings wrong there, it also doesn't make a difference if i apply a class filter in the overlapping actors.

as debug function overlapping these boxes makes them red while ending the overlap makes them white/pink.

if it were only the boxes at the outer edges of this collision box that are not affected by the for-loop everything would be fine, i could just make the box bigger, but even objects entirely inside the box are ignored at random which is pretty bad. Attached files are the blueprint, the stack of enemy boxes once i overlapped them with my DamageBox without hitting, then after 2 hits and one from the player perspective.

I'm grateful for any info you can give me!

Product Version: UE 4.21
foreachbp.jpg (137.7 kB)
foreach1.jpg (332.2 kB)
foreach3.jpg (254.0 kB)
foreach4.jpg (214.5 kB)
more ▼

asked Nov 28 '18 at 05:42 PM in Blueprint Scripting

avatar image

31 1 4

avatar image Tuerer Nov 28 '18 at 07:28 PM

I think the problem is in cooperation of the hit animation and the ForEach loop, the animation in still playing while the loop performs and at every given point in time overlapped components change.
You shouldn't use ForEach loop here. You should apply damage On Begin Overlap same as you make the boxes red.
Why ForEach? Maybe there's something I can't see though.

avatar image nonamed46 Nov 28 '18 at 09:18 PM

hm yeah i might try not having the box on all the time but only switch it on instead. Then i can just use the Begin Overlap event once every time i hit. Thanks!!

i guess it could be an issue if the array of overlapping actors is cleared and refilled every frame. since the player actor and the attached box are not moving in any direction (root motion is disabled) there shouldn't be a change in the overlapping actors.

you see what i'm trying here is to have this DamageBox on all the time and use Apply Damage to everything inside that Box once every time i attack. Thus the for each loop over all overlapping objects of this box. Maybe i could make this work (just for the sake of testing this^^) if i save the array of overlapping actors to a variable and loop over that variable instead of the ever changing array of overlapping actors

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

So thanks to Tuerer i thought again on the way "Get Overlapping Actors" works. I believe that the array is being cleared and recreated every frame, so the "For Each Loop" ends randomly since the array is being cleared before it can reach the end. So the function can never really iterate over every object before the array is emptied again unless enough objects have been destroyed that the array is short enough to be iterated over in under one frame.

It is probably much smarter to just activate and deactivate the triggerbox instead of having it on all the time and iterate over everything inside once every attack. Though for the sake of resolving the error and proving that i'm not completely insane i fixed my original BP by saving the Overlapping Actors in a Variable before attempting to loop over the array.

fixed Blueprint before hit after hit

foreach-v2.jpg (109.8 kB)
v2-before.jpg (95.2 kB)
v2-after.jpg (136.6 kB)
more ▼

answered Nov 28 '18 at 09:41 PM

avatar image

31 1 4

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question