I’m basically trying to make it so that when you’re caught in the game, your inventory (actors held array) clears, and your held items are respawned around a list of randomly chosen locations (get all actors of class) to recollect. But seemingly by chance, the actors sometimes don’t spawn, skipping the deletion process as well. I did ensure the actors are set to always spawn, by the way. I’ve tried countless niche ideas to fix this, but can’t get anywhere…
In the screenshot, rather than clearing the array entirely, I was testing removing individual items from the array after they spawned.
Hello! I just tried this and now it seems to consistently be giving me the results of only spawning 2 of the items rather than all of them. This did fix the randomness of it breaking though. On top of that, your idea sparked something in me, the solution I came up with was to check if the inventory array was empty or not after the for each loop, and if it wasn’t it’d run the for each loop again to flush out anything else and spawn it in. Thankfully, it seems like this fixed it. Thank you for your help
as others suggested above, i personally would shuffle the array and iterate sequentially instead.
also tangential, spawning a ton of actors in a tight loop in a single frame is asking for performance issues, freezes, hitches, pso cache misses, and ue simply cancelling your loop due to duration.
might not happen during normal plays, but you’re leaving the door open for a player to gather a ton of objects and then loose them all together.
is good to start with a simple function to begin with, but you might keep it in mind for future optimizations. usually spawning staggered in several frames.
As @Auran13 said, I suspect you’re modifying the array mid-loop since you mentioned
“In the screenshot, rather than clearing the array entirely, I was testing removing individual items from the array after they spawned.“
So if you’re iterating the array ActorsHeld which first has [ Entry1, Entry2, Entry3, Entry4, Entry5 ].
If while processing Entry1, you remove Entry1, then the array becomes [ Entry2, Entry3, Entry4, Entry5 ]. So the for-loop continues to the second item, which is now Entry3. In the end you’ll only have processed half the array, because you definitely should not modify the array you’re iterating over.
Either:
Make a copy and iterate over that
Reverse iterate: process the last element first, then you can remove it during iteration
Or process the whole array and empty it afterwards.