Xlinka / NeosPlus

NeosVR Plugin Extra Logix nodes and features
https://discord.gg/9QAaMtXwke
Other
29 stars 19 forks source link

ParseEnum node #84

Closed dfgHiatus closed 1 year ago

dfgHiatus commented 1 year ago

There is probably a more efficient way of doing this, but I figured I'd take a stab at it.

Related issue: https://github.com/Neos-Metaverse/NeosPublic/issues/3545

dfgHiatus commented 1 year ago

One thing to note: As enum parsing is relatively expensive in general, this will update the "IsParsed" output only when the input string has been changed at least once. A simple workaround is to compare 2 enums

Screenshot

Xlinka commented 1 year ago

Just get @ProbablePrime opinion if possible if not then we will continue with how it is.

ProbablePrime commented 1 year ago

What... why are you looping?

ProbablePrime commented 1 year ago
using System;

public class Program
{
    enum Season
    {
        Spring,
        Summer,
        Autumn,
        Winter
    }
    public static void Main()
    {
        Season result;
        var e = Enum.TryParse<Season>("Summer", out result);
        if (e)
          Console.WriteLine(result.ToString());
        else
          Console.WriteLine("Cheese");
    }
}

What's the loop for?

dfgHiatus commented 1 year ago

In order to use Enum.TryParse we needed to extend struct. Read about this here

ProbablePrime commented 1 year ago
using System;
using FrooxEngine.LogiX;

namespace FrooxEngine
{
    [Category(new string[] { "LogiX/Input" })]
    [GenericTypes(GenericTypes.Group.CommonEnums)]
    public class ParseEnum<E> : LogixOperator<E> where E Enum, IConvertible
    {
        public readonly Input<string> Canidate;
        public readonly Input<bool> IgnoreCase;
        public readonly Output<bool> IsParsed;

        public override E Content
        {
            get
            {
                E testEnum;
                IsParsed.Value = Enum.TryParse<E>(Canidate.EvaluateRaw(), IgnoreCase.EvaluateRaw(), out testEnum);
                return testEnum; // Logically equivalent to default(E), or the first item in an enumeration
            }
        }
    }
}

Works without the struct

dfgHiatus commented 1 year ago

On my end that isn't the case. I will defer to one of colleagues to confirm this works for them

Frozenreflex commented 1 year ago

It complains about struct on my end as well.

ProbablePrime commented 1 year ago

neos actual, doesnt complain, but either way it works, should be more performant too.

thanks for double checking