-
I have entities that can "follow" (move towards) other entities. To implement this behavior i was thinking about using the relationship features of flecs which looks awesome. I read the doc about Relationship and Flecs Query Language. To query the components I'm using something similar to
Here is my test #include <stdio.h>
#include "flecs.h"
typedef struct
{
double x, y;
} Position, Destination;
ECS_COMPONENT_DECLARE(Position);
ECS_COMPONENT_DECLARE(Destination);
ECS_TAG_DECLARE(Follow);
static void UpdateDestination(ecs_iter_t *it)
{
Destination *destination = ecs_field(it, Destination, 0);
const Position *position = ecs_field(it, Position, 2);
for (int i = 0; i < it->count; i++)
{
destination[i] = position[i];
assert(destination[i].x == 123);
assert(destination[i].y == 456);
}
}
int main(int argc, char *argv[])
{
ecs_world_t *world = ecs_init_w_args(argc, argv);
// World configuration
ECS_COMPONENT_DEFINE(world, Position);
ECS_COMPONENT_DEFINE(world, Destination);
ECS_TAG_DEFINE(world, Follow);
ECS_SYSTEM(world, UpdateDestination, EcsOnUpdate, [out] Destination($this), Follow($this, $followee), [in] Position($followee));
// Entity prefab
ecs_entity_t entityPrefab = ecs_entity(world, {.add = ecs_ids(EcsPrefab)});
ecs_set(world, entityPrefab, Position, {0});
ecs_set(world, entityPrefab, Destination, {0});
// Followee
ecs_entity_t followee = ecs_new_w_pair(world, EcsIsA, entityPrefab);
ecs_set(world, followee, Position, {123, 456});
// Follower
ecs_entity_t follower1 = ecs_new_w_pair(world, EcsIsA, entityPrefab);
ecs_add_pair(world, follower1, Follow, followee);
ecs_entity_t follower2 = ecs_new_w_pair(world, EcsIsA, entityPrefab);
ecs_add_pair(world, follower2, Follow, followee);
ecs_progress(world, 0);
ecs_fini(world);
} I'm creating three entities, an entity In the system loop, for the first entity (i = 0) it is working, however the assert is failing for the second entity (i = 1, and above if i'm adding more followers). What am i missing/doing wrong ? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
After doing more tests I think I understand the behavior now. What I was assuming:
How it seems to work:
Understanding that allowed me to adjust my test and this is now working as intended: #include <stdio.h>
#include "flecs.h"
typedef struct
{
double x, y;
} Position, Destination;
ECS_COMPONENT_DECLARE(Position);
ECS_COMPONENT_DECLARE(Destination);
ECS_TAG_DECLARE(Follow);
static void UpdateDestination(ecs_iter_t *it)
{
Destination *destination = ecs_field(it, Destination, 0);
const Position followeePosition = *ecs_field(it, Position, 2);
for (int i = 0; i < it->count; i++)
{
destination[i] = followeePosition ;
assert(destination[i].x == 123);
assert(destination[i].y == 456);
}
}
int main(int argc, char *argv[])
{
ecs_world_t *world = ecs_init_w_args(argc, argv);
// World configuration
ECS_COMPONENT_DEFINE(world, Position);
ECS_COMPONENT_DEFINE(world, Destination);
ECS_TAG_DEFINE(world, Follow);
ECS_SYSTEM(world, UpdateDestination, EcsOnUpdate, [out] Destination($this), Follow($this, $followee), [in] Position($followee));
// Entity prefab
ecs_entity_t entityPrefab = ecs_entity(world, {.add = ecs_ids(EcsPrefab)});
ecs_set(world, entityPrefab, Position, {0});
ecs_set(world, entityPrefab, Destination, {0});
// Followee
ecs_entity_t followee = ecs_new_w_pair(world, EcsIsA, entityPrefab);
ecs_set(world, followee, Position, {123, 456});
// Follower
ecs_entity_t follower1 = ecs_new_w_pair(world, EcsIsA, entityPrefab);
ecs_add_pair(world, follower1, Follow, followee);
ecs_entity_t follower2 = ecs_new_w_pair(world, EcsIsA, entityPrefab);
ecs_add_pair(world, follower2, Follow, followee);
ecs_progress(world, 0);
ecs_fini(world);
} |
Beta Was this translation helpful? Give feedback.
After doing more tests I think I understand the behavior now.
What I was assuming:
position[i]
contains the Position component of the followee for the followeri
How it seems to work:
position
contains only one valueUnderstanding that allowed me to adjust my test and this is now working as intended: