How to overload operator==? FTransform

Hi , I would like to overload this operator and I need a detailed guide how I can do it without breaking the engine source code.

in which class I need it to add and how?

bool operator==(const FTransform& LTransform, const FTransform& RTransform)
{
return
	(
	    LTransform.GetLocation() == RTransform.GetLocation()
	);
}

Is this stuff any help?

2 Likes

Thank you Sir for replying
I added it to the header and it says the operator== has too many arguments

I must confess, I have no idea what I’m talking about. But with VS, I see a lot of people saying that there are two possible solutions to this.

  1. Overload as a unary operator

or

  1. Overload outside the class

See

and

1 Like

Can You test to compile the following code if to figure our what I am doing wrong?

you can test it without and with blueprint libraries, both gives the same error.

int32 Index;
FTransform Location;
TArray<FTransform> TransformArray;
bool bEqualValid ;

//Randomizing the array and subtract -1 index from the array
index = FMath::RandRange(0, TransformArray.Num() - 1);

Index = FCustomThunkTemplates::Array_Find(TransformArray, Transform);  //this function causing the operator overload error.
bEqualValid = UKismetMathLibrary::EqualEqual_IntInt(Index, -1); //this function causing the operator overload error.

I have problem with this from few days, in blueprints it works perfectly, Since FTransform has no == I won’t be able to use the normal methods of finding the index.

Sorry, I can’t, as I don’t use C++ :smiley:

Have you tried Unreal Slackers?

1 Like

Yes I found that channel yesterday and joined they are not able to help, they want me to use vector array, but I want to use transform array, transform has no == operator

1 Like
public:
generic <typename T> static cli::array <T> ^ FindAll(cli::array <T> ^ array, Predicate<T> ^ match);

hope it helps
cheers!

1 Like

Free operator==() is added OUTSIDE of class scope.
You should add it in some header, outside of the definition of any class.

Also, defining an equality operator for transforms in the way you suggest, would not pass code review for me – it will lead to hard to reason about bugs, because someone will assume it test for actual equality, and “most of the time” it will “work fine.”

Use the equality operator only for cases of strict equality. Add some wrapper function for other kinds of tests. Or, just code in the GetLocation() on each side when that is what you mean – it’s like 20 characters to type, and it’s much clearer to read and understand what’s being tested.

1 Like

…and another thread about this operator. Oh God.

@Alexa.Ki they are not able to help, they want me to use vector array, but I want to use transform array

How many people still have to tell you you’re doing it wrong?

@c0r37py Is this trolling or what?

2 Likes

I am now trying to switch to vector, for this I will need to modify so many things in other classes

I need a small example of implementation step by step , I am very new to overloading things

Sir can you explain how it works? or a small example will be helpful

:smiley: this is not a troll, this is actually I am showing her to stop what she is doing, overloading operator will still not help her since she is comparing float random offsets, the only thing can help her is this Post in her case of do/while loop || ContainsByPredicate.

you do something like without overloading operator

if (Available.Num() == -1)
	{
		Available = AllTransforms;
	}

	int32 randomIndex = FMath::RandRange(0, Available.Num() - 1);
	FTransform result = Available[randomIndex];
	Available.RemoveAt(randomIndex);
	return result;

have a return type of function FTransform

hope it helps
cheers!

2 Likes

is it right?

FTransform AMyGameMode::RandomLocations()
{
	if (Available.Num() == -1)
	{
		Available = AllLocations;
	}

	int32 randomIndex = FMath::RandRange(0, Available.Num() - 1);
	FTransform result = Available[randomIndex];
	Available.RemoveAt(randomIndex);
	return result;
}

and where the used locations which should not be repeat

Available.RemoveAt(randomIndex); <— removed it, why you need them?

1 Like

still duplicated locations :tired_face:

show the code how you call it?

1 Like