Objects should not be created or destroyed mid-tick.
{
for(const auto &s: systems)
s->tick(dt);
+ for(const auto &s: systems)
+ s->deferred_tick();
}
} // namespace Msp::Game
--- /dev/null
+#include "system.h"
+
+namespace Msp::Game {
+
+void System::deferred_tick()
+{
+ for(const auto &f: deferred_queue)
+ f();
+ deferred_queue.clear();
+}
+
+} // namespace Msp::Game
#ifndef MSP_GAME_SYSTEM_H_
#define MSP_GAME_SYSTEM_H_
+#include <functional>
#include <msp/time/timedelta.h>
namespace Msp::Game {
{
protected:
Stage &stage;
+ std::vector<std::function<void()>> deferred_queue;
System(Stage &s): stage(s) { }
public:
Stage &get_stage() const { return stage; }
virtual void tick(Time::TimeDelta) = 0;
+ virtual void deferred_tick();
+
+protected:
+ template<typename F>
+ void defer(F &&f) { deferred_queue.emplace_back(std::forward<F>(f)); }
};
} // namespace Msp::Game