Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add gt callback with python variables #564

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 65 additions & 15 deletions Source/UnrealEnginePython/Private/UEPyEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -723,22 +723,70 @@ PyObject *py_unreal_engine_tobject_iterator(PyObject * self, PyObject * args)

PyObject *py_unreal_engine_create_and_dispatch_when_ready(PyObject * self, PyObject * args)
{
PyObject *py_callable;
int named_thread = (int)ENamedThreads::GameThread;
if (!PyArg_ParseTuple(args, "O|i:create_and_dispatch_when_ready", &py_callable, &named_thread))
PyObject *py_callable = nullptr;
PyObject *py_params = nullptr;

Py_ssize_t TupleSize = PyTuple_Size(args);

if (TupleSize == 1)
{
return NULL;
//function with no params
if (!PyArg_ParseTuple(args, "O:create_and_dispatch_when_ready", &py_callable))
{
UE_LOG(LogPython, Log, TEXT("PyArg_ParseTuple without params failed"));
unreal_engine_py_log_error();
return NULL;
}
}
else
{
//function with params
if (!PyArg_ParseTuple(args, "OO:create_and_dispatch_when_ready", &py_callable, &py_params))
{
UE_LOG(LogPython, Log, TEXT("PyArg_ParseTuple with params failed"));
unreal_engine_py_log_error();

//Not an acceptable format, exit
return NULL;
}
}

//UE_LOG(LogPython, Log, TEXT("Start Task in Game thread? %d"), IsInGameThread());

if (!PyCallable_Check(py_callable))
{
return PyErr_Format(PyExc_TypeError, "argument is not callable");
}

Py_INCREF(py_callable);
if (py_params)
{
Py_INCREF(py_params);
}

const PyObject* py_callable_s = py_callable;
const PyObject* py_params_s = py_params;

FGraphEventRef task = FFunctionGraphTask::CreateAndDispatchWhenReady([&]() {

FGraphEventRef task = FFunctionGraphTask::CreateAndDispatchWhenReady([&, py_callable_s, py_params_s]() {
//UE_LOG(LogPython, Log, TEXT("In task graph, are in game thread? %d"), IsInGameThread());
FScopePythonGIL gil;
PyObject *ret = PyObject_CallObject(py_callable, nullptr);
PyObject *ret = nullptr;
PyObject *py_tuple_params = nullptr;

//do we have parameters?
if (py_params_s)
{
py_tuple_params = PyTuple_New(1);
PyTuple_SetItem(py_tuple_params, 0, (PyObject*)py_params_s);
ret = PyObject_CallObject((PyObject*)py_callable_s, py_tuple_params);
}
else
{
ret = PyObject_CallObject((PyObject*)py_callable_s, nullptr);
}

//did we get a valid return from our call?
if (ret)
{
Py_DECREF(ret);
Expand All @@ -747,16 +795,18 @@ PyObject *py_unreal_engine_create_and_dispatch_when_ready(PyObject * self, PyObj
{
unreal_engine_py_log_error();
}
Py_DECREF(py_callable);
}, TStatId(), nullptr, (ENamedThreads::Type)named_thread);


Py_BEGIN_ALLOW_THREADS;
FTaskGraphInterface::Get().WaitUntilTaskCompletes(task);
Py_END_ALLOW_THREADS;
// TODO Implement signal triggering in addition to WaitUntilTaskCompletes
// FTaskGraphInterface::Get().TriggerEventWhenTaskCompletes
if (py_params_s)
{
Py_DECREF(py_params_s);
}
if (py_tuple_params)
{
Py_DECREF(py_tuple_params);
}
Py_DECREF(py_callable_s);
}, TStatId(), nullptr, ENamedThreads::GameThread);

Py_INCREF(Py_None);
Py_RETURN_NONE;
}

Expand Down