How to achieve nested TMaps?

Hi all,

In my wave-based gamemode, I’m trying to figure out how to achieve something akin to nested TMaps. The reason I need this is because I have “Squads” which contains a dictionary of units with the number of that unit in the squad, and each squad has a value attached to it for spawning purposes. The layout of what it would look like is this:

Squads = {
		SquadSmall = {
			Grunt: 4, //UnitClass: How many of this unit is in this squad
			GruntMedium: 1,
		}: 5 , //SquadID: Value of Squad (for spawning purposes in wave-based gamemode)
		SquadGrunts = {
			Grunts: 6,
		}: 5,
		SquadMedium = {
			Grunt: 3,
			GruntMedium: 2
		}: 7,
		SquadHeavy = {
			GruntMedium: 4,
			GruntHeavy: 2,
		}: 10,
	}

I know nested containers within UE4/5 is not as straightforward, so how could I best achieve this?

Hi,

It looks from your example that you’re using a Dictionary as the key for the another Dictionary, which would definitely be quite complicated. A simplification would be to move the SquadID into a structure alongside a Dictionary of units, which would look like:

Squads = {
	"SquadSmall": {
		SquadID = 5,
		Units = {
			"Grunt" : 4,
			"GruntMedium" : 1
		}
	},
	"SquadGrunts": {
		SquadID = 5,
		Units = {
			"Grunt" : 6
		}
	},
	"SquadMedium": {
		SquadID = 7,
		Units = {
			"Grunt" : 3,
			"GruntMedium" : 2
		}
	},
	"SquadHeavy": {
		SquadID = 10,
		Units = {
			"GruntMedium" : 4,
			"GruntHeavy" : 2
		}
	}
}

Which in C++ code would look like:


USTRUCT(BlueprintType)
struct FSquadEntry
{
	GENERATED_BODY()
public:

	//Value of Squad (for spawning purposes in wave-based gamemode)
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 SquadID;

	//Unit Class, Quantity
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		TMap<FName, int32> Units;

	//Constructor to make example look nice
    FSquadEntry():SquadID(0){};
	FSquadEntry(int32 ID, TMap<FName, int32> InUnits):SquadID(ID), Units(InUnits){};
};

USTRUCT(BlueprintType)
struct FSquadList
{
	GENERATED_BODY()
public:

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		TMap<FName, FSquadEntry> Squads;

	//Construct dictionary using example data
	FSquadList()
	{
		Squads.Emplace("SquadSmall",
			FSquadEntry(5, {
				{"Grunt",4},
				{"GruntMedium", 1}
			}));
		Squads.Emplace("SquadGrunts",
			FSquadEntry(5, {
				{"Grunt",6}
			}));
		Squads.Emplace("SquadMedium",
			FSquadEntry(7, {
				{"Grunt",3},
				{"GruntMedium", 2}
			}));
		Squads.Emplace("SquadHeavy",
			FSquadEntry(10, {
				{"GruntMedium",4},
				{"GruntHeavy", 2}
			}));
	}
};

Hope that helps!

2 Likes

I forgot to come back here but I had my friend take a look at my code and suggest the same thing. It was really this simple.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.