Download

Custom Struct and Array.Find

Hi guys,

I have a little problem with a custom C++ Struct being used in an Array in Blueprints.
If I use Array.Find to find an item inside, the overloaded == operator is not called. But if I use my custom Compare Method, written to expose this function to blueprints, it works as expected.
Can someone point to my mistake here?

.h



// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Engine/UserDefinedStruct.h"
#include "FSaveStruct.generated.h"

USTRUCT(BlueprintType)
struct FSaveStruct 
{
	GENERATED_BODY()

public:
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	FGuid ObjectUUID;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	FString UserUUID;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	int32 ObjectState;

	//Constructor
	FSaveStruct()
	{
		UserUUID = "";
		ObjectState = 0;
	}

	inline bool operator==(const FSaveStruct& other) const
	{
		GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Red, "OPERATOR OVERLOADED!");
		return other.ObjectUUID.ToString() == ObjectUUID.ToString() && other.UserUUID == UserUUID;
	}
};

UCLASS()
class FANTASYSURVIVAL_API UFSaveStructLib : public UBlueprintFunctionLibrary 
{
	GENERATED_BODY()
public:
	UFUNCTION(BlueprintPure, meta = (DisplayName = "Equal FSaveStruct", CompactNodeTitle = "==", Keywords = "== equal"), Category = "Math")
		static bool EqualEqual_FSaveStructFSaveStruct(const FSaveStruct &arg1, const FSaveStruct &arg2);
};


.cpp:



#include "FSaveStruct.h"

bool UFSaveStructLib::EqualEqual_FSaveStructFSaveStruct(const FSaveStruct &arg1, const FSaveStruct &arg2)
{
	return arg1 == arg2;
}


Blueprint “Code”:
0cc0ff1f6c781e907813726bc128765bdeb5b1c5.png

I hope someone knows whats wrong here. I need to override the == operator, because I don’t want to compare all properties, because the Object State should not be compared (as this is different 99% of times).

I guess you expect the Find function to call your overloaded == operator for each lement but that may not be the case. the Array.Find Blueprint implementation is running in some virtual machine, this is not the same execution environment as your C++ code. Use a custom Find function instead in C++ and expose it to Blueprint.