Can add items to ustruct array but i cant remove them

So very much as the question title says, i can succesfully add items to my ustruct array but any try to remove them fills the compiler with errors!, The code itself

void AInventoryGameCharacter::DropItem(FInventoryItemStruct Item)
{
	AInventoryItem* WorldItem = GetWorld()->SpawnActor<AInventoryItem>(Item.ItemActor->GetActorClass(), FVector(0,0,0),FRotator(0,0,0)); // Spawn the item in the world
	WorldItem->ItemInfo = Item; // Assign the item info of the newly spawned item in the world to the one we dropped

        if(InventoryArray.Find(Item)) // If the item IS in the inventory
        {
           InventoryArray.Remove(Item); // Remove it THIS is the line that causes the error/errors
        }
	
	RefreshInventory(); // Update our inventory

}

And the error log from the compiler

1>C:\Program Files\Epic Games\4.6\Engine\Source\Runtime\Core\Public\Containers\Array.h(1906): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'FInventoryItemStruct' (or there is no acceptable conversion)
1>          C:\Program Files\Epic Games\4.6\Engine\Source\Runtime\Engine\Classes\Sound/DialogueWave.h(36): could be 'bool operator ==(const FDialogueContextMapping &,const FDialogueContextMapping &)'
1>          C:\Program Files\Epic Games\4.6\Engine\Source\Runtime\Engine\Classes\Sound/DialogueTypes.h(56): or       'bool operator ==(const FDialogueContext &,const FDialogueContext &)'
1>          C:\Program Files\Epic Games\4.6\Engine\Source\Runtime\Engine\Classes\Engine/StaticMesh.h(245): or       'bool operator ==(const FMeshSectionInfo &,const FMeshSectionInfo &)'
1>          C:\Program Files\Epic Games\4.6\Engine\Source\Runtime\Engine\Classes\Engine/SkeletalMesh.h(549): or       'bool operator ==(const UMaterialInterface &,const FSkeletalMaterial &)'
1>          C:\Program Files\Epic Games\4.6\Engine\Source\Runtime\Engine\Classes\Engine/SkeletalMesh.h(548): or       'bool operator ==(const FSkeletalMaterial &,const UMaterialInterface &)'
1>          C:\Program Files\Epic Games\4.6\Engine\Source\Runtime\Engine\Classes\Engine/SkeletalMesh.h(547): or       'bool operator ==(const FSkeletalMaterial &,const FSkeletalMaterial &)'
1>          c:\program files\epic games\4.6\engine\source\runtime\rhi\public\RHIResources.h(166): or       'bool operator ==(const FRHIUniformBufferLayout &,const FRHIUniformBufferLayout &)'
1>          C:\Program Files (x86)\Windows Kits\8.1\include\shared\guiddef.h(192): or       'bool operator ==(const GUID &,const GUID &)'
1>          while trying to match the argument list '(FInventoryItemStruct, const FInventoryItemStruct)'
1>          C:\Program Files\Epic Games\4.6\Engine\Source\Runtime\Core\Public\Containers\Array.h(1902) : while compiling class template member function 'int32 TArray<FInventoryItemStruct,FDefaultAllocator>::Remove(const FInventoryItemStruct &)'
1>          C:\Users\Alvaro Gamer\Documents\Unreal Projects\InventoryGame\Source\InventoryGame\Private\InventoryGameCharacter.cpp(164) : see reference to function template instantiation 'int32 TArray<FInventoryItemStruct,FDefaultAllocator>::Remove(const FInventoryItemStruct &)' being compiled
1>          C:\Users\Alvaro Gamer\Documents\Unreal Projects\InventoryGame\Source\InventoryGame\Public\InventoryGameCharacter.h(36) : see reference to class template instantiation 'TArray<FInventoryItemStruct,FDefaultAllocator>' being compiled
1>C:\Program Files\Epic Games\4.6\Engine\Source\Runtime\Core\Public\Containers\Array.h(1906): error C2088: '==' : illegal for struct
1 Like

You are comparing your Struct somewhere. I’m not that good in debugging C++, but the console Error clearly says that you use a “==” in the wrong place.

Since a can’t see a “==” in your struct itself, would you mind checking all places where you could have used it with your Struct? Or just show me the complete code where you use it.

I double checked in all my classes and im not using == with my struct anywhere, What i find weird is that if i comment the InventoryArray.Remove line it compiles fine, but as soon i un-comment it the error returns…

My guess is the .find function tries to find the matching UStruct but you have no == operator on your struct so it does not know how to compare the objects. I’d recommend having an array of pointers (im assuming InventoryArray is an array of your actual struct instead of pointers).

Actually scratch that, it compiles now but now i using the Array.Find function Seems to be working in a odd way, It seems to not return bools whatever i try to compare for a true or false value, Logging its index shows up its -1 for some reason…

This is the code im using

void AInventoryGameCharacter::GiveItem(FInventoryItemStruct Item)
{


	if (!InventoryArray.Find(Item)) // Doesnt seem to go beyond here
	{
		//Add the item to the array
		InventoryArray.Add(Item);
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Added item display text was %s"), *Item.ItemName.ToString()));
	}	
		RefreshInventory(); // Update our UI
}

And here is the struct bool operator, ( In another class )

bool operator==(FInventoryItemStruct const& Other) const
	{
		if (ItemName != Other.ItemName)
		{
			return false;
		}
		if (ItemActor != Other.ItemActor)
		{
			return false;
		}
		if (ItemImage != Other.ItemImage)
		{
			return false;
		}
		return true;
	}

did you put that == operator overload inside the definition of your struct?

USTRUCT(BlueprintType)
struct FYourStruct
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item")
		int32 ItemID;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item")
		int32 MaxAmmo;

    bool operator==(FYourStruct const& Other) const
     {
         if (ItemID != Other.ItemID)
         {
             return false;
         }
         if (MaxAmmo != Other.MaxAmmo)
         {
             return false;
         }
         return true;
     }

};

Yes i did

USTRUCT(blueprintable)
struct FInventoryItemStruct
{
	GENERATED_USTRUCT_BODY()

    public:

	UPROPERTY(EditDefaultsOnly,BlueprintReadOnly,Category = ItemInfo)
	class AInventoryItem* ItemActor;

	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = ItemInfo)
	UTexture2D* ItemImage;


	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = ItemInfo)
	FName ItemName;

	bool operator==(FInventoryItemStruct const& Other) const
	{

		if (ItemName != Other.ItemName)
		{
			return false;
		}
		if (ItemActor != Other.ItemActor)
		{
			return false;
		}
		if (ItemImage != Other.ItemImage)
		{
			return false;
		}
		return true;
		
	}

};

hmm.
maybe the const was in the wrong place:

bool operator==( const FInventoryItemStruct& Other ) const
{
    return ItemName == Other.ItemName
    && ItemActor == Other.ItemActor
    && ItemImage == Other.ItemImage;
}

Tried that, the .Find function does seem to return the item index now but if i try to use it in a If statement it still doesnt go beyond that…

Try my suggestion and save pointers to your struct inside the TArray. If you stick with your solution some things might go wrong down the line and not just this issue. For example, I see your GiveItem and DropItem functions are passing your struct in by value instead of by reference or a pointer. This means the copy constructor is being called but you don’t have that overloaded. Check what equality check returns false inside your == while debugging, or even if its being called at all. I’d reckon its the ItemName that is not matching up since perhaps default copy construction might not cut it, but that’s just a guess.

Hey, i just debugged the quals in the == operator of the struct and it actually returned true ( as in all the properties were equal, none returned false or mismatching ), I did try to use a array of pointers to my struct instead but since i need to directly get the array to later display it trough UMG it seems i cant make it both a array of pointers and public since the compiler shows up that i cant have a exposed pointer of that type, Instead i tried creating a function that returns a pointer to the inventory array but again the compiler dident like that heh…

maybe instead of:

if (!InventoryArray.Find(Item))

you could try:

if (InventoryArray.Find(Item) != INDEX_NONE)

This. Returning zero would mean the first element of the array wouldn’t it, classic programming error good catch.

I actually ended up going for a workaround, since the Find function returns the index of the item i validate the index and then grab said index, compare the item in the array with the index against the one we are trying to add / drop, and so far it has worked great, Thanks for all the help!