#Guid Shenanigans

1 messages · Page 1 of 1 (latest)

wanton sapphire
#

First time messing with System.Guid's so may be struggling due to ignorance. This is Networking related but not directly involving Networking so posting here.

I have this ScriptableObject setup im messing with where I want a unique guid stored on each one

public abstract class ScriptableNetworkObject<T> : ScriptableNetworkObject where T : ScriptableNetworkObject<T>
{
    [NonSerialized] private static Dictionary<Guid, T> dict = new Dictionary<Guid, T>();

    [SerializeField]
    byte[] m_Guid;

    public override Guid Guid => new Guid(m_Guid);

    private void Awake() => Try();
    private void OnEnable() => Try();
    private void OnValidate() => Try();

    private void Try()
    {
        Debug.Log(name + " is trying!" + " (" + Guid + ")");
        TryCreateGuid();
        TryRegister();
    }

    private void TryCreateGuid()
    {
        if (Application.isPlaying) return;
        if (m_Guid == null || m_Guid.Length == 0)
            m_Guid = Guid.NewGuid().ToByteArray();
    }

    private void TryRegister()
    {
        if (!dict.ContainsKey(Guid))
        {
            Debug.Log("Registering Item: " + name + " (" + Guid + ")");
            dict.Add(Guid, this as T);
        }
        else if (dict[Guid] != this)
        {
            Debug.Log("Item: " + name + " Already registered!" + " (" + Guid + ")");
            if (!Application.isPlaying)
            {
                m_Guid = Guid.NewGuid().ToByteArray();
                TryRegister();
            }
        }
    }

As far as I can see the byte arrays on all of the Items im testing are unique in editor but in build the Guid property here used in the dict is logging as identical for all of my Items? Not sure if this is something weird with serialization or guids or what

#

Right now im running this off Awake, OnEnable & OnValidate just for initial prototyping to ensure these are firing in all usecases. I remake the guid if we are in editor in TryRegister() because if you duplicate a pre-existing scriptableobject it will retain the guid from the one I copied

#

I haven't posted the code logging this yet but here's an example of this running in editor vs. in build

#

I think this is a serialization problem, based on opening up the objects in a text editor

#

I'll try making the guid in a custom editor

gilded cliff
#

Honestly, do you really need guids there? Wouldn't a simple index counter be better/simpler/easier to debug?

wanton sapphire
#

So that's what i've been doing normally (Usually I have a scriptableobject per content type that just has a list) but the official BossRoom example plays around with Guid concepts so I'm curious on if they end up feeling better to use

gilded cliff
#

I see.

wanton sapphire
#

I would not be suprised if I end up going back to indicies though 😄