Copy/Update Component
Scala has a very nice copy functionality for it's case classes. It looks like this:
case class Point(x: Float, y: Float, z: Float)
val p = Point(1.0, 2.0, 3.0)
p.copy(y = 5.0)
I've been able to reproduce a similar functionality with Entitas components in C# using the default parameters, nullables, and partial class functionality. I've added a "Point" class to my codebase but this would work for any component that has multiple properties.
My component looks like:
public class TransformComponent : IComponent
{
public Point position;
public Point rotation;
public Point scale;
}
namespace Entitas {
public partial class Entity {
public Entity CopyTransform(Assets.Models.Point? position = null, Assets.Models.Point? rotation = null, Assets.Models.Point? scale = null) {
var component = this.transform;
component.position = position.GetValueOrDefault(component.position);
component.rotation = rotation.GetValueOrDefault(component.rotation);
component.scale = scale.GetValueOrDefault(component.scale);
ReplaceComponent(ComponentIds.Transform, component);
return this;
}
}
}
Now you get really nice syntax like:
entity.CopyTransform(scale: new Point(5,4,3));
instead of having to do things like:
var t = e.transform
entity.ReplaceTransform(t.position, t.rotation, new Point(5,4,3));
I realize that the code generator could be used to add "CopyXXX" to all Entities but I don't know my way around C# well enough to do that. I was hoping that if the dev team thought this was a good idea that they might find it worth doing.
Great, looks good. I was thinking about that for some time, too. That might be very useful. I could see that being part of Entitas, but I'd like to do some performance tests first to figure out if it has some negative consequences on performance. If not, the CopyXXX could actually become the default ReplaceXXX
I like it, but I prefer the method name "ReplaceXXX", don't change it :cry:
That's what I meant, keep the name 'Replace' and do the suggested logic from the copy method
Okay then :dancer:
Now that I know this, feels imperative to implement it in Entitas asap
I have this line in the code where only changes the index and time (the last two parameters):
e.ReplaceTileAnimation(e.tileAnimation.map, e.tileAnimation.id, e.tileAnimation.frames, nextIndex, Time.time);
Yeah, i should have more fragmented components, but i need all that info to implement tile animation.
Maybe in the next version? :smile:
I've tested a bit and got some numbers. https://gist.github.com/KumoKairo/0ac544ade19f56dbc4c107d03919bb69 In 100 runs of setting the component values for 1 000 000 times the averages:
classical unnamed set: 6-7 ms nullables and .GetValueOrDefault - 18-19 ms
there's also a way to write that a little bit differently:
this.x = x ?? this.x
which turned out to be even slower:
?? operator - 30-32 ms
Plain old if(x != null) yields the same results of 30-32 ms
Judging by IL these parameters get compiled to an array of params, but there's no actual array allocation on invocation. Most likely the performance penalty depends on method invocations and possible cache misses or something (I don't really dig into ASM much, but there are twice as much working lines with one 'call' operator)
Can be solved manually by creating a custom code generator. I recommend duplicating the existing ComponentEntityGenerator and modifying it to your needs. See #265