Using string hashes for indexing quest state in TMap

Context

I’m trying to write my quest system and here’s the basic structure for a quest:

  • A quest is a C++ UObject that is composed, among other things, by objectives
  • Objectives are also UObjects. There is one base abstract class that represents an objective, and derived classes that represents objective types, such as reaching a location or killing an enemy.

I then created multiple blueprints extending my base quest, and due to the way I exposed the quest variables and objectives, I can use the editor to setup everything up for each quest and they are only instanced once the player accepts it. I have one blueprint for each quest in my game and so far it seems to work well.

My question

I’m now trying to save/load the quest state. My initial thoughts are to create a TMap in PlayerState and store the quest states in there (saving only the bare minimum, like what is the index for the current objective for a quest, how many enemies the player has killed already, or if the quest has been completed already).

While I could use the blueprints class for each quest as the key in TMap, that sounds like an overkill, so I was trying to find a way to automatically generate a ID for each quest in a consistent way, meaning that every blueprint quest class needs to map to a unique ID and it needs to be the same ID every time as I need to save this information and be able to retrieve it in a different play session.

My idea was to use the GetTypeHash function using the class name of each quest to generate an integer ID, but I don’t know how the hashing function for strings work in unreal or what are the changes for collisions.

Is that a good approach? Are there any other approaches I could consider?

Type hashing could work.

But you can also use Guid structure as a Key in the map and have a Guid ID = NewGuid() in your quest objectives to identify them.

GUID will vary between gameplays, I believe, and everytime the quest is instanced it will have a different ID. I was looking for a way to get the same ID everytime.

One of the reasons for that is that I intend to have quests that depend on other quests to be complete. If I can consistently map a quest to an Id, then when I’m creating a “child” class, I can provide the class of the quest it depends on, map that to an ID and query my state map if the desired quest has been complete already, for example

Guids will stay consistent if you store them in a UPROPERTY(EditDefaultsOnly) variable. When you create the Blueprint, you can assign a new guid to that property and it’ll stay that way just like any other property you set in a Blueprint asset.

Depending on your long-term plans, it might also be worth looking into primary asset ids. They work well with the Asset Manager, which is something you might have to consider 2 years from now when you have 200 quests you need to load without causing the framerate to suffer.

Asset ids look promising! I wasn’t aware they were a thing. I’m fairly new to unreal engine still trying to get used to all the available tools.

Thanks!