Skip to content

Commit

Permalink
Update flecs::ref to store world instead of stage (#913)
Browse files Browse the repository at this point in the history
* update ecs_get_ref_id() to handle staged world

Encountered a case using the C++ api where an assert() hit when using
`get_ref().get()`.  This was because the world inside the `flecs::ref`
was actually a stage, and not the world.

Updated `ecs_get_ref_id()` to convert stage to world during lookup.

* Revert "update ecs_get_ref_id() to handle staged world"

This reverts commit 1ca55e4.

* convert stage to world in flecs::ref constructor

From feedback on PR, moved the conversion of stage into workd to the
constructor for flecs::ref.
  • Loading branch information
dmlary authored Jan 17, 2023
1 parent fd82e60 commit 337d189
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
9 changes: 6 additions & 3 deletions include/flecs/addons/cpp/ref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ namespace flecs
*/
template <typename T>
struct ref {
ref(world_t *world, entity_t entity, flecs::id_t id = 0)
: m_world( world )
, m_ref()
ref(world_t *world, entity_t entity, flecs::id_t id = 0)
: m_ref()
{
// the world we were called with may be a stage; convert it to a world
// here if that is the case
m_world = world ? const_cast<flecs::world_t *>(ecs_get_world(world))
: nullptr;
if (!id) {
id = _::cpp_type<T>::id(world);
}
Expand Down
3 changes: 2 additions & 1 deletion test/cpp_api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,8 @@
"non_const_ref",
"pair_ref",
"pair_ref_w_entity",
"pair_ref_second"
"pair_ref_second",
"from_stage"
]
}, {
"id": "Module",
Expand Down
9 changes: 9 additions & 0 deletions test/cpp_api/src/Refs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,12 @@ void Refs_pair_ref_second() {

test_int(e.get_second<Position>(tag)->x, 11);
}

void Refs_from_stage() {
flecs::world world;
flecs::world stage = world.get_stage(0); // get default stage
flecs::entity e = stage.entity().set<Position>({10, 20});
auto ref = e.get_ref<Position>();
test_int(ref->x, 10);
test_int(ref->y, 20);
}

0 comments on commit 337d189

Please sign in to comment.