I just made a little code that checks if my array contains something.
Then I realized that later on I need the index of this as well. So I added “find” after the “contains” branch to get the index.
I did it this way because my very first thought was, jeah he doesnt have to try and search the index if it doesnt exist in the array…
But after looking at it for a few seconds I very much doubt the usefulness of this because they probably both do the exact same thing just with a different output. But I am not 100% sure so I thought why not make it clear for all and once.
So, please confirm or correct me. They both do the same and if I need the Index anyway I can replace the “contains” with “find” and branch for -1 index to see if it contains it or not.
Using “find” will get that array entry if it exists, returning the Index of where it exists. It returns -1 if it fails.
Using “contain” simply asks if it exists in the array, returning true or false.
If you check to see if it is “contained” in the array, and if it does, then proceed to “find” it, you will handle the error for if it doesn’t exist.
If you only “find” it, then you will need to check if the result is not -1, to then execute the rest of the logic. This would be shorter by one node.
The point of having both exist is for preference. Because you want the index, you might as well work with “find” but I don’t see it as that big of a deal using both.
If you need the Index, Then FIND is what you want to use. It returns Both the index and a Boolean.
If you don’t need the index and only want to check if it exists, then CONTAINS is what you want.
Good example for Contains usage would be limited storage in an inventory system.
e.g. You can only have one of X in inventory. You find another and want to add it.
Does inventory “Contain” X: True → Do Nothing, Else Add
If you have stacking then Find
“Find” X in inventory: Found (bool true) → Use index for quantity → Add, Not Found (bool false): Add new entry
Correction!
The above is only true for TMap. TArray Find only returns index.