In-place map modification worked

This commit is contained in:
John McCardle 2024-03-15 20:00:57 -04:00
parent 0a8f67e391
commit c9d5251c71
4 changed files with 25 additions and 3 deletions

View File

@ -77,7 +77,8 @@ void GameEngine::manageTimer(std::string name, PyObject* target, int interval)
{ {
//Py_DECREF(timers[name].target); //Py_DECREF(timers[name].target);
std::cout << "Erasing a timer" << std::endl; std::cout << "Erasing a timer" << std::endl;
timers.erase(it); //timers.erase(it);
timers[name] = std::make_shared<PyTimerCallable>(Py_None, 1000, runtime.getElapsedTime().asMilliseconds());
std::cout << "It was erased" << std::endl; std::cout << "It was erased" << std::endl;
return; return;
} }
@ -94,9 +95,18 @@ void GameEngine::manageTimer(std::string name, PyObject* target, int interval)
void GameEngine::testTimers() void GameEngine::testTimers()
{ {
int now = runtime.getElapsedTime().asMilliseconds(); 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++;
} }
} }

View File

@ -23,6 +23,11 @@ PyObject* PyCallable::call(PyObject* args, PyObject* kwargs)
return PyObject_Call(target, args, kwargs); return PyObject_Call(target, args, kwargs);
} }
bool PyCallable::isNone()
{
return (target == Py_None || target == NULL);
}
PyTimerCallable::PyTimerCallable(PyObject* _target, int _interval, int now) PyTimerCallable::PyTimerCallable(PyObject* _target, int _interval, int now)
: PyCallable(_target), interval(_interval), last_ran(now) : PyCallable(_target), interval(_interval), last_ran(now)
{} {}

View File

@ -9,6 +9,8 @@ protected:
PyCallable(PyObject*); PyCallable(PyObject*);
~PyCallable(); ~PyCallable();
PyObject* call(PyObject*, PyObject*); PyObject* call(PyObject*, PyObject*);
public:
bool isNone();
}; };
class PyTimerCallable: public PyCallable class PyTimerCallable: public PyCallable

View File

@ -90,6 +90,7 @@ def remove_random():
print("done") print("done")
import random import random
import time
def stress_test(*args): def stress_test(*args):
global running global running
global timers global timers
@ -112,6 +113,10 @@ def stress_test(*args):
#print(timers) #print(timers)
print("Segfaultin' time") print("Segfaultin' time")
mcrfpy.delTimer("recurse") mcrfpy.delTimer("recurse")
print("Does this still work?")
time.sleep(0.5)
print("How about now?")
stress_test() stress_test()