Hi, i’m working on an inventory system and i need “update inventory”, how can I order my STRUCT Array by deleting the indexes that have the amount 0 when it’s used by the player? It’s 2 days I’m trying to find something on the forum but I can not solve this problem… can you help me please? thanks in advance! Any suggestion please.
You’re removing elements from the same
array you’re iterating over. It’ll
shrink the size of the array, but the
loop counter inside ForEachLoop won’t
decrease, so it’ll skip elements.
To quote the description of the macro:
Warning: Do not add, remove, or
reorder elements in the input array
during the execution of the loop
body!, which is exactly what you’re
doing.
I see what you mean, you’re right. So flag the index for removal once they’re all discovered. There are so many ways to achieve that.
I’ll remove my original answer, it’s not correct as is.
You’re removing elements from the same array you’re iterating over. It’ll shrink the size of the array, but the loop counter inside ForEachLoop won’t decrease, so it’ll skip elements.
To quote the description of the macro: Warning: Do not add, remove, or reorder elements in the input array during the execution of the loop body!, which is exactly what you’re doing.
I’m not saying it cannot be avoided or fixed, but as-is, your example will result in unexpected consequences for people who will copy it.
@Everynone yes you’re right, there are many ways to achieve it, I just wanted to prevent people learning from an example they should avoid, thanks for the response.
@HarryHighDef that’s right, using a ReverseForEachLoop usually bypasses the problem (or creating a copy of the original array, or an index array for removals as @Everynone said).
You can just create a local temp variable in the function that holds the original array, iterate through that copy and remove from the original array.
You can also use Rama’s plugin or the LE Extended Standard Library
Doesn’t work for a “struct” but if anyone is trying to simply sort an array of integers this will accomplish that in one neat little node. Link to the tutorial to create the node is below (just copy/paste C++ code). This will also give you a String sort and a Float sort.