Download

Is it possible to define a TMap with more than one value type?

This may be an intensely silly question, but I would really like to use a single TMap to encapsulate a whole bunch of disparate player data with differing types (floats for their age, strings for their name, ESomeEnum for custom data, and so forth), then key it to some ECharacterData enum, so I could do something like the following:



TMap<ECharacterData , supertype> characterMap

characterMap.Add(ECharacterData::CharacterName, "Bob");
characterMap.Add(ECharacterData::CharacterAge, 46.25f);
characterMap.Add(ECharacterData::CharacterBloodType, EBloodType::OPositive);

Is this at all possible? All the documentation I’ve found seems to assume that you want your maps to use a single value type, but it would be considerably more flexible if I could wrap a variety of values into a single dictionary.

You can add an USTRUCT as value.

Hmm, that would work as a wrapper, but wouldn’t it in turn require me to make one big struct with every possible value type?

You can add in a struct as many variables as you want.

Right, but I mean, say the tvalue I want to store could be an int, float, string or vector; if I wrap everything in a struct, wouldn’t that mean that I’d be reserving four variables’ worth of memory just to store data in one?

I mean the key would be the player ID (for example player 0), and the value would be the player data stored in USTRUCT (whatever you want to store). But for your purposes would be better if you use a player class and then store all data there.

Hmm okay, thank you very much for the advice :slight_smile:

If you really want to store different primitive type as value, you can store all as FString like a Json.

First, you need to extends TMap class, to add some functions : getFloatValue(key); getIntegerValue(key); getStringValue(key); getEnumValue(key) and the same for setter, in those functions you will check if enum is compatible with the variable type.
You can convert to and as String with the unreal engine reflection system, Rama show how to do that in this wiki (at the end ;)).
And for Integer, Float, etc… you can find this useful wiki (again done by Rama).

But like Scienziatogm say, it will be better to make an Player data USTRUCT, also take a look to PlayerState to store individual player data :slight_smile:

I hope this help you !