Skip to content

Commit 9c9f56b

Browse files
author
Roberto De Ioris
committed
implemented FunctionToCall and Arguments for UPythonScript
1 parent 6a6cde3 commit 9c9f56b

5 files changed

Lines changed: 44 additions & 4 deletions

File tree

‎Source/UnrealEnginePython/Private/PythonScript.cpp‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,47 @@
22

33
#include "PythonScript.h"
44

5+
static void callback(void *arg) {
6+
UPythonScript *self = (UPythonScript *)arg;
7+
self->CallSpecificFunctionWithArgs();
8+
}
9+
510
void UPythonScript::Run() {
611
FUnrealEnginePythonModule &PythonModule = FModuleManager::GetModuleChecked<FUnrealEnginePythonModule>("UnrealEnginePython");
712
PythonModule.RunFile(TCHAR_TO_UTF8(*ScriptPath));
13+
if (!FunctionToCall.IsEmpty()) {
14+
CallSpecificFunctionWithArgs();
15+
}
816
}
917

1018
void UPythonScript::RunSandboxed() {
1119
FUnrealEnginePythonModule &PythonModule = FModuleManager::GetModuleChecked<FUnrealEnginePythonModule>("UnrealEnginePython");
12-
PythonModule.RunFileSandboxed(TCHAR_TO_UTF8(*ScriptPath));
20+
PythonModule.RunFileSandboxed(TCHAR_TO_UTF8(*ScriptPath), callback, this);
21+
}
22+
23+
void UPythonScript::CallSpecificFunctionWithArgs() {
24+
PyObject *function_to_call = PyDict_GetItemString(PyEval_GetGlobals(), TCHAR_TO_UTF8(*FunctionToCall));
25+
if (!function_to_call) {
26+
UE_LOG(LogPython, Error, TEXT("unable to find function %s"), *FunctionToCall);
27+
return;
28+
}
29+
30+
int n = FunctionArgs.Num();
31+
PyObject *args = nullptr;
32+
33+
if (n > 0)
34+
args = PyTuple_New(n);
35+
36+
for (int i = 0; i < n; i++) {
37+
PyTuple_SetItem(args, i, PyUnicode_FromString(TCHAR_TO_UTF8(*FunctionArgs[i])));
38+
}
39+
40+
PyObject *ret = PyObject_Call(function_to_call, args, nullptr);
41+
Py_DECREF(args);
42+
if (!ret) {
43+
unreal_engine_py_log_error();
44+
}
45+
else {
46+
Py_DECREF(ret);
47+
}
1348
}

‎Source/UnrealEnginePython/Private/UEPyModule.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ static PyObject *py_unreal_engine_sandbox_exec(PyObject * self, PyObject * args)
102102
return NULL;
103103
}
104104
FUnrealEnginePythonModule &PythonModule = FModuleManager::GetModuleChecked<FUnrealEnginePythonModule>("UnrealEnginePython");
105-
PythonModule.RunFileSandboxed(filename);
105+
PythonModule.RunFileSandboxed(filename, nullptr, nullptr);
106106
Py_INCREF(Py_None);
107107
return Py_None;
108108
}

‎Source/UnrealEnginePython/Private/UnrealEnginePython.cpp‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ void FUnrealEnginePythonModule::RunFile(char *filename) {
282282
}
283283

284284
// run a python script in a new sub interpreter (useful for unit tests)
285-
void FUnrealEnginePythonModule::RunFileSandboxed(char *filename) {
285+
void FUnrealEnginePythonModule::RunFileSandboxed(char *filename, void (*callback)(void *arg), void *arg) {
286286
FScopePythonGIL gil;
287287
char *full_path = filename;
288288
if (!FPaths::FileExists(filename))
@@ -348,6 +348,10 @@ void FUnrealEnginePythonModule::RunFileSandboxed(char *filename) {
348348
return;
349349
}
350350
#endif
351+
352+
if (callback)
353+
callback(arg);
354+
351355
Py_EndInterpreter(py_new_state);
352356
PyThreadState_Swap(_main);
353357
}

‎Source/UnrealEnginePython/Public/PythonScript.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ class UPythonScript : public UObject
2525
UFUNCTION()
2626
void RunSandboxed();
2727

28+
void CallSpecificFunctionWithArgs();
2829
};
2930

‎Source/UnrealEnginePython/Public/UnrealEnginePython.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class UNREALENGINEPYTHON_API FUnrealEnginePythonModule : public IModuleInterface
2222
void RunString(char *);
2323
void RunStringSandboxed(char *);
2424
void RunFile(char *);
25-
void RunFileSandboxed(char *);
25+
void RunFileSandboxed(char *, void(*callback)(void *arg), void *arg);
2626

2727
void UESetupPythonInterpreter(bool);
2828

0 commit comments

Comments
 (0)