Using Custom C++ Structs as TMap Keys in Unreal Engine

biq
The Startup
Published in
3 min readOct 15, 2020

--

The title picture, selected to impress you with random code.

Intro

Structuring the data in your solutions is a vital part of success. Epic Games has made a quite convenient to integrate custom structs into Unreal Engine’s infrastructure. In this article, we take a look at how we can achieve integration of a custom struct into the Engine’s hashing system. This can be useful for comparisons and more excitingly, using your struct as a key value in a TMap instance.

Setup

Minimal Declaration

First let’s define our minimal data structure. We have three include statements, of which the third is of particular noteworthiness. The file HashMeIfYouCan.generated.h is an auto generated header, containing meta information gathered by the Unreal Header Tool. It is convetion, to set up your Engine Objects, with a consistent name for the file and type. In this case our type is called FHashMeIfYouCan. The F prefix is Unreal Engine’s (mandatory) naming convention for any engine struct. Notice also that we annotate the struct definition with Unreal Engine’s reflection and type system macros. We provide a BlueprintType struct specifier to ask for blueprint exposure. At the top of the structure body definition sits a bit of boiler plate code generation through the GENERATE_BODY macro, which generates all functionality needed to make your…

--

--