From 03d89503617a1e0fa2b20c7e189b7c204057b9a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6kalp=20=C3=96zcan?= Date: Sat, 3 Jan 2026 12:44:00 +1100 Subject: [PATCH] Add child-prefab-matching repro remove binaries --- _REPRO/child-prefab-matching/.gitignore | 2 + _REPRO/child-prefab-matching/Troll.flecs | 3 + _REPRO/child-prefab-matching/build-repro.sh | 5 ++ _REPRO/child-prefab-matching/repro.cpp | 63 +++++++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 _REPRO/child-prefab-matching/.gitignore create mode 100644 _REPRO/child-prefab-matching/Troll.flecs create mode 100755 _REPRO/child-prefab-matching/build-repro.sh create mode 100644 _REPRO/child-prefab-matching/repro.cpp diff --git a/_REPRO/child-prefab-matching/.gitignore b/_REPRO/child-prefab-matching/.gitignore new file mode 100644 index 0000000000..cddf6bb0d6 --- /dev/null +++ b/_REPRO/child-prefab-matching/.gitignore @@ -0,0 +1,2 @@ +flecs.o +repro diff --git a/_REPRO/child-prefab-matching/Troll.flecs b/_REPRO/child-prefab-matching/Troll.flecs new file mode 100644 index 0000000000..5979d45d98 --- /dev/null +++ b/_REPRO/child-prefab-matching/Troll.flecs @@ -0,0 +1,3 @@ +prefab Troll : Enemy { + HitPoints: {value: 20} +} diff --git a/_REPRO/child-prefab-matching/build-repro.sh b/_REPRO/child-prefab-matching/build-repro.sh new file mode 100755 index 0000000000..fa8351b614 --- /dev/null +++ b/_REPRO/child-prefab-matching/build-repro.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +set -ex + +gcc-15 -c ../../distr/flecs.c -std=c99 -O1 -o flecs.o +g++-15 repro.cpp flecs.o -o repro -std=c++17 -lpthread diff --git a/_REPRO/child-prefab-matching/repro.cpp b/_REPRO/child-prefab-matching/repro.cpp new file mode 100644 index 0000000000..1d7a17215f --- /dev/null +++ b/_REPRO/child-prefab-matching/repro.cpp @@ -0,0 +1,63 @@ +#include +#include + +#include "../../distr/flecs.h" + +int main(int argc, char* argv[]) { + flecs::world world; + + struct HitPoints { + float value; + }; + world.component("HitPoints") + .member("value") + .set({ float(10.0) }); + + flecs::entity enemy = world.prefab("Enemy"); + + if (world.script_run_file("Troll.flecs") != 0) { + std::cerr << "Error running script" << std::endl; + return 1; + } + + flecs::entity Troll = world.lookup("Troll"); + flecs::entity instance = world.entity().is_a(Troll); + + auto q = world.query_builder() + .with(flecs::IsA, enemy); + + // The original pattern as in https://github.com/VoxInteractive/Surwave/blob/4aa069c7fb4ccb292ffe0cd4690ec6023ccb1be6/Game/cpp/systems/enemy_death.h#L44 + q.each([](flecs::entity e) { + std::cout << e.target(flecs::IsA).name() << std::endl; // prints Troll + }); + + // Recommended pattern (faster) - https://discord.com/channels/633826290415435777/1455553733978099763/1455641967605059854 + q.each([](flecs::iter& it, size_t row) { + std::cout << it.pair(0).second().name() << std::endl; // prints Troll + }); + + auto repro_system = world.system("Repro") + .with(flecs::IsA, enemy) + .kind(0) + .run([](flecs::iter& it) { + + while (it.next()) { + for (auto entity_index : it) { + flecs::entity entity = it.entity(static_cast(entity_index)); + + // The original pattern + const flecs::entity prefab_entity_via_entity_target = entity.target(flecs::IsA); + + // Recommended pattern + const flecs::entity prefab_entity_via_it_pair = it.pair(1).second(); + + std::cout << prefab_entity_via_entity_target.name() << std::endl; // prints Troll + std::cout << prefab_entity_via_it_pair.name() << std::endl; // !!! prints Enemy + } + } + }); + + repro_system.run(); + + return 0; +}