-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Feature Request: abstract WrapperComponent class #88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Request: abstract WrapperComponent class #88
Conversation
Hey, nice :) You can definitely do that. I merged the part where the abstract classes are ignored to support the WrapperComponent. While the WrapperComponent might be nice I don't think it should be part of Entitas but rather part of your Entitas tool belt ;) Thanks, and I hope it's ok for you. |
I've felt the same way, very nice solution and I'm definitely going to use it myself. Thanks @LinusVanElswijk |
@LinusVanElswijk I updated the wiki with your WrapperComponent |
@sschmid I wonder if this would fit better into the Entitas project is it was a modification to the code generator. IE, for single field components, generate a convenience accessor that you can call off of an entity. Thoughts? If you think it would fit in the project It's something I can look into once I get some spare time. |
Though instead of another accessor it would probably be better to just add an extension to the component class located in the generated component code. |
I was thinking about modifying the code generator to achieve this, but my main argument why I didn't do it was: If you add another field to the component, lot's of places in your code won't compile anymore. |
Hmm, yeah I understand your concern. Maybe if it was an field attribute: XComponent : IComponent
{
[Entitas.CodeGeneration.ImplicitField]
public int foo;
public string otherNonImplicitField;
} Or maybe if it was a Ruby on Rails style convention over configuration approach: XComponent : IComponent
{
// value is a magicial field name that gets turn into an implicit field
public int value;
public string otherNonImplicitField;
} That way you could keep your code working when you added a second field. Thoughts? |
It's maybe the very first time I wouldn't actually mind some magic :) I only see 1 problem: public class ScoreComponent : IComponent {
public int value;
} var score = e.score; How would you actually get the ScoreComponent from an entity now? |
I would use the implicit operator method that Linus created. So ScoreComponent score = e.score;
int scoreValue = e.score;
var scoreDefault = e.score; // this is a ScoreComponent |
I think it won't make it into Entitas by default for the following reasons: int score = e.score; // returns score.value
var score = e.score; // returns ScoreComponent (Almost) same code, different results, not a big fan of that. Adding new fields in the future will also break this unless I add magic by reserving the field name "value" and treat it differently or I add a new Attribute for the code generator. But of course you can still use the WrapperComponent or easily add a custom code generator to get the result you suggested. But I'll push this responsibility to the application layer and away from Entitas ;) |
I understand your concerns @sschmid. I think the convenience would be nice, but it could lead to weird errors / bugs if the users weren't wary of it. Thanks @LinusVanElswijk for the WrapperComponent. I will use that in future projects. |
Thanks @LinusVanElswijk this is very nice! @sschmid In regards to your concerns, I would argue this is an example of why using |
I liked this idea for simple components, like Name, Info or Score etc. But when I create the NameComponent like this; |
Are you using the Rosyln generator? It was broken in there at one point, though should be fixed now if the asset store has updated. |
Yes, you're right I'm using the external generator. Unfortunately your answer showed me that I got bigger problems, if I auto-import inside Unity, I can generate with Unity but not with Roslyn. If I auto-import in Terminal, I can generate with Roslyn but not with Unity. :( Is that normal? |
Yes. |
In many cases, all a component has to do is hold just a single value.
Components of this type can feel a bit clunky, because you end up writing things like:
The code becomes more concise when an implicit conversion operator is added to these Components:
I added the abstract WrapperComponent class to allow users to create these single value Components including the implicit conversion operator in a single line of code:
This line of code is equivalent to the following: