Skip to content

Commit 5c6c7e7

Browse files
author
Roberto De Ioris
committed
ported PyPawn to the new system, improved manual ticking
1 parent ff01f17 commit 5c6c7e7

6 files changed

Lines changed: 95 additions & 18 deletions

File tree

Source/UnrealEnginePython/Private/PyPawn.cpp

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,30 @@ APyPawn::APyPawn()
1111

1212
}
1313

14+
void APyPawn::PostInitializeComponents()
15+
{
16+
Super::PostInitializeComponents();
17+
18+
if (!py_pawn_instance)
19+
return;
20+
21+
FScopePythonGIL gil;
22+
23+
if (!PyObject_HasAttrString(py_pawn_instance, (char *)"post_initialize_components"))
24+
return;
25+
26+
PyObject *pic_ret = PyObject_CallMethod(py_pawn_instance, (char *)"post_initialize_components", NULL);
27+
if (!pic_ret) {
28+
unreal_engine_py_log_error();
29+
return;
30+
}
31+
Py_DECREF(pic_ret);
32+
}
1433

1534
// Called when the game starts
16-
void APyPawn::BeginPlay()
35+
void APyPawn::PreInitializeComponents()
1736
{
18-
Super::BeginPlay();
19-
20-
// ...
37+
Super::PreInitializeComponents();
2138

2239
if (PythonModule.IsEmpty())
2340
return;
@@ -77,6 +94,27 @@ void APyPawn::BeginPlay()
7794

7895
ue_bind_events_for_py_class_by_attribute(this, py_pawn_instance);
7996

97+
if (!PyObject_HasAttrString(py_pawn_instance, (char *)"pre_initialize_components"))
98+
return;
99+
100+
PyObject *pic_ret = PyObject_CallMethod(py_pawn_instance, (char *)"pre_initialize_components", NULL);
101+
if (!pic_ret) {
102+
unreal_engine_py_log_error();
103+
return;
104+
}
105+
Py_DECREF(pic_ret);
106+
}
107+
108+
// Called when the game starts
109+
void APyPawn::BeginPlay()
110+
{
111+
Super::BeginPlay();
112+
113+
if (!py_pawn_instance)
114+
return;
115+
116+
FScopePythonGIL gil;
117+
80118
if (!PyObject_HasAttrString(py_pawn_instance, (char *)"begin_play"))
81119
return;
82120

@@ -88,7 +126,6 @@ void APyPawn::BeginPlay()
88126
Py_DECREF(bp_ret);
89127
}
90128

91-
92129
// Called every frame
93130
void APyPawn::Tick(float DeltaTime)
94131
{
@@ -109,6 +146,29 @@ void APyPawn::Tick(float DeltaTime)
109146
}
110147

111148

149+
void APyPawn::EndPlay(const EEndPlayReason::Type EndPlayReason)
150+
{
151+
if (!py_pawn_instance)
152+
return;
153+
154+
FScopePythonGIL gil;
155+
156+
if (PyObject_HasAttrString(py_pawn_instance, (char *)"end_play")) {
157+
PyObject *ep_ret = PyObject_CallMethod(py_pawn_instance, (char *)"end_play", (char*)"i", (int)EndPlayReason);
158+
159+
if (!ep_ret) {
160+
unreal_engine_py_log_error();
161+
}
162+
163+
Py_XDECREF(ep_ret);
164+
}
165+
166+
Super::EndPlay(EndPlayReason);
167+
168+
// ...
169+
}
170+
171+
112172
void APyPawn::CallPythonPawnMethod(FString method_name)
113173
{
114174
if (!py_pawn_instance)

Source/UnrealEnginePython/Private/UEPyEditor.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -394,18 +394,16 @@ PyObject *py_unreal_engine_import_asset(PyObject * self, PyObject * args) {
394394
return Py_None;
395395
}
396396

397-
398397
PyObject *py_unreal_engine_editor_tick(PyObject * self, PyObject * args) {
399-
float delta_seconds;
400-
PyObject *py_bool;
401-
if (!PyArg_ParseTuple(args, "fO:editor_tick", &delta_seconds, &py_bool)) {
398+
float delta_seconds = FApp::GetDeltaTime();
399+
PyObject *py_bool = nullptr;
400+
if (!PyArg_ParseTuple(args, "|fO:editor_tick", &delta_seconds, &py_bool)) {
402401
return NULL;
403402
}
404403

405-
GEditor->Tick(delta_seconds, PyObject_IsTrue(py_bool) ? true : false);
404+
GEditor->Tick(delta_seconds, (py_bool && PyObject_IsTrue(py_bool)) ? true : false);
406405

407-
Py_INCREF(Py_None);
408-
return Py_None;
406+
Py_RETURN_NONE;
409407
}
410408

411409
PyObject *py_unreal_engine_message_dialog_open(PyObject * self, PyObject * args) {

Source/UnrealEnginePython/Private/UEPyEngine.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -387,17 +387,26 @@ PyObject *py_unreal_engine_string_to_guid(PyObject * self, PyObject * args) {
387387
return PyErr_Format(PyExc_Exception, "unable to build FGuid");
388388
}
389389

390+
PyObject *py_unreal_engine_slate_tick(PyObject * self, PyObject * args) {
391+
FSlateApplication::Get().PumpMessages();
392+
FSlateApplication::Get().Tick();
393+
Py_RETURN_NONE;
394+
}
395+
390396
PyObject *py_unreal_engine_engine_tick(PyObject * self, PyObject * args) {
391-
float delta_seconds;
392-
PyObject *py_bool;
393-
if (!PyArg_ParseTuple(args, "fO:engine_tick", &delta_seconds, &py_bool)) {
397+
float delta_seconds = FApp::GetDeltaTime();
398+
PyObject *py_bool = nullptr;
399+
if (!PyArg_ParseTuple(args, "|fO:engine_tick", &delta_seconds, &py_bool)) {
394400
return NULL;
395401
}
396402

397-
GEngine->Tick(delta_seconds, PyObject_IsTrue(py_bool) ? true : false);
403+
GEngine->Tick(delta_seconds, (py_bool && PyObject_IsTrue(py_bool)) ? true : false);
398404

399-
Py_INCREF(Py_None);
400-
return Py_None;
405+
Py_RETURN_NONE;
406+
}
407+
408+
PyObject *py_unreal_engine_get_delta_time(PyObject * self, PyObject * args) {
409+
return PyFloat_FromDouble(FApp::GetDeltaTime());
401410
}
402411

403412

Source/UnrealEnginePython/Private/UEPyEngine.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ PyObject *py_unreal_engine_load_package(PyObject *, PyObject *);
3232
PyObject *py_unreal_engine_string_to_guid(PyObject *, PyObject *);
3333

3434
PyObject *py_unreal_engine_engine_tick(PyObject *, PyObject *);
35+
PyObject *py_unreal_engine_slate_tick(PyObject *, PyObject *);
36+
PyObject *py_unreal_engine_get_delta_time(PyObject *, PyObject *);
3537

3638
PyObject *py_unreal_engine_all_classes(PyObject *, PyObject *);
3739

Source/UnrealEnginePython/Private/UEPyModule.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ static PyMethodDef unreal_engine_methods[] = {
279279
#endif
280280

281281
{ "engine_tick", py_unreal_engine_engine_tick, METH_VARARGS, "" },
282+
{ "slate_tick", py_unreal_engine_slate_tick, METH_VARARGS, "" },
283+
{ "get_delta_time", py_unreal_engine_get_delta_time, METH_VARARGS, "" },
282284

283285
{ "new_object", py_unreal_engine_new_object, METH_VARARGS, "" },
284286

Source/UnrealEnginePython/Public/PyPawn.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@ class APyPawn : public APawn
1717
APyPawn();
1818
~APyPawn();
1919

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

2327
// Called every frame
2428
virtual void Tick(float DeltaSeconds) override;
2529

30+
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
31+
2632
UPROPERTY(EditAnywhere , Category = "Python")
2733
FString PythonModule;
2834

0 commit comments

Comments
 (0)