Skip to content

Enum casting is weird (especially for synchronization) #178

@JLChnToZ

Description

@JLChnToZ

Please refer to following syntax inside OnPreSerialization(), it is syntactically (and logically) correct in C# (since it was just casting the underly primitive type), I was expect the first and second also works in U#, but it didn't, only the third workaround works.

using UnityEngine;
using UdonSharp;
using VRC.SDKBase;

[UdonBehaviourSyncMode(BehaviourSyncMode.Manual)]
public class Enum2ByteTest : UdonSharpBehaviour
{
    [SerializeField] TestEnum someEnum = TestEnum.Two;
    [UdonSynced] byte syncdEnumAsByte;

    public override void Interact()
    {
        if (!Networking.IsOwner(gameObject)) Networking.SetOwner(Networking.LocalPlayer, gameObject);
        RequestSerialization();
    }

    public override void OnPreSerialization()
    {
        // This will cause serialization failed warning and fail to synchronize
        syncdEnumAsByte = (byte)someEnum;

        // This also fails
        syncdEnumAsByte = (byte)(int)someEnum;

        // This works
        int intEnum = (int)someEnum;
        syncdEnumAsByte = (byte)intEnum;
    }
}

public enum TestEnum
{
    Zero,
    One,
    Two,
    Three,
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions