Skip to content

Commit 6b378a6

Browse files
author
Roberto De Ioris
committed
ported PyCharacter to the new api
1 parent ba60668 commit 6b378a6

File tree

2 files changed

+85
-3
lines changed

2 files changed

+85
-3
lines changed

Source/UnrealEnginePython/Private/PyCharacter.cpp

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ APyCharacter::APyCharacter()
1414

1515

1616
// Called when the game starts
17-
void APyCharacter::BeginPlay()
17+
void APyCharacter::PreInitializeComponents()
1818
{
1919

2020

21-
Super::BeginPlay();
21+
Super::PreInitializeComponents();
2222

2323
// ...
2424

@@ -79,9 +79,50 @@ void APyCharacter::BeginPlay()
7979

8080
ue_bind_events_for_py_class_by_attribute(this, py_character_instance);
8181

82-
if (!PyObject_HasAttrString(py_character_instance, (char *)"begin_play")) {
82+
if (!PyObject_HasAttrString(py_character_instance, (char *)"pre_initialize_components")) {
83+
return;
84+
}
85+
86+
PyObject *pic_ret = PyObject_CallMethod(py_character_instance, (char *)"pre_initialize_components", NULL);
87+
if (!pic_ret) {
88+
unreal_engine_py_log_error();
89+
return;
90+
}
91+
Py_DECREF(pic_ret);
92+
}
93+
94+
void APyCharacter::PostInitializeComponents()
95+
{
96+
Super::PostInitializeComponents();
97+
98+
if (!py_character_instance)
99+
return;
100+
101+
FScopePythonGIL gil;
102+
103+
if (!PyObject_HasAttrString(py_character_instance, (char *)"post_initialize_components"))
104+
return;
105+
106+
PyObject *pic_ret = PyObject_CallMethod(py_character_instance, (char *)"post_initialize_components", NULL);
107+
if (!pic_ret) {
108+
unreal_engine_py_log_error();
83109
return;
84110
}
111+
Py_DECREF(pic_ret);
112+
}
113+
114+
// Called when the game starts
115+
void APyCharacter::BeginPlay()
116+
{
117+
Super::BeginPlay();
118+
119+
if (!py_character_instance)
120+
return;
121+
122+
FScopePythonGIL gil;
123+
124+
if (!PyObject_HasAttrString(py_character_instance, (char *)"begin_play"))
125+
return;
85126

86127
PyObject *bp_ret = PyObject_CallMethod(py_character_instance, (char *)"begin_play", NULL);
87128
if (!bp_ret) {
@@ -358,6 +399,41 @@ void APyCharacter::SetupPlayerInputComponent(class UInputComponent* input)
358399
{
359400
Super::SetupPlayerInputComponent(input);
360401

402+
if (!py_character_instance)
403+
return;
404+
405+
FScopePythonGIL gil;
406+
407+
// no need to check for method availability, we did it in begin_play
408+
409+
PyObject *ret = PyObject_CallMethod(py_character_instance, (char *)"setup_player_input_component", (char *)"O", ue_get_python_wrapper(input));
410+
if (!ret) {
411+
unreal_engine_py_log_error();
412+
return;
413+
}
414+
Py_DECREF(ret);
415+
}
416+
417+
void APyCharacter::EndPlay(const EEndPlayReason::Type EndPlayReason)
418+
{
419+
if (!py_character_instance)
420+
return;
421+
422+
FScopePythonGIL gil;
423+
424+
if (PyObject_HasAttrString(py_character_instance, (char *)"end_play")) {
425+
PyObject *ep_ret = PyObject_CallMethod(py_character_instance, (char *)"end_play", (char*)"i", (int)EndPlayReason);
426+
427+
if (!ep_ret) {
428+
unreal_engine_py_log_error();
429+
}
430+
431+
Py_XDECREF(ep_ret);
432+
}
433+
434+
Super::EndPlay(EndPlayReason);
435+
436+
// ...
361437
}
362438

363439

Source/UnrealEnginePython/Public/PyCharacter.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ class APyCharacter : public ACharacter
1616
APyCharacter();
1717
~APyCharacter();
1818

19+
// Called whenever the Actor is instantiated (before begin play)
20+
virtual void PreInitializeComponents() override;
21+
virtual void PostInitializeComponents() override;
22+
1923
// Called when the game starts
2024
virtual void BeginPlay() override;
2125

@@ -25,6 +29,8 @@ class APyCharacter : public ACharacter
2529
// Called to bind functionality to input
2630
virtual void SetupPlayerInputComponent(class UInputComponent* input) override;
2731

32+
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
33+
2834
UPROPERTY(EditAnywhere , Category = "Python")
2935
FString PythonModule;
3036

0 commit comments

Comments
 (0)