Skip to content

Commit 6a3f070

Browse files
author
Roberto De Ioris
committed
added support for python generators in component tick
1 parent d14aed2 commit 6a3f070

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

Source/UnrealEnginePython/Private/PythonComponent.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ UPythonComponent::UPythonComponent()
1313

1414
PythonTickForceDisabled = false;
1515
PythonDisableAutoBinding = false;
16+
PythonTickEnableGenerator = false;
1617

1718
bWantsInitializeComponent = true;
19+
20+
py_generator = nullptr;
1821
}
1922

2023
void UPythonComponent::InitializePythonComponent()
@@ -163,6 +166,23 @@ void UPythonComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActo
163166

164167
FScopePythonGIL gil;
165168

169+
if (PythonTickEnableGenerator && py_generator)
170+
{
171+
PyObject *ret = PyIter_Next(py_generator);
172+
if (!ret)
173+
{
174+
if (PyErr_Occurred())
175+
{
176+
unreal_engine_py_log_error();
177+
}
178+
Py_DECREF(py_generator);
179+
py_generator = nullptr;
180+
return;
181+
}
182+
Py_DECREF(ret);
183+
return;
184+
}
185+
166186
// no need to check for method availability, we did it in component initialization
167187

168188
PyObject *ret = PyObject_CallMethod(py_component_instance, (char *)"tick", (char *)"f", DeltaTime);
@@ -171,6 +191,15 @@ void UPythonComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActo
171191
unreal_engine_py_log_error();
172192
return;
173193
}
194+
195+
if (PythonTickEnableGenerator)
196+
{
197+
py_generator = PyObject_GetIter(ret);
198+
if (!py_generator)
199+
{
200+
UE_LOG(LogPython, Error, TEXT("tick is not a python generator"));
201+
}
202+
}
174203
Py_DECREF(ret);
175204

176205
}

Source/UnrealEnginePython/Public/PythonComponent.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class UNREALENGINEPYTHON_API UPythonComponent : public UActorComponent
3737
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Python")
3838
bool PythonDisableAutoBinding;
3939

40+
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Python")
41+
bool PythonTickEnableGenerator;
42+
4043
UFUNCTION(BlueprintCallable, Category = "Python")
4144
void InitializePythonComponent();
4245

@@ -89,5 +92,7 @@ class UNREALENGINEPYTHON_API UPythonComponent : public UActorComponent
8992
PyObject * py_component_instance;
9093
// mapped uobject, required for debug and advanced reflection
9194
ue_PyUObject *py_uobject;
95+
96+
PyObject *py_generator;
9297
};
9398

0 commit comments

Comments
 (0)