@@ -811,6 +811,95 @@ PyObject *py_unreal_engine_create_and_dispatch_when_ready(PyObject * self, PyObj
811811}
812812
813813
814+ PyObject * py_unreal_engine_run_on_gt (PyObject *self, PyObject * args)
815+ {
816+ PyObject *py_callable = nullptr ;
817+ PyObject *py_params = nullptr ;
818+
819+ Py_ssize_t TupleSize = PyTuple_Size (args);
820+
821+ if (TupleSize == 1 )
822+ {
823+ // function with no params
824+ if (!PyArg_ParseTuple (args, " O:create_and_dispatch_when_ready" , &py_callable))
825+ {
826+ UE_LOG (LogPython, Log, TEXT (" PyArg_ParseTuple without params failed" ));
827+ unreal_engine_py_log_error ();
828+ return NULL ;
829+ }
830+ }
831+ else
832+ {
833+ // function with params
834+ if (!PyArg_ParseTuple (args, " OO:create_and_dispatch_when_ready" , &py_callable, &py_params))
835+ {
836+ UE_LOG (LogPython, Log, TEXT (" PyArg_ParseTuple with params failed" ));
837+ unreal_engine_py_log_error ();
838+
839+ // Not an acceptable format, exit
840+ return NULL ;
841+ }
842+ }
843+
844+ // UE_LOG(LogPython, Log, TEXT("Start Task in Game thread? %d"), IsInGameThread());
845+
846+ if (!PyCallable_Check (py_callable))
847+ {
848+ return PyErr_Format (PyExc_TypeError, " argument is not callable" );
849+ }
850+
851+ Py_INCREF (py_callable);
852+ if (py_params)
853+ {
854+ Py_INCREF (py_params);
855+ }
856+
857+ const PyObject* py_callable_s = py_callable;
858+ const PyObject* py_params_s = py_params;
859+
860+
861+ FGraphEventRef task = FFunctionGraphTask::CreateAndDispatchWhenReady ([&, py_callable_s, py_params_s]() {
862+ // UE_LOG(LogPython, Log, TEXT("In task graph, are in game thread? %d"), IsInGameThread());
863+ FScopePythonGIL gil;
864+ PyObject *ret = nullptr ;
865+ PyObject *py_tuple_params = nullptr ;
866+
867+ // do we have parameters?
868+ if (py_params_s)
869+ {
870+ py_tuple_params = PyTuple_New (1 );
871+ PyTuple_SetItem (py_tuple_params, 0 , (PyObject*)py_params_s);
872+ ret = PyObject_CallObject ((PyObject*)py_callable_s, py_tuple_params);
873+ }
874+ else
875+ {
876+ ret = PyObject_CallObject ((PyObject*)py_callable_s, nullptr );
877+ }
878+
879+ // did we get a valid return from our call?
880+ if (ret)
881+ {
882+ Py_DECREF (ret);
883+ }
884+ else
885+ {
886+ unreal_engine_py_log_error ();
887+ }
888+ if (py_params_s)
889+ {
890+ Py_DECREF (py_params_s);
891+ }
892+ if (py_tuple_params)
893+ {
894+ Py_DECREF (py_tuple_params);
895+ }
896+ Py_DECREF (py_callable_s);
897+ }, TStatId (), nullptr , ENamedThreads::GameThread);
898+
899+ Py_INCREF (Py_None);
900+ Py_RETURN_NONE;
901+ }
902+
814903#if PLATFORM_MAC
815904PyObject *py_unreal_engine_main_thread_call (PyObject * self, PyObject * args)
816905{
0 commit comments