RandomItemType(ItemType); //Randomize items by type
if (ItemsTypeArr.Find(ItemType) == -1) //check for duplicates
{
ItemsTypeArr.Add(ItemType); // add all items to array if not duplicated
}
else if (ItemsTypeArr.Find(ItemType) != -1)//check for duplicates
{
RandomItemType(ItemType);//Re-randomize items to avoid any duplicate
}
the goal is I have 20 groups of items, and the item should not be duplicated in its own group randomly.
the problem is executing this statement logs Array is out of range but in blueprints working fine.
Please correct me if I am not doing the logic correctly, thanks
You can use something like this, I used ECollisionChannel as my enum example
Be careful with this though because you might end up with an infinite loop, check first if your array can still take extra values otherwise the loop with always fail because the array is already filled with all possible options.
ECollisionChannel ItemType;
do
{
ItemType = RandomItemType();
}while(ItemsTypeArr.Contains(ItemType));
ItemsTypeArr.Add(ItemType); //Add the item
yep , I am doing something wrong in this function itself , and I now believe that if statement has nothing to do with it… I will update the thread once I figure out the place of the bug
I see the random integer from 0 to 99 however you can’t guarantee that the array has a valid index, for this I would suggest doing something like to ensure the index is valid: ItemType = Arr[FMath::RandRange(0, Arr.Num() - 1)];
This will limit the random integer to within range and should fix your out of bound issue.
Edit: changed my example to be similar to your Arr array.