Problem removing items from an array


In this image the loop will remove the correct item from the array however I have tried about every thing to get the correct amount removed. As you can see in the first loop it loops through the required item class. The element is a struct which is broken where we have the Item class and the required amount to make the selected item. For now the item is a scope which takes 2 lenses and 1 metal tube which the player crafts from metal and glass. When crafting those the items are removed out of the character’s inventory, however when i go to craft the scope it only removes 1 lense and 1 metal tube. Any ideas? Also in the image I do not have any of the logic to check for the required item amount I deleted that portion unfortunately trying to figure it out. However, I tried a condition statement checking the int. If the requirement was met then it would break.

One issue you might be running into here is that i think when you remove an item from the array in the ForEach loop like you’re doing, it is going to resize the array, and the foreach loop is potentially skipping some of the items in your inventory. Ie if you remove whatever is in the array at index 1, then the array resizes and everything has its index moved down by 1. The item that was at entry 2 now goes to 1, whatever was at 3 goes to index 2, etc. But the loop is going to still move on to index 2 (so that item just got skipped).

One way you could get around this is by making a temporary item array, and when you find the matching item, instead of removing it from the player inventory right away, add it to the temporary array. Once you’re done looping through the player’s inventory, you would then loop through the temporary array, removing any entry in there from the player’s inventory (use the ‘remove item’ node instead of remove index and it will find and remove the correct item for you).

Hope that makes sense! I don’t have access to ue4 at the moment so I can’t show you what i mean visually.

Like Variss mentioned, the reason that this is not working is that the indices are all changing when you remove an item from the array. I have an easy solution to this problem, but it may not be the best: Rather than actually removing the index or the item, store the index that you would have removed anywhere inside an array of ints. Now you need to sort this array from highest value at the lowest index, and lowest value at the highest index. Now you can loop through your array of ints, and do ‘Remove Index’ on your original index at the index of the current array’s value. A better way would be to have a for each loop that counts down rather than up, but unfortunately that is not an option unless you remake a loop. The method I provided removes indices from highest to lowest, meaning that removing them won’t effect the next indices.

Edit: Since the modified for each loop option is much cheaper and much faster to do, I’ve modified it on my end, and here is a picture of the new for each loop:

I’ve highlighted all of the changes I made, hopefully I got them all. Just check the ‘Start from End’ option in your function, enjoy!

Edit: New engine versions (4.10) may overwrite your changes, so if you want, you can just create a macro library (right click in content browser -> Misc -> Macro Library), and paste this modified loop into the macro library, now you can access this new macro from anywhere, and it won’t be overwritten.

Makes sense. I figured it had something to do with the loop.


I know its kinda messy but this is the solution I came up with. Actually works. The reason I chose not to modify the for loop is if I did and updated then I would have to keep redoing my work. Any input is welcome.

Well you could just make a new macro to store your new loop, seeing as it is the most efficient way to do it.

I will keep that in mind. Right now I am trying to get the main functionality forged out before I polish it. The hard part that is yet to come is how to render or show a model within a widget or just having it show above the UI. Trying to add functionality to allow players to customize the item before it is craft and have a preview of the look.

Well, I can’t help you there, but if you have any other questions regarding logic (like this one), I absolutely love figuring them out, so feel free to post.

use a reversed foreachloop only when you want to remove and that should do the trick :wink: tested and applied

Hi there, 6 years from the future.

My current project involves creating an array of UI images and dragging and dropping them into other areas of the UI. I have found a solution that works for my project is the following:
Set the visibility of the item to collapse instead of forcefully removing the item from the array. This way works for me because I can update the array behind the scenes whenever the player leaves the UI, and keep each item in the array in its original index with no modifications to the loop.

Hope this helps someone in 2022.

Personally I feel the overall structure of your inventory isn’t suited for what you want it to do. At least not this part.

Again, Personally, I’d use a data table to define what crafting materials are needed for each crafted item. Simple get row matching name, returning friendly, easily usable data.

My “materials inventory” would be extremely simplified so I could use simple data from the data table row to presort, search, add, remove items/amounts without going through loops and hurdles.

I stay away from loops as much as possible. They have a direct impact on frame time.
Also Data tables make it easy to adjust, add, remove craft items, and formulas for said items.