How To use the new dynamic enum (update from a patch) template / feature?

Can’t figure out how it is supposed to work.

image

namespace Main;

[Serializable]
public class DynEnum : DynamicEnumBase<DynEnum, DynEnumDefinition>
{
    public DynEnum(string value) : base(value)
    {
    }

    [CreateDefault]
    public static DynEnum CreateDefault()
    {
        return CreateDefaultBase();
    }
}

public class DynEnumDefinition : DynamicEnumDefinitionBase<DynEnumDefinition>
{
    Dictionary<string, object> entries = new Dictionary<string, object>();
    Subject<object> trigger = new Subject<object>();


    public void AddEntry(string name)
    {
        entries[name] = null;
        trigger.OnNext("");
    }

    public void RemoveEntry(string name)
    {
        entries.Remove(name);
        trigger.OnNext("");
    }

    public void ClearEntries()
    {
        entries.Clear();
        trigger.OnNext("");
    }

    protected override IReadOnlyDictionary<string, object> GetEntries()
    {
        return entries;
    }

    protected override IObservable<object> GetEntriesChangedObservable()
    {
        return trigger;
    }

    protected override bool AutoSortAlphabetically => true;
}

When I create the enum I have to pass a value (Foo), which is then in the enum. But as soon as I try to add another entry (Bar) I get an error / warning.

DynEnum

DynEnum.7z (20.8 KB)

Maybe the template code is not correct, here is the reference implementation:

You can try the *.vl docs here to see a patch using it:

If this is the case the devvvvs can consider this a bug report :)

There are two dynamic enum templates: One that says entries can be updated from a patch and the other entries can change programatically at runtime. The latter (and older?) one seems to be the code that is featured in the gray book, it’s similar to the demolib but not quite the same and missing some parts. I haven’t tried it.
The former is prominently featured in the changelog for 6.3 so I assumed it is a newer maybe easier way to do things. The comment in the template says:

// Steps to implement your own enum based on this template:
// 1) Rename “DynamicEnumTemplate” to what your enum should be named
// 2) Rename “DynamicEnumTemplateDefinition” accordingly

(The renaming is done by the wizard.)

When reading this I expect it to work after these steps are performed but it doesn’t.

Here is a demo, showing how to work with a dynamic enum created via the new template “entries can be updated from a patch”. it seems the main confusion is that you don’t create the definition or enum manually. you simply use the type:
DynamicEnumDemo.zip (59.4 KB)

the point of the new template is not to make it easier than the previously existing template. it shows a different usecase of the same dynamic-enum mechanism, ie. one where you need to change entries from within a patch.

Only if you customize the filename, ie. not if you leave the wizard at its defaults.

1 Like