{"id":3861,"date":"2022-06-03T18:11:26","date_gmt":"2022-06-03T16:11:26","guid":{"rendered":"https:\/\/store.algosyntax.com\/?post_type=asx-lms-tutorial-cpt&#038;p=3861"},"modified":"2026-03-09T17:31:42","modified_gmt":"2026-03-09T15:31:42","slug":"ue5-how-to-use-a-custom-struct-as-tmap-key","status":"publish","type":"asx-lms-tutorial-cpt","link":"https:\/\/store.algosyntax.com\/tutorials\/unreal-engine\/ue5-how-to-use-a-custom-struct-as-tmap-key\/","title":{"rendered":"UE5 How To Use A Custom Struct As TMap Key"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"3861\" class=\"elementor elementor-3861\" data-elementor-post-type=\"asx-lms-tutorial-cpt\">\n\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-ecb734f elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"ecb734f\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-8aebbde\" data-id=\"8aebbde\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-98ef0ff elementor-widget elementor-widget-text-editor\" data-id=\"98ef0ff\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p>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.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-f81388a elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"f81388a\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-4db6d7b\" data-id=\"4db6d7b\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-bd105fd elementor-widget elementor-widget-code-highlight\" data-id=\"bd105fd\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t\t\t\t<div class=\"prismjs-default copy-to-clipboard \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-cpp line-numbers\">\n\t\t\t\t<code readonly=\"true\" class=\"language-cpp\">\n\t\t\t\t\t<xmp>struct  FMyStruct\r\n{\r\n\r\nprivate:\r\n\r\n\tint SomeIntData=0;\r\n\r\npublic:\r\n\tFMyStruct();\r\n\t~FMyStruct();\r\n\r\n\t\/\/Override the comparison operator\r\n\tbool operator==(const FMyStruct& Other) const\r\n\t{\r\n\t\treturn SomeIntData==Other.SomeIntData;\r\n\t}\r\n\r\n};\r\n\r\nFORCEINLINE uint32 GetTypeHash(const FMyStruct& MidiTime)\r\n{\r\n\tuint32 Hash = FCrc::MemCrc32(&MyStruct, sizeof(FMyStruct));\r\n\treturn Hash;\r\n}<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-9b1e164 elementor-widget elementor-widget-heading\" data-id=\"9b1e164\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Required Function Overloads<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-9d666d1 elementor-widget elementor-widget-text-editor\" data-id=\"9d666d1\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p>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&#8217;t Worry).<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-d1bb699 elementor-widget elementor-widget-heading\" data-id=\"d1bb699\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t\t<h3 class=\"elementor-heading-title elementor-size-default\">Comparison Operator ==<\/h3>\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-95bf300 elementor-widget elementor-widget-text-editor\" data-id=\"95bf300\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p>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.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-b1c7177 elementor-widget elementor-widget-code-highlight\" data-id=\"b1c7177\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t\t\t\t<div class=\"prismjs-default copy-to-clipboard \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-cpp line-numbers\">\n\t\t\t\t<code readonly=\"true\" class=\"language-cpp\">\n\t\t\t\t\t<xmp>...\r\n\t\/\/Override the comparison operator\r\n\tbool operator==(const FMyStruct& Other) const\r\n\t{\r\n\t\treturn SomeIntData==Other.SomeIntData;\r\n\t}\r\n\r\n...\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-94fc5d2 elementor-widget elementor-widget-text-editor\" data-id=\"94fc5d2\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p>The TMap has to determine if two potential keys are equal, and it will use this function so that&#8217;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).<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-8a4bfd5 elementor-widget elementor-widget-heading\" data-id=\"8a4bfd5\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t\t<h3 class=\"elementor-heading-title elementor-size-default\">GetTypeHash Overload<\/h3>\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-50e9fe3 elementor-widget elementor-widget-text-editor\" data-id=\"50e9fe3\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p>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.\u00a0<\/p><p>\u00a0<\/p><p>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.\u00a0 Unreal Engine has helper functions to generate hashes. This is how you can easily generate and return a unique hash:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-2882a75 elementor-widget elementor-widget-code-highlight\" data-id=\"2882a75\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t\t\t\t<div class=\"prismjs-default copy-to-clipboard \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-cpp line-numbers\">\n\t\t\t\t<code readonly=\"true\" class=\"language-cpp\">\n\t\t\t\t\t<xmp>\r\n\r\nFORCEINLINE uint32 GetTypeHash(const FMyStruct& MidiTime)\r\n{\r\n\tuint32 Hash = FCrc::MemCrc32(&MyStruct, sizeof(FMyStruct));\r\n\treturn Hash;\r\n}<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-6933b39 elementor-widget elementor-widget-text-editor\" data-id=\"6933b39\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p>The FCrc::MemCrc32() will generate a hash for your struct. Just call the function and return the Hash. You don&#8217;t need to worry about what&#8217;s going on under the hood or what hashes are unless you have a good reason to.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-462cc7f elementor-widget elementor-widget-heading\" data-id=\"462cc7f\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t\t<h4 class=\"elementor-heading-title elementor-size-default\">WHAT IS FORCEINLINE?<\/h4>\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-6f592e9 elementor-widget elementor-widget-text-editor\" data-id=\"6f592e9\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p>This will inline your function. Meaning it instructs the compiler to replace every function call of this function with its contents. It&#8217;s an optimization MACRO. Meaning your code that calls this function will run faster. You can read more about C++ inline functions and what&#8217;s their purpose.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<\/div>\n\t\t","protected":false},"featured_media":0,"template":"","asx-lms-tutorial-categories":[58,45],"asx-lms-tutorial-tags":[46],"class_list":["post-3861","asx-lms-tutorial-cpt","type-asx-lms-tutorial-cpt","status-publish","hentry","asx-lms-tutorial-categories-unreal-c-api","asx-lms-tutorial-categories-unreal-engine","asx-lms-tutorial-tags-cpp","entry"],"acf":[],"_links":{"self":[{"href":"https:\/\/store.algosyntax.com\/asx-rest\/wp\/v2\/asx-lms-tutorial-cpt\/3861","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/store.algosyntax.com\/asx-rest\/wp\/v2\/asx-lms-tutorial-cpt"}],"about":[{"href":"https:\/\/store.algosyntax.com\/asx-rest\/wp\/v2\/types\/asx-lms-tutorial-cpt"}],"version-history":[{"count":0,"href":"https:\/\/store.algosyntax.com\/asx-rest\/wp\/v2\/asx-lms-tutorial-cpt\/3861\/revisions"}],"wp:attachment":[{"href":"https:\/\/store.algosyntax.com\/asx-rest\/wp\/v2\/media?parent=3861"}],"wp:term":[{"taxonomy":"asx-lms-tutorial-categories","embeddable":true,"href":"https:\/\/store.algosyntax.com\/asx-rest\/wp\/v2\/asx-lms-tutorial-categories?post=3861"},{"taxonomy":"asx-lms-tutorial-tags","embeddable":true,"href":"https:\/\/store.algosyntax.com\/asx-rest\/wp\/v2\/asx-lms-tutorial-tags?post=3861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}