Doraku / DefaultEcs.Analyzer

Roslyn analyzers for DefaultEcs users.
MIT No Attribution
13 stars 1 forks source link

Reduce boilerplate code even more ? #6

Open genaray opened 2 years ago

genaray commented 2 years ago

Well this addon is already awesome for reducing the amount of boilerplate code by a LOT. But i was thinking if we could possible reduce it even further.

ECS like unity or entt ( c++ ) already mastered this by also having an way to iterate over entities like this...

# Unity 
Entities.ForEach((in Entity entity, ref Transform t, ref Velocity vel) => {
   t += vel;
}).Schedule();

Which is very compact and straight forward. Also reduces the boilerplate code to a minimum. I dont know how hard this would be to implement, but i think it would be a cool additional feature to have ( Especially for those who want to combine systems easier ).

What do you think about it ? ^^

Doraku commented 2 years ago

This syntax is actually what made me start this code generator haha. A couple of things to keep in mind:

With that said, if we are ok with the small (? it would be interesting to mesure it) perf loss because of delegate, it should be possible to have some extension methods generated on top of EntityQueryBuilder to automatically create or get the resulting EntitySet and apply the delegate on each entity for you (as long as it does not end like this lol). There may be some internal stuff in DefaultEcs that would need to be made public to make it happens, I will see.

genaray commented 2 years ago

This syntax is actually what made me start this code generator haha. A couple of things to keep in mind:

  • much like my current implementation which require to be inside a specific system type (AEntitySetSystem, ...) unity also require you to be inside their SystemBase type
  • the big difference as you said is that unity allow you to define multiple "sub system" inside a single system while you can only have one with my implementation
  • this captured lambda is probably handled by their code generation to remove the virtual call for better performance, I don't think I would ever be able to do that which is why I opted for the method declaration (with the UpdateAttribute) to keep performance the best I could

With that said, if we are ok with the small (? it would be interesting to mesure it) perf loss because of delegate, it should be possible to have some extension methods generated on top of EntityQueryBuilder to automatically create or get the resulting EntitySet and apply the delegate on each entity for you (as long as it does not end like this lol). There may be some internal stuff in DefaultEcs that would need to be made public to make it happens, I will see.

Thanks for your fast reply ! That actually sounds very good :) Delegates are still insane fast ( i saw a benchmark somewhere stating that they are nearly as fast as normal method calls ).

Just out of curiosity, why isnt it possible to handle this via a code generator ? ^^ I have no experience in that topic, i just thought that such generators could pretty much solve anything :D

Doraku commented 2 years ago

With the way roslyn code generators work, you can only add more code, you can't change existing one. What I suspect Unity of doing is actually replacing the ForEach call in the compiled IL with a direct call with the delegate code instead of calling the delegate (I have used some stuff like this in the past)

genaray commented 2 years ago

With the way roslyn code generators work, you can only add more code, you can't change existing one. What I suspect Unity of doing is actually replacing the ForEach call in the compiled IL with a direct call with the delegate code instead of calling the delegate (I have used some stuff like this in the past)

Alright, thanks for the insight ! :D