I am in the process of upgrading my project to a newer version of Entitas and I noticed that 0.37.0 is generating void for the component methods, which means I can no longer chain Entity creation, ie, something like below no longer works;
context.ReplaceMoveInput(movement).IsDeleteOnExit(true);
Or for a specific entity,
e.AddTurnBased(i + 1, config.turnDelay)
.IsAIMove(true)
.AddFoodDamager(dmg)
.AddSmoothMove(config.turnDelay)
.AddAudioAttackSource(Audio.scavengers_enemy1, Audio.scavengers_enemy2);
Is there a replacement or new best practice for this functionality? Having a fluent style API was really nice for quickly setting up entities.
It's due to performance. See: https://gitter.im/sschmid/Entitas-CSharp?at=58a356d321d548df2c0c0367
Ok thanks for the link,
The transcript from Simon is as follows:
It鈥檚 actually both - Design and Performance.
Design:
I think Fluent Api makes sense when creating / configuring / building objects. That鈥檚 why Systems.Add() can still be chained. You do it once in the beginning. With entities you do more and different things all the time.
Performance:
Before I moved the whole code base to a type-safe approach, I did several tests regarding generics, methods, method returns. Methods which return sth are generally slower. But to be fair, you have to run this test a lot of times to see a performance difference, but generally it鈥檚 slower :wink:
Both those arguments might not seem strong on their own, but both of them point at the same direction which is removing the return.
But if you鈥檙e life depends on it, or you simply have a different opinion on that, it鈥檚 now a piece of cake to add your own custom code generator which can revive the returns :wink:
I think I'll look into using the Systems.Add() method he mentioned. Does anyone have an example?
I finished converting my example project over to the latest version of Entitas.
Still looking for an example of what Simon was referring to.
Hey @JamesMcMahon,
sorry for removing this, but the good thing is, if you're preferring a different output from the code generator you can easily customize this by adding and using a custom generator.
All current generators can be found here:
https://github.com/sschmid/Entitas-CSharp/tree/develop/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/CodeGenerators
You can create a new class and get inspired (aka copy paste) by existing generators. Put all your custom generators into an Editor folder and you will be able to select them in the Entitas Prefs Window.
You could for example change the ComponentEntityGenerator and create a custom one that return the entity.
public ${ContextName}Entity Add${ComponentName}(${memberArgs}) {
var index = ${Index};
var component = CreateComponent<${ComponentType}>(index);
${memberAssignment}
AddComponent(index, component);
return this;
}
Hope that helps
Ah, I see what Systems.Add was referring to now (that API being fluent but this change made the entities non-fluent).
Thanks for the answer.
Most helpful comment
Hey @JamesMcMahon,
sorry for removing this, but the good thing is, if you're preferring a different output from the code generator you can easily customize this by adding and using a custom generator.
All current generators can be found here:
https://github.com/sschmid/Entitas-CSharp/tree/develop/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/CodeGenerators
You can create a new class and get inspired (aka copy paste) by existing generators. Put all your custom generators into an Editor folder and you will be able to select them in the Entitas Prefs Window.
You could for example change the
ComponentEntityGeneratorand create a custom one that return the entity.https://github.com/sschmid/Entitas-CSharp/blob/develop/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/CodeGenerators/ComponentEntityGenerator.cs#L20-L43
Hope that helps