|
| 1 | +#include "UnrealEnginePythonPrivatePCH.h" |
| 2 | + |
| 3 | +static PyObject *py_ue_ftimerhandle_clear(ue_PyFTimerHandle *self, PyObject * args) { |
| 4 | + self->world->GetTimerManager().ClearTimer(self->thandle); |
| 5 | + Py_INCREF(Py_None); |
| 6 | + return Py_None; |
| 7 | +} |
| 8 | + |
| 9 | +static PyObject *py_ue_ftimerhandle_pause(ue_PyFTimerHandle *self, PyObject * args) { |
| 10 | + Py_INCREF(Py_None); |
| 11 | + return Py_None; |
| 12 | +} |
| 13 | + |
| 14 | +static PyObject *py_ue_ftimerhandle_unpause(ue_PyFTimerHandle *self, PyObject * args) { |
| 15 | + Py_INCREF(Py_None); |
| 16 | + return Py_None; |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +static PyMethodDef ue_PyFTimerHandle_methods[] = { |
| 21 | + |
| 22 | + // Transform |
| 23 | + |
| 24 | + { "clear", (PyCFunction)py_ue_ftimerhandle_clear, METH_VARARGS, "" }, |
| 25 | + { "pause", (PyCFunction)py_ue_ftimerhandle_pause, METH_VARARGS, "" }, |
| 26 | + { "unpause", (PyCFunction)py_ue_ftimerhandle_unpause, METH_VARARGS, "" }, |
| 27 | + { NULL } /* Sentinel */ |
| 28 | +}; |
| 29 | + |
| 30 | +// destructor |
| 31 | +static void ue_pyftimerhandle_dealloc(ue_PyFTimerHandle *self) { |
| 32 | + Py_DECREF(self->py_callable); |
| 33 | + Py_TYPE(self)->tp_free((PyObject *)self); |
| 34 | +} |
| 35 | + |
| 36 | +static PyTypeObject ue_PyFTimerHandleType = { |
| 37 | + PyVarObject_HEAD_INIT(NULL, 0) |
| 38 | + "unreal_engine.FTimerHandle", /* tp_name */ |
| 39 | + sizeof(ue_PyFTimerHandle), /* tp_basicsize */ |
| 40 | + 0, /* tp_itemsize */ |
| 41 | + (destructor)ue_pyftimerhandle_dealloc, /* tp_dealloc */ |
| 42 | + 0, /* tp_print */ |
| 43 | + 0, /* tp_getattr */ |
| 44 | + 0, /* tp_setattr */ |
| 45 | + 0, /* tp_reserved */ |
| 46 | + 0, /* tp_repr */ |
| 47 | + 0, /* tp_as_number */ |
| 48 | + 0, /* tp_as_sequence */ |
| 49 | + 0, /* tp_as_mapping */ |
| 50 | + 0, /* tp_hash */ |
| 51 | + 0, /* tp_call */ |
| 52 | + 0, /* tp_str */ |
| 53 | + 0, /* tp_getattro */ |
| 54 | + 0, /* tp_setattro */ |
| 55 | + 0, /* tp_as_buffer */ |
| 56 | + Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 57 | + "Unreal Engine FTimerHandle", /* tp_doc */ |
| 58 | + 0, /* tp_traverse */ |
| 59 | + 0, /* tp_clear */ |
| 60 | + 0, /* tp_richcompare */ |
| 61 | + 0, /* tp_weaklistoffset */ |
| 62 | + 0, /* tp_iter */ |
| 63 | + 0, /* tp_iternext */ |
| 64 | + ue_PyFTimerHandle_methods, /* tp_methods */ |
| 65 | +}; |
| 66 | + |
| 67 | +void ue_python_init_ftimerhandle(PyObject *ue_module) { |
| 68 | + ue_PyFTimerHandleType.tp_new = PyType_GenericNew; |
| 69 | + if (PyType_Ready(&ue_PyFTimerHandleType) < 0) |
| 70 | + return; |
| 71 | + |
| 72 | + Py_INCREF(&ue_PyFTimerHandleType); |
| 73 | + PyModule_AddObject(ue_module, "FTimerHandle", (PyObject *)&ue_PyFTimerHandleType); |
| 74 | +} |
| 75 | + |
| 76 | +PyObject *py_ue_set_timer(ue_PyUObject *self, PyObject * args) { |
| 77 | + |
| 78 | + ue_py_check(self); |
| 79 | + |
| 80 | + float rate; |
| 81 | + PyObject *py_callable; |
| 82 | + PyObject *py_loop = nullptr; |
| 83 | + float first_delay = -1; |
| 84 | + if (!PyArg_ParseTuple(args, "fO|Of:simple_move_to_location", &rate, &py_callable, &py_loop, &first_delay)) { |
| 85 | + return NULL; |
| 86 | + } |
| 87 | + |
| 88 | + if (!PyCallable_Check(py_callable)) |
| 89 | + return PyErr_Format(PyExc_Exception, "object is not a callable"); |
| 90 | + |
| 91 | + bool loop = false; |
| 92 | + if (py_loop && PyObject_IsTrue(py_loop)) |
| 93 | + loop = true; |
| 94 | + |
| 95 | + UWorld *world = ue_get_uworld(self); |
| 96 | + if (!world) |
| 97 | + return PyErr_Format(PyExc_Exception, "unable to retrieve UWorld from uobject"); |
| 98 | + |
| 99 | + FTimerDelegate timer_delegate; |
| 100 | + UPythonDelegate *py_delegate = NewObject<UPythonDelegate>(); |
| 101 | + py_delegate->SetPyCallable(py_callable); |
| 102 | + // fake UFUNCTION for bypassing checks |
| 103 | + timer_delegate.BindUFunction(py_delegate, FName("PyFakeCallable")); |
| 104 | + //py_delegate->AddToRoot(); |
| 105 | + |
| 106 | + FTimerHandle thandle; |
| 107 | + world->GetTimerManager().SetTimer(thandle, timer_delegate, rate, loop, first_delay); |
| 108 | + |
| 109 | + ue_PyFTimerHandle *ret = (ue_PyFTimerHandle *)PyObject_New(ue_PyFTimerHandle, &ue_PyFTimerHandleType); |
| 110 | + if (!ret) { |
| 111 | + world->GetTimerManager().ClearTimer(thandle); |
| 112 | + return PyErr_Format(PyExc_Exception, "unable to allocate FTimerHandle python object"); |
| 113 | + } |
| 114 | + ret->thandle = thandle; |
| 115 | + ret->py_callable = py_callable; |
| 116 | + // will be decref'ed on clear |
| 117 | + Py_INCREF(ret->py_callable); |
| 118 | + ret->world = world; |
| 119 | + |
| 120 | + return (PyObject *)ret; |
| 121 | +} |
| 122 | + |
0 commit comments