I’m not very good at C++, but my game needs python, so I’m trying to make a python code interpreter.
I want to process some text using python and neural networks, and upload the answer to UE4.
I found a C++ code(link text) on the internet that can execute a python file. I managed to embed it in the blueprintfunction Library class. Then, using the same function, I get the value from the text file in which the answer is written.
.cpp:
bool UMyBlueprintFunctionLibrary::PlayPythonFile(FString FileName, FString LoadDir, FString& result)
{
LoadDir += "\\";
LoadDir += FileName;
char fname[] = "PythonFile.py";
FILE* fp;
Py_Initialize();
fp = _Py_fopen(fname, "r");
PyRun_SimpleFile(fp, fname);
Py_Finalize();
return FFileHelper::LoadFileToString(result, *LoadDir);
}
.h:
UCLASS()
class GAME_API UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
UFUNCTION(BlueprintCallable, Category = "Custom")
static bool PlayPythonFile(FString FileName, FString LoadDir, FString& result);
};
The problem is that the part of the code that is responsible for python does nothing. At the same time, there are no errors.
I know that probably this solution is not the best, but so far I do not know how to do it better. Could you please help me to make the function work properly?