Enum auto-generation
See original GitHub issueHi,
I have a suggestion. There’s a case me (and others on my team) have run into multiple times now, and each time we ended up copy-pasting a bunch of code. I think codegeneration is a much more appropriate way of dealing with this, but I would like to hear from the community as well 👍
using System;
using System.Collections.Generic;
using Entitas;
using Entitas.CodeGeneration.Attributes;
using NUnit.Framework;
using UnityEngine;
// CodeGeneration flag
namespace Entitas.CodeGeneration.Attributes {
public class EnumFlagAttribute : System.Attribute {
}
}
[Game][EnumFlag]
public class StatusComponent : IComponent {
public Status value;
}
public enum Status {
On,
StandBy,
Off
}
public partial class Contexts {
public void DemonstrateEnumFlag() {
var e = game.CreateEntity();
e.ReplaceStatus(Status.Off); // nothing new
// under the hood, it ALSO uses flag components
Assert.IsTrue (e.isStatusOff);
Assert.IsFalse(e.isStatusOn);
Assert.IsFalse(e.isStatusStandBy);
e.ReplaceStatus(Status.On);
Assert.IsTrue (e.isStatusOn);
Assert.IsFalse(e.isStatusOff);
Assert.IsFalse(e.isStatusStandBy);
// this way, you can react to the flags being set
var g = game.GetGroup(GameMatcher.StatusOn);
g.OnEntityAdded += (_g, _e, _i, _c) => {
Debug.Log("STATUS WAS TURNED ON");
};
// it's still a normal enum component, we did not remove any functionality
var g2 = game.GetGroup(GameMatcher.Status);
g2.OnEntityAdded += (_g, _e, _i, _c) => {
Debug.Log("STATUS CHANGED: " + _e.status.value);
};
}
}
Cheers
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top Results From Across the Web
C# Auto generating Class fill Property from enum
Is there away to Auto-generate a full Class Property from an enum or a list? All of my Property will be bool and...
Read more >alembic-autogenerate-enums
Alembic hook that allows enums values to be upgraded and downgraded in migrations automatically.
Read more >Autogenerate doesn't correctly handle postgresql enums
Migrated issue, originally created by Ulrich Petri (@ulope) According to #67 enum types used in columns have to be explicitly created and destroyed....
Read more >enum.auto() in Python
auto() method, we can get the assigned integer value automatically by just using enum.auto() method. ... Automatically assign the integer value to ...
Read more >Auto-generate enum from object keys? : r/typescript
Hello, there is no way to generate an enum "programmatically". The best you can get is a string literal with type MyKeys =...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Simon will probably tell you to write a custom code generator. I think the idea is really good though, and warrants consideration for being part of entitas.
I imagine that generating a flag component for each state of the enum is how you would have to do it for the custom matchers and groups. This could easily explode out into many many components, but that would be your decision when you add the attribute to your component right?
Many of the helper methods could be done without any extra flag components - you could have
e.isStatusOn,e.isStatusOff,e.isStatusStandby, as a wrapper around a simple equality check -> under the hood it would bee.status.value == Status.On, no need for an actual new component generated for each enum state there.They way I usually deal with sth like this is a reactive system that reacts to StatusComponent and in the Filter() I filter for the desired enum value.
@FNGgames knows me by now 😉
If it makes sense for you, you can look into creating a custom code generator for this and share it with us.