Algosyntax Logo

Modifying Objects In TArray,TSet or TMap

If you’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.

Working With Unreal Containers

A common mistake when working with containers is not to get the objects in the container by reference.


Unreal Engine has data containers like TArray, TMap and TSet.


When working with storing objects in these containers the developer can either insert a pointer to the object or the object itself.


Most of  UObjects are pointers and added as pointers. So you’ll always be dealing with a reference(pointer).


The problem may arise when we use a custom struct or class. 


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. 


Now, when we get the object from the TArray with the intention of modifying it, we may forget to get it by reference. This means we would be getting a copy and modifying the copy the actual object inside the TArray.


The solution is to always get objects by reference 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’ll always be sure you are modifying the object.


Here is an example of how not to do it and how to  do it, notice the use of the & reference character:

					//You are working with copies here
for (auto ObjInArray : TArrayOfObjects)

//Get the reference
for (auto& ObjInArray : TArrayOfObjects)

MIDI Engine 3 For UE5


Gamechanger plugin for notehighway rhythm games and music visualizers


Add Text and 2D UMG widgets to Level Sequences

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!