Download

TMap of TMaps

Hey guys,

So i’m trying to have a TMap of TMap - Weirdly worded so I’ll try and explain

What I’m trying to do is have:



*
// MAPTEST.h//


typedef TMap<FString, FString> SeedData;

enum SeedType{
	Apple_Seed,
	Potato_Seed,
};
class INVENTORYTESTS_API MapTest
{
public:
	MapTest();
	~MapTest();

	static TMap<int, SeedData> Seeds; //*EXAMPLE* so 0 (enum) brings up data for Apple_Seed
	static TMap<int, SeedData> SetupSeeds();
private:
	
};

// !MAPTEST.h //


// MAPTEST.CPP //
MapTest::MapTest()
{
	Seeds = SetupSeeds();
}

MapTest::~MapTest()
{
}

TMap<int, SeedData> MapTest::SetupSeeds()
{
	// setup out seed data container
	TMap<int, SeedData> s;
	
	// Apple
	SeedData d;
	d.Add("label", "Apple Seed");
	d.Add("amount_of_meshes", "3");
	
	s.Add(0, d);
	
	// Potato
	SeedData e;
	e.Add("label", "Potato Seed");
	e.Add("amount_of_meshes", "3");
	
	s.Add(1, e);
	return s;
}

//! MAPTEST.CPP //*


If I am correct you can’t do this? As the type has to have a constructor and == operator etc.

I’m basically looking for confirmation that this can’t be done because I am just getting a massive error which basically says: Needs constructor for Seed Data
or if it can be done please let me know how.

I haven’t tried this but you should check the way I was using it in Java:

TMap<int, TMap<FString, FString>>

In theory it should work.

Personally, when it comes to 2D arrays (and extended to 2D maps), I prefer to just wrap them in a struct and make a simple accessor / setter function. Depending on your performance needs it can save a lot of headaches for just one extra instruction.

Also, again depending on performance concerns, TArrays can use find and are usable as UPROPERTY’s, so unless you are accessing multiple times per tick or you data is huge it can sometimes be the more sensible choice.

I don’t know if it’s impossible, but it’s probably best to avoid it to be honest. I know you can’t have nested containers as UPROPERTY()'s, so you have to manage your objects yourself which can be a pain. Same goes with 2D TArrays.

I notice you’re using C++'s default ‘int’ as the type, You really want to stick to Unreal integer types, especially when using it’s containers.

Nested containers are not supported, no.

Yup. This is the correct answer.

Think of the TMap as a list of objects which are indexed by a hash value.
A TMap is an object as well, so you can have a hash set of hash sets.

Keep in mind that the TMap is not supported by the editor itself, so you can’t directly expose it to end users via the UPROPERTY() macro. If you want to expose access to a TMap, you’d have to create functions which act as accessors to your internal data structure. Likewise with nested TArray’s: TArray<TArray<int32>> MyArray;

Pro tip: Try to keep your key value an integer value. It’s much faster to do integer comparisons than string comparisons. If you want something human readable, using enums for keys works perfectly.