Answer the question
In order to leave comments, you need to log in
How to organize Tween system within ECS (entt)?
There was a need for a tween system for the game. Almost all logic in the game is tied to ecs. I am using the entt library.
The idea came up as follows: I made a component with data about twin.
struct TweenComponent
{
EasingType type;
float delay;
float fromValue;
float toValue;
float duration;
};
void update(entt::registry& registry, float dt)
{
auto view = registry.view<TweenComponent>();
for(auto entity: view)
{
auto tween = registry.get<TweenComponent>(entity);
...
}
}
Answer the question
In order to leave comments, you need to log in
In this case, one component and one system is not enough. When an arbitrary number of other components are affected, linking them all to any one component is like burying the ECS back into the ground.
Why single component design is doomed. Every system in the ECS must work with component sampling. In EnTT, this is done using the component flow representation - entt::registry::view
.
Such a representation optimally organizes a sequence of entities that exactly contain the components indicated in the representation. If you start checking for the presence of other components in an entity, in addition to being contrary to the concept of ECS, this will affect fetching from memory outside the map and greatly reduce system performance. Therefore, the system should not work with components outside of its selection.
By design, you want to make a system that will interpolate arbitrary values of arbitrary fields of arbitrary components using the data of one particular component. Such a design will only lead you to the fact that in this system you will have to make a selection for all components, which, in fact, will make this system dysfunctional. This is simply because not all entities, the values of whose components you want to interpolate, will have all the other components, the values of which the system can also interpolate.
How to be in that case. You need to isolate functionality into more local systems instead of one big interpolation system. Here you have an entity color component - ColorComponent
, and you can interpolate this color. So you need a color interpolation component -ColorInterpolationComponent
. In this interpolation component, you need to define the interpolation law, the initial and final values, as well as the interpolation time. ColorInterpolationComponent
says that all data is ColorComponent
interpolated according to a certain law between certain values and for a certain time.
Your interpolation functions are always clean, i.e. depend only on the arguments passed to them. For any data type, from scalar to matrix, you can define an interpolation function between two values. These are all things external to the ECS, and inside the ECS ***InterpolationComponent
you will have your own system for each, which makes a selection by the target component and by the interpolation component. Thus, already at the stage of selecting components, you will have only suitable entities, and all the rest will be filtered out.
It is also worth noting that this approach allows one specific value to be interpolated by only one function at a time. If multiple simultaneous interpolations are required for a single value, this approach is no longer suitable. On the other hand, in order to correctly converge several such animations for a single value, a much more complex system is required than a simple interpolation function.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question