44
55static PyObject *py_ue_ftimerhandle_clear (ue_PyFTimerHandle *self, PyObject * args)
66{
7- self->world ->GetTimerManager ().ClearTimer (self->thandle );
8- Py_INCREF (Py_None);
9- return Py_None;
7+ if (!self->world .IsValid ())
8+ return PyErr_Format (PyExc_Exception, " World's timer is invalid" );
9+ self->world .Get ()->GetTimerManager ().ClearTimer (self->thandle );
10+ Py_RETURN_NONE;
1011}
1112
1213static PyObject *py_ue_ftimerhandle_pause (ue_PyFTimerHandle *self, PyObject * args)
1314{
14- self->world ->GetTimerManager ().PauseTimer (self->thandle );
15- Py_INCREF (Py_None);
16- return Py_None;
15+ if (!self->world .IsValid ())
16+ return PyErr_Format (PyExc_Exception, " World's timer is invalid" );
17+ self->world .Get ()->GetTimerManager ().PauseTimer (self->thandle );
18+ Py_RETURN_NONE;
1719}
1820
1921static PyObject *py_ue_ftimerhandle_unpause (ue_PyFTimerHandle *self, PyObject * args)
2022{
21- self->world ->GetTimerManager ().UnPauseTimer (self->thandle );
22- Py_INCREF (Py_None);
23- return Py_None;
23+ if (!self->world .IsValid ())
24+ return PyErr_Format (PyExc_Exception, " World's timer is invalid" );
25+ self->world .Get ()->GetTimerManager ().UnPauseTimer (self->thandle );
26+ Py_RETURN_NONE;
2427}
2528
2629
@@ -37,7 +40,14 @@ static PyMethodDef ue_PyFTimerHandle_methods[] = {
3740// destructor
3841static void ue_pyftimerhandle_dealloc (ue_PyFTimerHandle *self)
3942{
40- Py_DECREF (self->py_callable );
43+ if (self->world .IsValid ())
44+ {
45+ self->world .Get ()->GetTimerManager ().ClearTimer (self->thandle );
46+ }
47+ if (self->delegate_ptr .IsValid ())
48+ {
49+ self->delegate_ptr .Reset ();
50+ }
4151 Py_TYPE (self)->tp_free ((PyObject *)self);
4252}
4353
@@ -107,26 +117,24 @@ PyObject *py_ue_set_timer(ue_PyUObject *self, PyObject * args)
107117 if (!world)
108118 return PyErr_Format (PyExc_Exception, " unable to retrieve UWorld from uobject" );
109119
110- FTimerDelegate timer_delegate;
111- TSharedRef<FPythonSmartDelegate> py_delegate = MakeShareable (new FPythonSmartDelegate);
112- py_delegate->SetPyCallable (py_callable);
113-
114- timer_delegate.BindSP (py_delegate, &FPythonSmartDelegate::Void);
115120
116- FTimerHandle thandle;
117- world->GetTimerManager ().SetTimer (thandle, timer_delegate, rate, loop, first_delay);
118121
119122 ue_PyFTimerHandle *ret = (ue_PyFTimerHandle *)PyObject_New (ue_PyFTimerHandle, &ue_PyFTimerHandleType);
120123 if (!ret)
121124 {
122- world->GetTimerManager ().ClearTimer (thandle);
123125 return PyErr_Format (PyExc_Exception, " unable to allocate FTimerHandle python object" );
124126 }
125- ret->thandle = thandle;
126- ret->py_callable = py_callable;
127- // will be decref'ed on clear
128- Py_INCREF (ret->py_callable );
129- ret->world = world;
127+
128+ FTimerDelegate timer_delegate;
129+ TSharedRef<FPythonSmartDelegate> py_delegate = MakeShareable (new FPythonSmartDelegate);
130+ py_delegate->SetPyCallable (py_callable);
131+
132+ timer_delegate.BindSP (py_delegate, &FPythonSmartDelegate::Void);
133+
134+ world->GetTimerManager ().SetTimer (ret->thandle , timer_delegate, rate, loop, first_delay);
135+
136+ new (&ret->delegate_ptr ) TSharedPtr<FPythonSmartDelegate>(py_delegate);
137+ ret->world = TWeakObjectPtr<UWorld>(world);
130138
131139 return (PyObject *)ret;
132140}
0 commit comments