Download

Error in FindLast method with Struct Array

How I can fix this problem with FindLast?
image

Index is uninitialized.

image It’s not working. Error in method “FindLast”

Show find last function.
Or try Index = Inventory.FindLast();

if Inventory.FindLast(); does not work, you can try this below. That should work if what Dejan33 wrote does not work.

Index = Inventory.num() - 1; // that should be the last index or all your inventorys. if num() is say 5 that would be 0 thru 4, so add minus 1 to get your actual full array. other wise if no - 1 you’ll get 0 thru 5 and you’ll be off by 1.

This is FindLast function


I need to get the index of the found element in the array, not the last element.

how are you going to check the array for it, if it has not been added yet to the array? look at AddUnique(ElementType&& Item) enstead off just add. If i am not mistaken that will check if its added already and if it not it will add it.

FindLast function returns INDEX_NONE if item hasn’t found.

Index is a local variable.
Maybe you need a pointer to the Index for that function to set the index.
Not sure how that works anymore. Working with blueprints too long.

It didn’t help with Index. Most likely the problem is in the Item variable.

Item = AddToitem; ? then use Item. Once you add it then you will have an index to it. Then you can get its index.

/**
* Adds unique element to array if it doesn’t exist.
*
* Move semantics version.
*
* @param Args Item to add.
* @returns Index of the element in the array.
* @see Add, AddDefaulted, AddZeroed, Append, Insert
*/

Even if find last give the index, nothing is done with it, otherwise it returns INDEX_NONE which is used in else expression? How thus that work?

The function returns bool value (true or false), and INDEX_NONE or another index is written to the variable Index

Index = Inventory.AddUnique(Item);// that should get you your index after you have added it.

Yes, but then you are using INDEX_NONE in the else expression if nothing is found. Or zero since that is how it is initialized.

No, because if the function doesn’t find any elements, it will return bool = false and assign the variable the value INDEX_NONE. In this case, else will not be executed

Did you get it working?

While nothing happened, the error remained in FindLast. Do you know any other functions for finding the index of an element?

Is this a compiler error or debug error?

VS don’t think it’s a mistake, but UE do.