Camobiwon
Camobiwon9mo ago

GUIDManager

I posted a very old version of this a long time ago (https://canary.discord.com/channels/393839515074297858/677646675560366141/981127682437836812) but have improved this script a lot over time and figured I would share a much better version of it until we get some official GUID component. This is intended for you to add a GUID int to your model, syncing it on start, and registering it to the GUIDManager's dictionary. Using it is extremely simple, let's say you had a Ball component reference you wanted to sync, first you'd make your ball component either derive from the simple GUIDModel pre-implemented one, or simply add your own int to your existing model (Check GUID model for how to set it up in OnRealtimeModelReplaced). Then in some other script that wants to send a reference to the ball, simply send the GUID of it (Typically ball.GUID as the ID reference), then when it is received on your local or remote client, simply do GUIDManager.FetchComponent<Ball>(guidOfBall) or alternatively GUIDManager.TryFetchComponent(guidOfBall, out Ball ball) if you prefer the bool return type for if it was found in the dictionary. My old implementation used to use strings of GUIDs to send for reference but I found this was very inefficient and changed it over entirely to ints. ints are 4 bytes compared to the GUID string being about 80, dictionary lookup hashing is literally free as the int is it's own hashcode, and many other efficiencies are gained over strings, and still allows for about 4 billion entries which is a crazy high amount. Hopefully you get some use out of this and find it useful :)
3 Replies
Yips
Yips8mo ago
@Camobiwon Thanks for this. I was looking at your previous implementation from a while back and just recently found this updated one One issue I am running into with a GUID system to pass references is waiting for the models to be replaced. If you connect to a room and one model needs a synced guid reference to another model how do you go about waiting for the other model replacement and GUID setup to be finished before replacing the current model? Do you just push the FetchComponent logic to OnStart or have some static callback for when everything is registered? For example connecting to an existing room with a Dog Component that contains a ballID reference.
Camobiwon
CamobiwonOP8mo ago
Yo good question, I already ran into the same problem before, I feel like the fix I do was kinda hacky but it did work. Basically if something does have a race condition like that, then trigger a coroutine / task to wait until the GUIDManager contains that value, then when it does fetching it from there. Can get a screenshot in a sec @Yips This is how I approach it, using UniTask for the record
if(GUIDManager.ContainsGUID(guid)) {
DoSomething(guid);
} else { //GUID was not present in the dictionary, this was likely due to something happening before it got registered, wait for it..
WaitForGUID(guid).Forget();
}
if(GUIDManager.ContainsGUID(guid)) {
DoSomething(guid);
} else { //GUID was not present in the dictionary, this was likely due to something happening before it got registered, wait for it..
WaitForGUID(guid).Forget();
}
private async UniTaskVoid WaitForGUID(int guid) {
await UniTask.WaitUntil(() => GUIDManager.ContainsGUID(guid));
DoSomething(guid);
}
private async UniTaskVoid WaitForGUID(int guid) {
await UniTask.WaitUntil(() => GUIDManager.ContainsGUID(guid));
DoSomething(guid);
}
Yips
Yips8mo ago
Awesome! I'll give it a shot, thanks a bunch for sharing