Download

Casting a TMap possible?

I wanted to know if it is possible to cast a TMap having the same key name name but the container having a different type of pointer compared to the one initialized.

The TMap is of the type


TMap<FName, uint8*>

I want to convert it to type


Map<FName, FEquipmentSlotChanceData*>

Without the conversion, I have to write


FEquipmentSlotChanceData* Asset = (FEquipmentSlotChanceData*)(Map[FName(*SlotName)]);

to get the value.

You can only cast the result of the lookup and not the TMap itself. You could write a template function to do this so it looks more natural.

template< class TYPE > TYPE* MapCast(TMap<Fname, uint8*>& Map, FName Key) { return (TYPE*)Map[SlotName]; }

BTW, be careful when creating FNames as you can trigger memory leaks that way by an ever growing array of names. Better to construct with the NAME_Find flag so that it doesn’t always create if it is missing.

@Joe, will the Templating work even if the TMap is comprised of UObjects? I thought you couldn’t Template any UObject-related items?

EDIT: Ignore that, had a derp moment :stuck_out_tongue: