Skip to content

Commit 91c3ebe

Browse files
author
Roberto De Ioris
committed
improved tick management, allows python ue classed to force disabling it
1 parent ed6e079 commit 91c3ebe

6 files changed

Lines changed: 39 additions & 43 deletions

File tree

‎Source/UnrealEnginePython/Public/PyActor.cpp‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ APyActor::APyActor()
1212
this->OnActorHit.AddDynamic(this, &APyActor::PyOnActorHit);
1313
this->OnClicked.AddDynamic(this, &APyActor::PyOnActorClicked);
1414

15+
PythonTickForceDisabled = false;
16+
1517
// pre-generate PyUObject (for performance)
1618
ue_get_python_wrapper(this);
1719
}
@@ -24,16 +26,6 @@ void APyActor::BeginPlay()
2426

2527
// ...
2628

27-
const TCHAR *blob = *PythonCode;
28-
29-
int ret = PyRun_SimpleString(TCHAR_TO_UTF8(blob));
30-
31-
UE_LOG(LogPython, Warning, TEXT("Python ret = %d"), ret);
32-
33-
if (ret) {
34-
unreal_engine_py_log_error();
35-
}
36-
3729
if (PythonModule.IsEmpty())
3830
return;
3931

@@ -78,6 +70,13 @@ void APyActor::BeginPlay()
7870
UE_LOG(LogPython, Error, TEXT("Unable to set 'uobject' field in actor wrapper class"));
7971
}
8072

73+
if (!PyObject_HasAttrString(py_actor_instance, "tick") || PythonTickForceDisabled) {
74+
SetActorTickEnabled(false);
75+
}
76+
77+
if (!PyObject_HasAttrString(py_actor_instance, "begin_play"))
78+
return;
79+
8180
PyObject *bp_ret = PyObject_CallMethod(py_actor_instance, "begin_play", NULL);
8281
if (!bp_ret) {
8382
unreal_engine_py_log_error();

‎Source/UnrealEnginePython/Public/PyActor.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ class APyActor : public AActor
2323
// Called every frame
2424
virtual void Tick(float DeltaSeconds) override;
2525

26-
UPROPERTY(EditAnywhere, meta = (Multiline = true), Category = "Python")
27-
FString PythonCode;
28-
2926
UPROPERTY(EditAnywhere , Category = "Python")
3027
FString PythonModule;
3128

3229
UPROPERTY(EditAnywhere, Category = "Python")
3330
FString PythonClass;
3431

32+
UPROPERTY(EditAnywhere, Category = "Python")
33+
bool PythonTickForceDisabled;
34+
3535
UFUNCTION(BlueprintCallable, Category = "Python")
3636
void CallPythonActorMethod(FString method_name, FString args);
3737

‎Source/UnrealEnginePython/Public/PyPawn.cpp‎

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ APyPawn::APyPawn()
99
this->OnActorBeginOverlap.AddDynamic(this, &APyPawn::PyOnActorBeginOverlap);
1010
this->OnClicked.AddDynamic(this, &APyPawn::PyOnActorClicked);
1111

12+
PythonTickForceDisabled = false;
13+
1214
// pre-generate PyUObject (for performance)
1315
ue_get_python_wrapper(this);
1416
}
@@ -21,16 +23,6 @@ void APyPawn::BeginPlay()
2123

2224
// ...
2325

24-
const TCHAR *blob = *PythonCode;
25-
26-
int ret = PyRun_SimpleString(TCHAR_TO_UTF8(blob));
27-
28-
UE_LOG(LogPython, Warning, TEXT("Python ret = %d"), ret);
29-
30-
if (ret) {
31-
unreal_engine_py_log_error();
32-
}
33-
3426
if (PythonModule.IsEmpty())
3527
return;
3628

@@ -75,6 +67,11 @@ void APyPawn::BeginPlay()
7567
UE_LOG(LogPython, Error, TEXT("Unable to set 'uobject' field in pawn wrapper class"));
7668
}
7769

70+
// disable ticking if not required
71+
if (!PyObject_HasAttrString(py_pawn_instance, "tick") || PythonTickForceDisabled) {
72+
SetActorTickEnabled(false);
73+
}
74+
7875
if (!PyObject_HasAttrString(py_pawn_instance, "begin_play"))
7976
return;
8077

@@ -95,9 +92,6 @@ void APyPawn::Tick(float DeltaTime)
9592
if (!py_pawn_instance)
9693
return;
9794

98-
if (!PyObject_HasAttrString(py_pawn_instance, "tick"))
99-
return;
100-
10195
PyObject *ret = PyObject_CallMethod(py_pawn_instance, "tick", "f", DeltaTime);
10296
if (!ret) {
10397
unreal_engine_py_log_error();

‎Source/UnrealEnginePython/Public/PyPawn.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ class APyPawn : public APawn
2323
// Called every frame
2424
virtual void Tick(float DeltaSeconds) override;
2525

26-
UPROPERTY(EditAnywhere, meta = (Multiline = true), Category = "Python")
27-
FString PythonCode;
28-
2926
UPROPERTY(EditAnywhere , Category = "Python")
3027
FString PythonModule;
3128

3229
UPROPERTY(EditAnywhere, Category = "Python")
3330
FString PythonClass;
3431

32+
UPROPERTY(EditAnywhere, Category = "Python")
33+
bool PythonTickForceDisabled;
34+
3535
UFUNCTION(BlueprintCallable, Category = "Python")
3636
void CallPythonPawnMethod(FString method_name);
3737

‎Source/UnrealEnginePython/Public/PythonComponent.cpp‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ UPythonComponent::UPythonComponent()
1010
bWantsBeginPlay = true;
1111
PrimaryComponentTick.bCanEverTick = true;
1212

13+
PythonTickForceDisabled = false;
14+
1315
// pre-generate PyUObject (for performance)
1416
ue_get_python_wrapper(this);
1517
}
@@ -22,16 +24,6 @@ void UPythonComponent::BeginPlay()
2224

2325
// ...
2426

25-
const TCHAR *blob = *PythonCode;
26-
27-
int ret = PyRun_SimpleString(TCHAR_TO_UTF8(blob));
28-
29-
UE_LOG(LogPython, Warning, TEXT("Python ret = %d"), ret);
30-
31-
if (ret) {
32-
unreal_engine_py_log_error();
33-
}
34-
3527
if (PythonModule.IsEmpty())
3628
return;
3729

@@ -42,7 +34,7 @@ void UPythonComponent::BeginPlay()
4234
}
4335

4436
#if WITH_EDITOR
45-
// todo implement autoreload with a dictionary of mule timestamps
37+
// todo implement autoreload with a dictionary of module timestamps
4638
py_component_module = PyImport_ReloadModule(py_component_module);
4739
if (!py_component_module) {
4840
unreal_engine_py_log_error();
@@ -76,6 +68,15 @@ void UPythonComponent::BeginPlay()
7668
UE_LOG(LogPython, Error, TEXT("Unable to set 'uobject' field in component wrapper class"));
7769
}
7870

71+
// disable ticking if no tick method is exposed
72+
if (!PyObject_HasAttrString(py_component_instance, "tick") || PythonTickForceDisabled) {
73+
SetComponentTickEnabled(false);
74+
}
75+
76+
if (!PyObject_HasAttrString(py_component_instance, "begin_play")) {
77+
return;
78+
}
79+
7980
PyObject *bp_ret = PyObject_CallMethod(py_component_instance, "begin_play", NULL);
8081
if (!bp_ret) {
8182
unreal_engine_py_log_error();
@@ -93,6 +94,8 @@ void UPythonComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActo
9394
if (!py_component_instance)
9495
return;
9596

97+
// no need to check for method availability, we did it in begin_play
98+
9699
PyObject *ret = PyObject_CallMethod(py_component_instance, "tick", "f", DeltaTime);
97100
if (!ret) {
98101
unreal_engine_py_log_error();

‎Source/UnrealEnginePython/Public/PythonComponent.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ class UPythonComponent : public UActorComponent
2222
// Called every frame
2323
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
2424

25-
UPROPERTY(EditAnywhere, meta = (Multiline = true), Category = "Python")
26-
FString PythonCode;
27-
2825
UPROPERTY(EditAnywhere , Category = "Python")
2926
FString PythonModule;
3027

3128
UPROPERTY(EditAnywhere, Category = "Python")
3229
FString PythonClass;
3330

31+
UPROPERTY(EditAnywhere, Category = "Python")
32+
bool PythonTickForceDisabled;
33+
3434
UFUNCTION(BlueprintCallable, Category = "Python")
3535
void CallPythonComponentMethod(FString method_name, FString args);
3636

0 commit comments

Comments
 (0)