Skip to content

Commit 7370ba5

Browse files
author
Roberto De Ioris
committed
added UPythonComponent::CallPythonComponentMethodStringArray
1 parent 3646583 commit 7370ba5

2 files changed

Lines changed: 91 additions & 2 deletions

File tree

‎Source/UnrealEnginePython/Public/PythonComponent.cpp‎

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ void UPythonComponent::SetPythonAttrFloat(FString attr, float f)
179179
}
180180
}
181181

182+
183+
182184
void UPythonComponent::SetPythonAttrInt(FString attr, int n)
183185
{
184186
if (!py_component_instance)
@@ -265,7 +267,7 @@ bool UPythonComponent::CallPythonComponentMethodBool(FString method_name, FStrin
265267
float UPythonComponent::CallPythonComponentMethodFloat(FString method_name, FString args)
266268
{
267269
if (!py_component_instance)
268-
return false;
270+
return 0;
269271

270272
FScopePythonGIL gil;
271273

@@ -280,7 +282,7 @@ float UPythonComponent::CallPythonComponentMethodFloat(FString method_name, FStr
280282

281283
if (!ret) {
282284
unreal_engine_py_log_error();
283-
return false;
285+
return 0;
284286
}
285287

286288
float value = 0;
@@ -295,6 +297,39 @@ float UPythonComponent::CallPythonComponentMethodFloat(FString method_name, FStr
295297
return value;
296298
}
297299

300+
int UPythonComponent::CallPythonComponentMethodInt(FString method_name, FString args)
301+
{
302+
if (!py_component_instance)
303+
return 0;
304+
305+
FScopePythonGIL gil;
306+
307+
PyObject *ret = nullptr;
308+
if (args.IsEmpty()) {
309+
ret = PyObject_CallMethod(py_component_instance, TCHAR_TO_UTF8(*method_name), NULL);
310+
}
311+
else {
312+
ret = PyObject_CallMethod(py_component_instance, TCHAR_TO_UTF8(*method_name), (char *)"s", TCHAR_TO_UTF8(*args));
313+
}
314+
315+
316+
if (!ret) {
317+
unreal_engine_py_log_error();
318+
return 0;
319+
}
320+
321+
int value = 0;
322+
323+
if (PyNumber_Check(ret)) {
324+
PyObject *py_value = PyNumber_Long(ret);
325+
value = PyLong_AsLong(py_value);
326+
Py_DECREF(py_value);
327+
}
328+
329+
Py_DECREF(ret);
330+
return value;
331+
}
332+
298333
FString UPythonComponent::CallPythonComponentMethodString(FString method_name, FString args)
299334
{
300335
if (!py_component_instance)
@@ -330,6 +365,54 @@ FString UPythonComponent::CallPythonComponentMethodString(FString method_name, F
330365
return ret_fstring;
331366
}
332367

368+
void UPythonComponent::CallPythonComponentMethodStringArray(FString method_name, FString args, TArray<FString> &output_strings)
369+
{
370+
if (!py_component_instance)
371+
return;
372+
373+
output_strings.Empty();
374+
375+
FScopePythonGIL gil;
376+
377+
PyObject *ret = nullptr;
378+
if (args.IsEmpty()) {
379+
ret = PyObject_CallMethod(py_component_instance, TCHAR_TO_UTF8(*method_name), NULL);
380+
}
381+
else {
382+
ret = PyObject_CallMethod(py_component_instance, TCHAR_TO_UTF8(*method_name), (char *)"s", TCHAR_TO_UTF8(*args));
383+
}
384+
385+
if (!ret) {
386+
unreal_engine_py_log_error();
387+
return;
388+
}
389+
390+
if (!PyList_Check(ret)) {
391+
UE_LOG(LogPython, Error, TEXT("return value is not a list"));
392+
return;
393+
}
394+
395+
Py_ssize_t len = PyList_Size(ret);
396+
397+
for (Py_ssize_t i = 0; i < len; i++) {
398+
PyObject *py_str = PyObject_Str(PyList_GetItem(ret, i));
399+
if (!py_str) {
400+
Py_DECREF(ret);
401+
return;
402+
}
403+
404+
char *str_ret = PyUnicode_AsUTF8(py_str);
405+
406+
FString ret_fstring = FString(UTF8_TO_TCHAR(str_ret));
407+
408+
output_strings.Add(ret_fstring);
409+
410+
Py_DECREF(py_str);
411+
}
412+
413+
Py_DECREF(ret);
414+
}
415+
333416

334417
UPythonComponent::~UPythonComponent()
335418
{

‎Source/UnrealEnginePython/Public/PythonComponent.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,15 @@ class UPythonComponent : public UActorComponent
4343
UFUNCTION(BlueprintCallable, Category = "Python")
4444
FString CallPythonComponentMethodString(FString method_name, FString args);
4545

46+
UFUNCTION(BlueprintCallable, Category = "Python")
47+
void CallPythonComponentMethodStringArray(FString method_name, FString args, TArray<FString> &output_strings);
48+
4649
UFUNCTION(BlueprintCallable, Category = "Python")
4750
float CallPythonComponentMethodFloat(FString method_name, FString args);
4851

52+
UFUNCTION(BlueprintCallable, Category = "Python")
53+
int CallPythonComponentMethodInt(FString method_name, FString args);
54+
4955
UFUNCTION(BlueprintCallable, Category = "Python")
5056
void SetPythonAttrInt(FString attr, int Integer);
5157

0 commit comments

Comments
 (0)