x

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"

Cannot Adjust for the Index shift when Removing from Array During ForLoop

Hello there!

So, I still have the problem that when removing elements one by one from an array (or instanced static mesh component) during a for loop I'm unable to adjust the new index correctly to take in account that the array shrinks each time it has removed an element.

Initially I wanted to solve this problem with a reverse for loop but with this method I noticed that I am unable to update my reference which is imperative for my system to work correctly.

alt text

As you can see in the picture above:

  • Fist I'm adding a bunch of instances to an Instanced Static mesh Component and right after that I've created a Transform Array to reference its content (this is the reference I need to update after removing an instance).

  • Second I'm going through this very Transform Array and use its index as my remove value in order to remove the instances in my Instanced Static Mesh Component one by one.

As you can see I'm using a variable "MinusOne" because I want to adjust the Remove Value after an Instance has been removed (because of the index shifting as a result of that). At the beginning the value is 0 then, when the first instance has been removed, I set it to 1 so it calculates the new remove value -1 in order to get the correct index for the next element again.. however it doesn't seem to work like that.

Maybe I'm overlooking something? Is this not the correct way to adjust for the index shifting? My system is almost done and I still haven't figured this out, so I would be eternally grateful for some advice!

Thanks again for your help!

Product Version: UE 4.13
Tags:
minusone.png (1.9 MB)
more ▼

asked Oct 03 '16 at 09:12 AM in Blueprint Scripting

avatar image

OpenStringStudio
133 31 30 34

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

2 answers: sort voted first

You should have a quick re-read of the comments in the old topic. If it's still not clear, stick some print string nodes in there and print out the index you're getting from the foreach loop and the index you're passing into the remove instance node. I think once you see the numbers you'll see what's going on.

more ▼

answered Oct 03 '16 at 10:25 AM

avatar image

daveREspawn
87 7 11 15

avatar image OpenStringStudio Oct 04 '16 at 07:25 AM

Why are you so reluctant to give me an answer..? I will try some further but I'd really appreciate some help..

If I would have understood it in the first place I wouldn't be here desperately looking for an explanation. I've already discussed the issue with my designer co-worker but she's just a designer after all and wasn't really able to help me. I gave this matter serious thought but still I don't seem to get it!

So please, if you do know what I'm doing wrong, if you know the correct answer in my case (as shown in the picture), it would help me much more if you could give me the solution right away so I can have this famed "aha" effect and move on..

There are a lot of other things I have to take care of as well and I'm dwelling on this for far too long now without any success.

Thanks a lot, Kind Regards Gull Jemon

avatar image daveREspawn Oct 04 '16 at 11:14 AM

In your foreach loop, you've got all the items. So for this example say there are 10. It loops over each of them, so index 0 to 9. At the start, the foreach 'array index' will match up with your instance index, since it also has 10 items.

First the first loop the index will be 0, so you'll remove instance 0. Second loop you'll go to remove index 1, and with this loop you'll subtract 1, so the index is 0, that will work Third loop the index will be 2, you'll subtract one, making the remove index 1 which is now wrong, because you're skipping 0 thanks to the reorder of the instances after a removal

So, keep track of the number of items removed, decrement your removal index by that value.

I was reluctant to just give you the answer because you learn more from working on these things yourself. Especially true for this situation where you had all the info you needed from the docs and from the previous topics.

Anyway, hope that fixes your issue and good luck with your project.

avatar image OpenStringStudio Oct 04 '16 at 06:37 PM

Cool thanks a lot, that's exactly what I need. You see I've tried this already and bumped into a problem, so I thought it was wrong..

UPDATE: This is the correct answer though! My system didn't work due to some other error I've made prior to these calculations..

This here is the Solution without reverse for each loop:

alt text

Like that some instances get removed and some not, but the index will always be correct no matter what happens.

Thanks a lot for your help,

Kind Regards Gull Jemon

works.png (993.2 kB)
(comments are locked)
10|2000 characters needed characters left
Viewable by all users

Make an array variable of integers and store the index values you want to remove. Then on complete go through that array and remove the items of the TargetArrayList.

more ▼

answered Oct 04 '16 at 10:13 AM

avatar image

Allart
6 1 2

(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