Skip to content

Commit 29a7863

Browse files
author
Roberto De Ioris
committed
override for InitializeComponent()
1 parent 4c50d47 commit 29a7863

2 files changed

Lines changed: 27 additions & 7 deletions

File tree

‎Source/UnrealEnginePython/Private/PythonComponent.cpp‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ UPythonComponent::UPythonComponent()
1515
PythonTickForceDisabled = false;
1616
PythonDisableAutoBinding = false;
1717

18-
18+
bWantsInitializeComponent = true;
1919
}
2020

2121
void UPythonComponent::InitializePythonComponent() {
@@ -77,26 +77,44 @@ void UPythonComponent::InitializePythonComponent() {
7777

7878
ue_bind_events_for_py_class_by_attribute(this, py_component_instance);
7979

80-
if (!PyObject_HasAttrString(py_component_instance, (char *)"begin_play")) {
80+
if (!PyObject_HasAttrString(py_component_instance, (char *)"initialize_component")) {
8181
return;
8282
}
8383

84-
PyObject *bp_ret = PyObject_CallMethod(py_component_instance, (char *)"begin_play", NULL);
85-
if (!bp_ret) {
84+
PyObject *ic_ret = PyObject_CallMethod(py_component_instance, (char *)"initialize_component", NULL);
85+
if (!ic_ret) {
8686
unreal_engine_py_log_error();
8787
}
88-
Py_XDECREF(bp_ret);
88+
Py_XDECREF(ic_ret);
8989

9090
}
9191

92+
void UPythonComponent::InitializeComponent()
93+
{
94+
Super::InitializeComponent();
95+
InitializePythonComponent();
96+
}
97+
98+
9299
// Called when the game starts
93100
void UPythonComponent::BeginPlay()
94101
{
95102
Super::BeginPlay();
96103

97-
// ...
104+
if (!py_component_instance)
105+
return;
98106

99-
InitializePythonComponent();
107+
FScopePythonGIL gil;
108+
109+
if (!PyObject_HasAttrString(py_component_instance, (char *)"begin_play")) {
110+
return;
111+
}
112+
113+
PyObject *bp_ret = PyObject_CallMethod(py_component_instance, (char *)"begin_play", NULL);
114+
if (!bp_ret) {
115+
unreal_engine_py_log_error();
116+
}
117+
Py_XDECREF(bp_ret);
100118

101119
}
102120

‎Source/UnrealEnginePython/Public/PythonComponent.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class UPythonComponent : public UActorComponent
2323
// Called every frame
2424
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
2525

26+
virtual void InitializeComponent() override;
27+
2628
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Python")
2729
FString PythonModule;
2830

0 commit comments

Comments
 (0)