From c9d5251c71bb994de365801beb453fb61922da31 Mon Sep 17 00:00:00 2001 From: John McCardle Date: Fri, 15 Mar 2024 20:00:57 -0400 Subject: [PATCH] In-place map modification worked --- src/GameEngine.cpp | 16 +++++++++++++--- src/PyCallable.cpp | 5 +++++ src/PyCallable.h | 2 ++ src/scripts/game.py | 5 +++++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/GameEngine.cpp b/src/GameEngine.cpp index 74ed443..6c3d95d 100644 --- a/src/GameEngine.cpp +++ b/src/GameEngine.cpp @@ -77,7 +77,8 @@ void GameEngine::manageTimer(std::string name, PyObject* target, int interval) { //Py_DECREF(timers[name].target); std::cout << "Erasing a timer" << std::endl; - timers.erase(it); + //timers.erase(it); + timers[name] = std::make_shared(Py_None, 1000, runtime.getElapsedTime().asMilliseconds()); std::cout << "It was erased" << std::endl; return; } @@ -94,9 +95,18 @@ void GameEngine::manageTimer(std::string name, PyObject* target, int interval) void GameEngine::testTimers() { int now = runtime.getElapsedTime().asMilliseconds(); - for (auto& [name, timer]: timers) + //for (auto& [name, timer]: timers) + auto it = timers.begin(); + while (it != timers.end()) { - timer->test(now); + it->second->test(now); + + if (it->second->isNone()) + { + it = timers.erase(it); + } + else + it++; } } diff --git a/src/PyCallable.cpp b/src/PyCallable.cpp index dee7884..2aca5c2 100644 --- a/src/PyCallable.cpp +++ b/src/PyCallable.cpp @@ -23,6 +23,11 @@ PyObject* PyCallable::call(PyObject* args, PyObject* kwargs) return PyObject_Call(target, args, kwargs); } +bool PyCallable::isNone() +{ + return (target == Py_None || target == NULL); +} + PyTimerCallable::PyTimerCallable(PyObject* _target, int _interval, int now) : PyCallable(_target), interval(_interval), last_ran(now) {} diff --git a/src/PyCallable.h b/src/PyCallable.h index b59e774..ae828c7 100644 --- a/src/PyCallable.h +++ b/src/PyCallable.h @@ -9,6 +9,8 @@ protected: PyCallable(PyObject*); ~PyCallable(); PyObject* call(PyObject*, PyObject*); +public: + bool isNone(); }; class PyTimerCallable: public PyCallable diff --git a/src/scripts/game.py b/src/scripts/game.py index 5684bc2..18e46d0 100644 --- a/src/scripts/game.py +++ b/src/scripts/game.py @@ -90,6 +90,7 @@ def remove_random(): print("done") import random +import time def stress_test(*args): global running global timers @@ -112,6 +113,10 @@ def stress_test(*args): #print(timers) print("Segfaultin' time") mcrfpy.delTimer("recurse") + print("Does this still work?") + time.sleep(0.5) + print("How about now?") + stress_test()