Only first item in array getting handled

Hello!

The concept here is to light a fire and add fuel (sticks) in VR.
When a fuel is added, 5 seconds per fuel is added to the duration of the fire and the actor is added to an array. An event is called to delete each actor that is in the array every 5 seconds until the array is empty. Essentially simulating fuel being burnt up.

The problem is it will delete the first stick put in the fire. Any additional sticks thereafter will not be deleted even though the extra time is added. I can’t seem to figure out why…

Any help would be greatly appreciated! Thanks.

What may be happening is the loop for delete fuel may be taking the first stick and running the loop exclusively with that stick, essentially not adding additional sticks to the loop despite the array being updated as the loop has already been started. What I would do is have a “time left on current stick” variable and have it count down to 0 (possibly using in delta time on every tick) then removing index 0 from the array when the timer reaches 0 and reset it if there is another stick in the array. I would find this very likely to be the case if a second stick added while the first stick is burning does not burn, but a third stick added after the fire dies out results in the second stick being deleted, but not the third.

As a side note, you have the loop remove the current array index from the array. This may be problematic due to the indexes changing because of deletion. (Index 0 is removed, making index 1 now 0, 2 now 1, and so on. Next loop deletes index 1, but that index is what use to be index 2, which skipped what was index 1, which is now a 0. You could remove index 0 every time, and the array would move all the values down afterward.

1 Like

You can’t put delays in a loop, it’s effectively ignored.

Also, as @DraixPrime points out, you can’t remove array items, while you’re processing an array. You can do that if you process the array backwards, though.

1 Like

Thanks for the help - I was able to get it working.

The delay was indeed causing issues. But without it, the fuel would “burn up” as soon as it entered the fire. So I created a For Each Loop with a Delay to fix that issue.

(I also modified adding to the array for functional improvement)

For anyone wondering, this is the tutorial I followed to make the custom For Each Loop with Delay. UE4 Tutorial - For Each Loop With Delay - YouTube

2 Likes

I once created a for loop with a delay by modifying a copy of the for loop. I was a bit jerry rigged, but it worked.