I’ve been trying for a while to create a unique identifier for Actor Component and for now I can’t seem to suceed !
What I want to do is having a component attach to an actor via blueprint or cpp generate a unique ID when dropped in world in editor.
I’ve tried lots of ways but for now I can’t seem to be able to generate a stable unique ID for a component without implementing it in the actor construct below.
I’ve tried a lot of different overiddes taking account of the CDO … but for now it seems impossible
So maybe to be a bit more clear: 1 Actor with 1 component attached to it.
Let’s drop it 200 times in world (the same actor), each of the 200 components on each actor should be uniquely identified.
If the world was to reload or the PIE restart they should keep their ID.
(And no just putting in construct if(Guid.isValid()){ Guid = FGuid::NewGuid()) } won’t work)
I wouldn’t have a Generate ID button – just generate it, post-demarshal, if the field is empty. As in, override the demarshal (or maybe even begin play) hook, to do it there.
Is it possible to invalidate the guid on a duplicate?
I think there’s a post-duplicate callback, but that may not capture all cases (like copy/paste, or import subscene.) You might keep a list of all IDs that have been used, and if you find the ID you load is already in that list, detect it as a duplicate. (You have to remove the ID when the component is disposed, too.)
The “Callback” just calls “Modify” on the component. This is for components which are being freshly loaded in a level (e.g. you added the component to BP, and the actor is just newly being loaded after its BP has been edited/compiled). The next tick is because Modify will automatically fail when the level loads. There could be a better/safer way to do it, but most callbacks I know of are called before the level starts loading.
Testing seems to show that duplication of the actor will still generate unique GUIDs; In the situation of duplication (e.g. directly duplicating the component), you can mark the GUID UPROPERTY as DuplicateTransient.