Reverse For Loop for Components

Hello there,

Update: To anyone with the same problem, you must use a Reverse For Loop to make this work:

I appreciate any kind of help!

Hey hey, no need for anything fancy or elaborate, if you remove an index, any index above that will just be decremented by one. So if you have 10 items, and you remove index 7, then 8 becomes 7 and 9 becomes 8.

Depending on your setup and from reading your question, you might be asking if I remove an instance, how do I let other items in the world know about it. If that’s the case then you might need to rework the relationships, so the interaction that removes the instance goes through the other objects.

What’s the end goal of storing the instance indexes?

Worth mentioning; If you break a hit result, from hitting an instanced static mesh comp apart and use the ‘hit item’ int, it will be your instance index.

Hope that helps,
Dave

Thanks a lot for your answer… My first assessment was right then… In any case thanks a lot for the reference pictures. I knew about the break result, I didn’t consider using it though because I want to reduce the amount of calculations to a minimum… however in this case I’m actually not sure what would be the better approach in terms of performance…?

No problem, happy to do it.
What’s the end goal, what are you trying to build? If you can tell us that, I or someone else might be able to help you out.

Basically the player is in a field of instances and when he moves outside of their range they should gradually be removed, however when he returns the instances should be added back to the scene again, you should be able to repeat this as many times as you want. It may be comparable to culling but the difference is that I am actually removing the instances from the component, I don’t want it just to cull…

If you need to remove them, you can always just move them under the level or to 0, 0, 0 and then put them back in their original place as you need to. That’ll save you a bunch of trouble. Would that work?

Hey thanks for your suggestion but this is no solution. It might be a supplement though… like at the beginning they will slowly submerge into the ground and only once they are not longer visible they get removed, this would be a very cool visual effect.

  • Maybe you can help me out some more?

I’m having trouble figuring out what exactly I need to do in order to make this work… here’s my setup:

You can think of it that way: I’m using an array of integers that holds all indices of one Component. When they have reached a certain distance they need to be removed one by one.

Here’s my problem. I know I have to modify the Return Value after each time I have removed an instance but I have tried to get behind the logic of this without any success…

If you dare to be so kind you could explain to me (or show me with a picture) how I can correctly remove my instances one by one within a for loop, meaning how I have to change around the Remove Value to make it work?

Thanks a lot for your effort,
I really appreciate it!

I could show you, but from the phrasing of your question and from the picture, it would be far more valuable for you to figure it out yourself.
I will give you the steps you need to take, looking up the nodes and how to use them is on you.

Since you have the index of the instance, get its transform.
Use the transform to get the distance between the player and the instance (vector length).
If that distance is far enough, remove the instance.

As for the loop you posted, that won’t work, since you’re not adjusting the remove value based on what’s been removed before. See my previous post about indexes.

Thanks but you don’t understand… all logic has been dealt with already! This is the last step.

I need no advice on an entire system. I know exactly what instances need to be removed… it’s just that the removing itself proves a little difficult to understand for me (because of the index shifting and such).

As I previously stated I just want to know how I need to adjust the remove value… It seems I’m unable to wrap my head around that.

So hopefully you understand me now. How do I make this work?

Thanks a lot!

update: apparently there’s a thing called “Reverse For Loop” which exactly addresses my problem: [A Little Tip] Remove elements from an array in one pass with a reverse for loop - Community Content, Tools and Tutorials - Unreal Engine Forums

I’m going to look into that now, hopefully it gives me the desired result…

Update2: Yes the Reverse For Loop definitely works perfectly fine!

Nice work

Hey, thanks for your help again. I noticed that the reverse for loop doesn’t really seem to solve my problem and I’m still not able to figure out the other method. I’ve created a new question for this very reason… maybe you want to look into that, would be super kind of you: https://answers.unrealengine.com/questions/499886/cannot-adjust-for-the-index-shift-when-removing-fr.html