Adding a new Entry in a Struct holding a TArray as Member value doesn't update it's entries

Hi! I am currently working on a Character Customization System where a HUDLayout dynamically create Widgets based on a set of Skins available for the Character Selected. A Skin is represented as a Struct called MaterialInstanceContainer and holds a TArray<TObjectPtr>. Player can mix and match their selection according to the Body Parts they select. In order to achieve the final result, I want to create a TMap<string, MaterialInstanceContainer> so that I can map each BodyParts available for selection with the individual material instance targeting the same BodyPart.

**ISSUE: My issue is as follow, when I try to foreach over my collection of Material Instances inside my Container, I do a string comparison and if the output is valid, I can then break my struct to access the Material Instance Array and ADD to it however, at the very end of the process, the length of the array inside Material Container is still at zero.

How can I add a new entry in the array that my Material Container Struct hold?

Thanks!

This is because function FIND for the map gives you a copy of the value, and not a reference.

You are adding element to the copy, then the copy is immediately discarded, and the original remains empty.

Promote the value (struct or array within) to a variable, add to the variable, then overwrite the value in the map, like so :

Thanks, I wasn’t expecting the FIND node to return by value. I have come up with an alternative that doesn’t use TMap if people are interested.

I have also tested promoting to a local variable and updating the entry in the TMap like you showed but the value returned from the Struct holding the TArray was not of the proper length after the foreach completed its cycle.

Here is a link to an in-depth explanation of the issue and how to fix it. arrays - Adding a new Entry in a Struct holding a TArray as Member value doesn’t update it’s entries - Stack Overflow