Skip to content

Commit f1bace9

Browse files
yakhiraIakhin, Ruslan
authored andcommitted
PythonComponent: Added function to return MAP strings.
1 parent b3a8183 commit f1bace9

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

‎Source/UnrealEnginePython/Private/PythonComponent.cpp‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,61 @@ UObject *UPythonComponent::CallPythonComponentMethodObject(FString method_name,
406406
}
407407

408408

409+
TMap<FString, FString> UPythonComponent::CallPythonComponentMethodMap(FString method_name, FString args){
410+
TMap<FString, FString> output_map;
409411

412+
if (!py_component_instance)
413+
return output_map;
414+
415+
FScopePythonGIL gil;
416+
417+
PyObject *ret = nullptr;
418+
if (args.IsEmpty()) {
419+
ret = PyObject_CallMethod(py_component_instance, TCHAR_TO_UTF8(*method_name), NULL);
420+
}
421+
else {
422+
ret = PyObject_CallMethod(py_component_instance, TCHAR_TO_UTF8(*method_name), (char *)"s", TCHAR_TO_UTF8(*args));
423+
}
424+
425+
if (!ret) {
426+
unreal_engine_py_log_error();
427+
return output_map;
428+
}
429+
430+
if (!PyDict_Check(ret)) {
431+
UE_LOG(LogPython, Error, TEXT("return value is not a dict"));
432+
return output_map;
433+
}
434+
435+
PyObject *py_keys = PyDict_Keys(ret);
436+
Py_ssize_t len = PyList_Size(py_keys);
437+
438+
for (Py_ssize_t i = 0; i < len; i++) {
439+
PyObject *py_key = PyList_GetItem(py_keys, i);
440+
PyObject *py_str_key = PyObject_Str(py_key);
441+
PyObject *py_str_value = PyObject_Str(PyDict_GetItem(ret, py_key));
442+
443+
if (!py_str_key || !py_str_value) {
444+
Py_DECREF(ret);
445+
return output_map;
446+
}
447+
448+
char *str_key = PyUnicode_AsUTF8(py_str_key);
449+
char *str_value = PyUnicode_AsUTF8(py_str_value);
450+
451+
FString ret_fstring_key = FString(UTF8_TO_TCHAR(str_key));
452+
FString ret_fstring_value = FString(UTF8_TO_TCHAR(str_value));
453+
454+
output_map.Add(ret_fstring_key, ret_fstring_value);
455+
456+
Py_DECREF(py_str_key);
457+
Py_DECREF(py_str_value);
458+
}
459+
460+
Py_DECREF(ret);
461+
462+
return output_map;
463+
}
410464
void UPythonComponent::CallPythonComponentMethodStringArray(FString method_name, FString args, TArray<FString> &output_strings)
411465
{
412466
if (!py_component_instance)

‎Source/UnrealEnginePython/Public/PythonComponent.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ class UPythonComponent : public UActorComponent
4646
UFUNCTION(BlueprintCallable, Category = "Python")
4747
FString CallPythonComponentMethodString(FString method_name, FString args);
4848

49+
UFUNCTION(BlueprintCallable, Category = "Python")
50+
TMap<FString, FString> CallPythonComponentMethodMap(FString method_name, FString args);
51+
4952
UFUNCTION(BlueprintCallable, Category = "Python")
5053
void CallPythonComponentMethodStringArray(FString method_name, FString args, TArray<FString> &output_strings);
5154

0 commit comments

Comments
 (0)