How to break the for each loop correctly?

Hi friends, I want to break the for-each-loop, not sure if i am doing right. look at bp example , i want the same way to break the loop.

//Remove any item from the array if this is not a weapon
for(int i = 1; i < 10; ++i)
{
     for (auto& Array : ItemsIDArr)
	{
		if (Array.Type != EItemType::E_Weapon)
		{
			ItemsIDArr.Remove(Array);
		}break;
	}
}

and to remove index we use someArray.Remove(index); ??
BP Example:

Hello, to break the loop like in the blueprint, the break statement should be inside the if statement.

      if (Array.Type != EItemType::E_Weapon)
      {
         ItemsIDArr.Remove(Array);
         break;
      }
2 Likes

Hi, it will break the for loop if break goes under if statement?

If the break is under the if, the loop would stop, but always after looking only at the first element in the array ItemsIDArr. Also, this can happens even if you don’t remove the first element of the array

and this Remove Index in blueprints is actually ItemsIDArr.Remove(index); ?
i am getting this error on compile time:

Error|C2678|binary '==': no operator found which takes a left-hand operand of type 'FST_ItemTypeAndID' (or there is no acceptable

If ItemsIDArray is of type TArray, then you can use RemoveAt to remove an element in a specific position of the array.

For the compilation error, I think is probably related to the Remove method

ItemsIDArr.Remove(Array);

The Remove method, remove as many instances of the element Array from ItemsIDArr, and for that you need to compare to the elements in ItemsIDArr, using the operator β€˜==’, and because is not defined there is an error.

1 Like