UE5 How To Use A Custom Struct As TMap Key

This tutorial will cover what to do in order to use a custom c++ struct with TMaps and TSets as map keys. You can refer to this full code as your read the tutorial. You should be able to understand it once you reach the end of the tutorial.

				
					struct  FMyStruct
{

private:

	int SomeIntData=0;

public:
	FMyStruct();
	~FMyStruct();

	//Override the comparison operator
	bool operator==(const FMyStruct& Other) const
	{
		return SomeIntData==Other.SomeIntData;
	}

};

FORCEINLINE uint32 GetTypeHash(const FMyStruct& MidiTime)
{
	uint32 Hash = FCrc::MemCrc32(&MyStruct, sizeof(FMyStruct));
	return Hash;
}
				
			

Required Function Overloads

Our Struct has to overload the == comparison operator in order to be used as a key. One more function we need to write is the GetTypeHash() function (Don’t Worry).

Comparison Operator ==

Your comparison operator should contain the logic that determines if two instances of your struct are equal. For example, in our code, if the integers within the two structs are equal then we can say the two struct objects are equal and return true.

				
					...
	//Override the comparison operator
	bool operator==(const FMyStruct& Other) const
	{
		return SomeIntData==Other.SomeIntData;
	}

...

				
			

The TMap has to determine if two potential keys are equal, and it will use this function so that’s why you need to define it. You can write any logic here that compares the two structs, as long as in the end, you return true(equal) or false(not equal).

GetTypeHash Overload

You must implement this function OUTSIDE your struct. It is not supposed to be a member function of your function BUT it has to to take your Struct type as a function argument. 

 

What is expected of you as a developer in this function is to write an algorithm that will generate a unique HASH for your Struct. DO NOT PANIC.  Unreal Engine has helper functions to generate hashes. This is how you can easily generate and return a unique hash:

				
					

FORCEINLINE uint32 GetTypeHash(const FMyStruct& MidiTime)
{
	uint32 Hash = FCrc::MemCrc32(&MyStruct, sizeof(FMyStruct));
	return Hash;
}
				
			

The FCrc::MemCrc32() will generate a hash for your struct. Just call the function and return the Hash. You don’t need to worry about what’s going on under the hood or what hashes are unless you have a good reason to.

WHAT IS FORCEINLINE?

This will inline your function. Meaning it instructs the compiler to replace every function call of this function with its contents. It’s an optimization MACRO. Meaning your code that calls this function will run faster. You can read more about C++ inline functions and what’s their purpose.

5/5

Welcome to our audio and rhythm plugins collection! With MidiEngine, you can easily import MIDI files and use midi events to create engaging rhythm gameplay or enhance your videos.

4.5/5

Plugin that helps add UMG Widgets to Level Sequences for Text Layers, Movie Titles, 2D layers and more… directly within sequencer. Cross platform.

5/5

Let your end users Split And Resize The UI at runtime. Perfect for both games and Applications.

Consider investing in some of our plugins. They might save you some time.  Also helps supports these tutorials.

Instantly get access to our plugins like Blender Curves Importer and UMG Cinematics and more when  you support us on Patreon!

 Join Us On Discord For More Daily Tips!