{"id":3837,"date":"2022-06-02T09:34:38","date_gmt":"2022-06-02T07:34:38","guid":{"rendered":"https:\/\/store.algosyntax.com\/?post_type=asx-lms-tutorial-cpt&#038;p=3837"},"modified":"2026-03-09T14:20:50","modified_gmt":"2026-03-09T12:20:50","slug":"unreal-engine-variables-in-tarray-keep-getting-reset","status":"publish","type":"asx-lms-tutorial-cpt","link":"https:\/\/store.algosyntax.com\/tutorials\/unreal-engine\/unreal-engine-variables-in-tarray-keep-getting-reset\/","title":{"rendered":"Modifying Objects In TArray,TSet or TMap"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"3837\" class=\"elementor elementor-3837\" 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-14e4c04 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"14e4c04\" 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-9faef4c\" data-id=\"9faef4c\" 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-282df74 elementor-widget elementor-widget-text-editor\" data-id=\"282df74\" 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>If you&#8217;re saving an object in a TArray or TMap and it appears not to work, here are a few troubleshooting tips. Sometimes it will appear to you that unreal engine creates and returns a new variable instead of returning the already saved one. You maybe suspecting Garbage Collection but most of the time this is human error.<\/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-e068721 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"e068721\" 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-fc584c7\" data-id=\"fc584c7\" 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-a50e97d elementor-widget elementor-widget-heading\" data-id=\"a50e97d\" 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\">Working With Unreal Containers<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-0d0f5d5 elementor-widget elementor-widget-text-editor\" data-id=\"0d0f5d5\" 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>A common mistake when working with containers is not to get the objects in the container by <strong>reference<\/strong>.<\/p><p>\u00a0<\/p><p>Unreal Engine has data containers like <a href=\"https:\/\/docs.unrealengine.com\/4.27\/en-US\/ProgrammingAndScripting\/ProgrammingWithCPP\/UnrealArchitecture\/TArrays\/\" target=\"_blank\" rel=\"noopener\">TArray<\/a>, <a href=\"https:\/\/docs.unrealengine.com\/4.27\/en-US\/ProgrammingAndScripting\/ProgrammingWithCPP\/UnrealArchitecture\/TMap\/\" target=\"_blank\" rel=\"noopener\">TMap<\/a> and <a href=\"https:\/\/docs.unrealengine.com\/4.27\/en-US\/ProgrammingAndScripting\/ProgrammingWithCPP\/UnrealArchitecture\/TSet\/\" target=\"_blank\" rel=\"noopener\">TSet.<\/a><\/p><p>\u00a0<\/p><p>When working with storing objects in these containers the developer can either insert a pointer to the object or the object itself.<\/p><p>\u00a0<\/p><p>Most of\u00a0 UObjects are pointers and added as pointers. So you&#8217;ll always be dealing with a reference(pointer).<\/p><p>\u00a0<\/p><p>The problem may arise when we use a custom struct or class.\u00a0<\/p><p>\u00a0<\/p><p>We may create an object of that class and insert it into a TArray. Keep in mind we are not inserting a pointer but the object.\u00a0<\/p><p>\u00a0<\/p><p>Now, when we get the object from the TArray with the intention of modifying it, we may forget to get it by <strong>reference<\/strong>. This means we would be getting a copy and modifying the copy the actual object inside the TArray.<\/p><p>\u00a0<\/p><p>The solution is to always get objects by <strong>reference\u00a0<\/strong>when working with containers or better yet, always create pointers to the objects and insert the pointers in the containers instead. By using pointers you&#8217;ll always be sure you are modifying the object.<\/p><p>\u00a0<\/p><p>Here is an example of how not to do it and how to\u00a0 do it, notice the use of the <strong>&amp;<\/strong> reference character:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-d8a0e0c elementor-widget elementor-widget-code-highlight\" data-id=\"d8a0e0c\" 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>\/\/You are working with copies here\r\nfor (auto ObjInArray : TArrayOfObjects)\r\n{}\r\n\r\n\/\/THIS IS THE CORRECT WAY TO DO IT\r\n\/\/Get the reference\r\nfor (auto& ObjInArray : TArrayOfObjects)\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\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-3837","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\/3837","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\/3837\/revisions"}],"wp:attachment":[{"href":"https:\/\/store.algosyntax.com\/asx-rest\/wp\/v2\/media?parent=3837"}],"wp:term":[{"taxonomy":"asx-lms-tutorial-categories","embeddable":true,"href":"https:\/\/store.algosyntax.com\/asx-rest\/wp\/v2\/asx-lms-tutorial-categories?post=3837"},{"taxonomy":"asx-lms-tutorial-tags","embeddable":true,"href":"https:\/\/store.algosyntax.com\/asx-rest\/wp\/v2\/asx-lms-tutorial-tags?post=3837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}