Skip to content

Commit 6cdf958

Browse files
committed
split out run_on_gt
-also add callbacks on background thread calls
1 parent eeab6d4 commit 6cdf958

File tree

4 files changed

+96
-4
lines changed

4 files changed

+96
-4
lines changed

Content/Scripts/upythread.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ def backgroundAction(args=None):
99
callback = args[1]
1010

1111
#call the blocking action
12-
action()
12+
result = action()
1313

1414
#return the result if we have a callback
1515
if callback:
16-
ue.run_on_gt(callback)
16+
if result is not None:
17+
ue.run_on_gt(callback, result)
18+
else:
19+
ue.run_on_gt(callback)
1720

1821
#run function on a background thread, optional callback when complete on game thread
1922
def run_on_bt(actionfunction, callback=None):

Source/UnrealEnginePython/Private/UEPyEngine.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
815904
PyObject *py_unreal_engine_main_thread_call(PyObject * self, PyObject * args)
816905
{

Source/UnrealEnginePython/Private/UEPyEngine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ PyObject *py_unreal_engine_get_mutable_default(PyObject *, PyObject *);
6565

6666

6767
PyObject *py_unreal_engine_create_and_dispatch_when_ready(PyObject *, PyObject *);
68+
PyObject *py_unreal_engine_run_on_gt(PyObject *, PyObject *);
6869

6970
PyObject *py_unreal_engine_convert_relative_path_to_full(PyObject *, PyObject *);
7071

Source/UnrealEnginePython/Private/UEPyModule.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ static PyMethodDef unreal_engine_methods[] = {
444444

445445

446446
{ "create_and_dispatch_when_ready", py_unreal_engine_create_and_dispatch_when_ready, METH_VARARGS, "" },
447+
{ "run_on_gt", py_unreal_engine_run_on_gt, METH_VARARGS, "" },
447448
#if PLATFORM_MAC
448449
{ "main_thread_call", py_unreal_engine_main_thread_call, METH_VARARGS, "" },
449450
#endif
@@ -497,8 +498,6 @@ static PyMethodDef unreal_engine_methods[] = {
497498

498499
{ "in_editor_capture", py_unreal_engine_in_editor_capture, METH_VARARGS, "" },
499500
#endif
500-
//duplicate for now for testing
501-
{ "run_on_gt", py_unreal_engine_create_and_dispatch_when_ready, METH_VARARGS, "" },
502501

503502
{ "clipboard_copy", py_unreal_engine_clipboard_copy, METH_VARARGS, "" },
504503
{ "clipboard_paste", py_unreal_engine_clipboard_paste, METH_VARARGS, "" },

0 commit comments

Comments
 (0)