diff --git a/include/flecs/addons/cpp/ref.hpp b/include/flecs/addons/cpp/ref.hpp index ffe6cfa3a9..403221757c 100644 --- a/include/flecs/addons/cpp/ref.hpp +++ b/include/flecs/addons/cpp/ref.hpp @@ -21,10 +21,13 @@ namespace flecs */ template 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(ecs_get_world(world)) + : nullptr; if (!id) { id = _::cpp_type::id(world); } diff --git a/test/cpp_api/project.json b/test/cpp_api/project.json index 6cc4ca815f..add2826e09 100644 --- a/test/cpp_api/project.json +++ b/test/cpp_api/project.json @@ -938,7 +938,8 @@ "non_const_ref", "pair_ref", "pair_ref_w_entity", - "pair_ref_second" + "pair_ref_second", + "from_stage" ] }, { "id": "Module", diff --git a/test/cpp_api/src/Refs.cpp b/test/cpp_api/src/Refs.cpp index bc9799f025..7ed1cdd6ec 100644 --- a/test/cpp_api/src/Refs.cpp +++ b/test/cpp_api/src/Refs.cpp @@ -115,3 +115,12 @@ void Refs_pair_ref_second() { test_int(e.get_second(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({10, 20}); + auto ref = e.get_ref(); + test_int(ref->x, 10); + test_int(ref->y, 20); +}