Mutate world from callbacks of third-party libraries #1050
-
Hi, In this case I'm using TGUI as my UI library and the scene example from https://github.com/SanderMertens/flecs/blob/master/examples/cpp/game_mechanics/scene_management/src/main.cpp gui->get<tgui::Button>("PlayButton")->onClick.connect([](auto) {
// how to do this?
world.add<ActiveScene, GameScene>();
}); In this case I want to switch to the game scene when the play button is pressed. Now I'm getting a little bit lost: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
You can capture variables in a lambda, so: flecs::world world;
// ...
gui->get<tgui::Button>("PlayButton")->onClick.connect([&](auto) { // note the [&]
world.add<ActiveScene, GameScene>();
}); If you don't want modifications to take effect directly, you can put them between world.defer_begin();
world.add<ActiveScene, GameScene>();
world.defer_end(); // add is executed here |
Beta Was this translation helpful? Give feedback.
Ah, that's a bit less straightforward.
What you can do is capture
flecs::world_t*
by value:I'll keep this use case in mind, I don't like how convoluted it is. Ideally the world object would be copyable, but that's a thorny problem to address (something to do with RAII and how copy constructors (don't) work).