Generate a Unique ID for Each Item?

Hi, is there a way to generate a unique ID for an item at runtime when the item is first created? Something that will not overlap with items in other levels so the ID can be used as the save slot name

In my inventory system I’m trying to save the items in an item container, it didn’t work just using structs because the engine doesn’t like looping structs (Struct A uses struct B as a variable, B uses C, and C uses A - invalidates any structs involved)

My solution to this is to create a SaveGame for item containers, and for each item that exists (either in the world or inside an inventory (I already have the item as a UObject that saves variables for persistence between in the world and in the inventory)). I want to store a unique ID to use as the save slot name (only items that are also item containers, such as bags, will use this save system), so when the item container is opened/game is loaded, it loads the save slot associated with the items unique ID, and loads the inventory from that save slot

I can’t use the name of the item as there can be multiple items with the same name - I can’t use the name of the actor/object because the suffix is based on the order the item is loaded at runtime/placed in the world in the editor, so they will share values with items from different worlds/sessions, and load the wrong item

I’m messing with using a FormatText as “{MainSaveGameSlot}_{Item}__{UniqueID}” where UniqueID is a randomly generated integer set when the item is first created, and added to a list in the GameInstance (which that list is also loaded with the main SaveGame, so it has a reference to every unique ID on this save file, and the randomly generated integer is checked against this array to make sure it doesn’t exist before it’s used), but I feel like this is an unefficient way of doing it which will cause problems in the future, also it’s limited to 4,294,967,296 items in total ever per save file, which sounds like a lot but my game is designed for making the save file last as long as possible, and when one is created each time a stack is split in the inventory for example, that adds up pretty quick

I’m feeling the task can be solved in a simpler manner, but i’m can’t tell exactly how right now.
Anyway, considering the exact question - you can use FGuid as unique identifier. It’s generated randomly, but it’s highly unlikely to have a collision with any other guid in your game

Thanks, GUID’s seem to be working for me!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.