Trying to access TMap data from SaveGame object is nullptr

I am having a problem when trying to use the SaveGame class. I created a custom SaveGame class, that has a member of type “TMap”, which I use to store information about the previous object game state in a struct. I have tried adding objects to the map and it seems to be working fine, as long as I am in the same scene. When I try to access one of the members of the map after loading a new scene (or in my case, the same scene), I get a nullptr when trying to use the same keys to access the maps I made in the previous scene i.e. ContainerIsStored will return false and if I remove it before trying to access the map, the game will crash. Any idea why this may be? Thank you in advance!

// Items stored when saving and changing levels, that need to be reinstantiated
TMap<FString, TArray<FInventoryObjectsStruct>> m_containerItems;

bool UMySaveGame::ContainerIsStored(FString l_containerID)
{
	if(m_containerItems.Find(l_containerID) == nullptr)
	{
		UE_LOG(LogTemp, Error, TEXT("Failed to find container in map!"));
		return false;
	}
	return true;
}

A couple of thoughts about this:

  • You have a TMap where the value is a TArray. I know that’s technically supported, but I don’t think it is when the variable has a UPROPERTY specifier
  • You don’t have a UPROPERTY specifier (at least not one I can see, but if you did I expect it would error out). Again, I think this is required in order to save the data unless you have some custom logic to serialize / deserialize the data to the TMap on load.

If the problem is the fact you can’t have a TArray in a TMap, then perhaps you should look at having a struct as the value rather than TArray. (e.g. TMap. The Struct can contain a TArray.

Sorry if that’s no use - just going off a hunch.

This has helped already - I didn’t have it as a UPROPERTY() and the hot compile complained that I cannot use nested containers. Thanks! When loading, I no longer get a nullptr. However, it seems that my map is getting erased when I load a new level.

My second problem was because some of the members of the struct(s) I was using were not a UPROPERTY as well.