Skip to content

Commit d7484a1

Browse files
author
Roberto De Ioris
committed
moved uplugin to Runtime
1 parent 9c71f03 commit d7484a1

File tree

6 files changed

+103
-4
lines changed

6 files changed

+103
-4
lines changed

Source/UnrealEnginePython/Private/PyActor.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ APyActor::APyActor()
77
{
88
PrimaryActorTick.bCanEverTick = true;
99

10+
this->OnActorBeginOverlap.AddDynamic(this, &APyActor::PyOnActorBeginOverlap);
11+
1012
// pre-generate PyUObject (for performance)
1113
ue_get_python_wrapper(this);
1214
}
@@ -38,12 +40,14 @@ void APyActor::BeginPlay()
3840
return;
3941
}
4042

43+
#if WITH_EDITOR
4144
// todo implement autoreload with a dictionary of module timestamps
4245
py_actor_module = PyImport_ReloadModule(py_actor_module);
4346
if (!py_actor_module) {
4447
unreal_engine_py_log_error();
4548
return;
4649
}
50+
#endif
4751

4852
if (PythonClass.IsEmpty())
4953
return;
@@ -97,6 +101,21 @@ void APyActor::Tick(float DeltaTime)
97101

98102
}
99103

104+
105+
void APyActor::PyOnActorBeginOverlap(AActor *overlapped, AActor *other)
106+
{
107+
if (!py_actor_instance)
108+
return;
109+
110+
PyObject *ret = PyObject_CallMethod(py_actor_instance, "on_actor_begin_overlap", "O", (PyObject *)ue_get_python_wrapper(other));
111+
if (!ret) {
112+
unreal_engine_py_log_error();
113+
return;
114+
}
115+
Py_DECREF(ret);
116+
}
117+
118+
100119
APyActor::~APyActor()
101120
{
102121
Py_XDECREF(py_actor_instance);

Source/UnrealEnginePython/Private/PyActor.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ class APyActor : public AActor
3232
UPROPERTY(EditAnywhere, Category = "Python")
3333
FString PythonClass;
3434

35+
3536
private:
3637
PyObject *py_actor_instance;
38+
39+
UFUNCTION()
40+
void PyOnActorBeginOverlap(AActor *overlapped, AActor *other);
3741
};
3842

Source/UnrealEnginePython/Private/PythonComponent.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55

6-
UPythonComponent::UPyActorComponent()
6+
UPythonComponent::UPythonComponent()
77
{
88
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
99
// off to improve performance if you don't need them.
@@ -41,12 +41,14 @@ void UPythonComponent::BeginPlay()
4141
return;
4242
}
4343

44+
#if WITH_EDITOR
4445
// todo implement autoreload with a dictionary of mule timestamps
4546
py_component_module = PyImport_ReloadModule(py_component_module);
4647
if (!py_component_module) {
4748
unreal_engine_py_log_error();
4849
return;
4950
}
51+
#endif
5052

5153
if (PythonClass.IsEmpty())
5254
return;
@@ -100,7 +102,7 @@ void UPythonComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActo
100102

101103
}
102104

103-
UPythonComponent::~UPyActorComponent()
105+
UPythonComponent::~UPythonComponent()
104106
{
105107
Py_XDECREF(py_component_instance);
106108
UE_LOG(LogPython, Error, TEXT("Python UActorComponent wrapper XDECREF'ed"));

Source/UnrealEnginePython/Private/UEPyModule.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,25 @@ static PyObject *py_unreal_engine_add_on_screen_debug_message(PyObject * self, P
6767
return Py_None;
6868
}
6969

70+
static PyObject *py_unreal_engine_find_class(PyObject * self, PyObject * args) {
71+
char *name;
72+
if (!PyArg_ParseTuple(args, "s:find_class", &name)) {
73+
return NULL;
74+
}
75+
76+
UClass *u_class = FindObject<UClass>(ANY_PACKAGE, UTF8_TO_TCHAR(name));
77+
78+
if (u_class) {
79+
ue_PyUObject *ret = ue_get_python_wrapper(u_class);
80+
if (!ret)
81+
return PyErr_Format(PyExc_Exception, "uobject is in invalid state");
82+
Py_INCREF(ret);
83+
return (PyObject *)ret;
84+
}
85+
86+
Py_INCREF(Py_None);
87+
return Py_None;
88+
}
7089

7190

7291
/*
@@ -81,6 +100,7 @@ static PyMethodDef unreal_engine_methods[] = {
81100
{ "log_warning", py_unreal_engine_log_warning, METH_VARARGS, "" },
82101
{ "log_error", py_unreal_engine_log_error, METH_VARARGS, "" },
83102
{ "add_on_screen_debug_message", py_unreal_engine_add_on_screen_debug_message, METH_VARARGS, "" },
103+
{ "find_class", py_unreal_engine_find_class, METH_VARARGS, "" },
84104
{ NULL, NULL },
85105
};
86106

@@ -215,6 +235,30 @@ static PyObject *py_ue_get_actor_location(ue_PyUObject *self, PyObject * args) {
215235

216236
}
217237

238+
static PyObject *py_ue_get_actor_velocity(ue_PyUObject *self, PyObject * args) {
239+
240+
ue_py_check(self);
241+
242+
FVector vec3;
243+
244+
if (self->ue_object->IsA<AActor>()) {
245+
vec3 = ((AActor *)self->ue_object)->GetVelocity();
246+
goto ret;
247+
}
248+
249+
if (self->ue_object->IsA<UActorComponent>()) {
250+
vec3 = ((UActorComponent *)self->ue_object)->GetOwner()->GetVelocity();
251+
goto ret;
252+
}
253+
254+
255+
return PyErr_Format(PyExc_Exception, "uobject is not an actor or a component");
256+
257+
ret:
258+
return Py_BuildValue("fff", vec3.X, vec3.Y, vec3.Z);
259+
260+
}
261+
218262
static PyObject *py_ue_get_input_axis(ue_PyUObject *self, PyObject * args) {
219263

220264
ue_py_check(self);
@@ -459,10 +503,33 @@ static PyObject *py_ue_all_actors(ue_PyUObject * self, PyObject * args) {
459503
return ret;
460504
}
461505

506+
static PyObject *py_ue_actor_components(ue_PyUObject * self, PyObject * args) {
507+
508+
ue_py_check(self);
509+
510+
if (!self->ue_object->IsA<AActor>()) {
511+
return PyErr_Format(PyExc_Exception, "uobject is not an AActor");
512+
}
513+
514+
AActor *actor = (AActor *)self->ue_object;
515+
516+
PyObject *ret = PyList_New(0);
517+
518+
for (UActorComponent *component : actor->GetComponents()) {
519+
ue_PyUObject *py_obj = ue_get_python_wrapper(component);
520+
if (!py_obj)
521+
continue;
522+
PyList_Append(ret, (PyObject *)py_obj);
523+
}
524+
525+
return ret;
526+
}
527+
462528
static PyObject *py_ue_is_a(ue_PyUObject *, PyObject *);
463529

464530
static PyMethodDef ue_PyUObject_methods[] = {
465531
{ "get_actor_location", (PyCFunction)py_ue_get_actor_location, METH_VARARGS, "" },
532+
{ "get_actor_velocity", (PyCFunction)py_ue_get_actor_velocity, METH_VARARGS, "" },
466533
{ "set_actor_location", (PyCFunction)py_ue_set_actor_location, METH_VARARGS, "" },
467534
{ "get_property", (PyCFunction)py_ue_get_property, METH_VARARGS, "" },
468535
{ "properties", (PyCFunction)py_ue_properties, METH_VARARGS, "" },
@@ -478,6 +545,7 @@ static PyMethodDef ue_PyUObject_methods[] = {
478545
{ "bind_input_axis", (PyCFunction)py_ue_bind_input_axis, METH_VARARGS, "" },
479546
{ "enable_input", (PyCFunction)py_ue_enable_input, METH_VARARGS, "" },
480547
{ "get_class", (PyCFunction)py_ue_get_class, METH_VARARGS, "" },
548+
{ "actor_components", (PyCFunction)py_ue_actor_components, METH_VARARGS, "" },
481549
{ NULL } /* Sentinel */
482550
};
483551

Source/UnrealEnginePython/Private/UEPyModule.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#pragma once
22

3+
4+
35
#include "UnrealEnginePython.h"
46

7+
#include "UEPyModule.generated.h"
8+
59

610

711
typedef struct {
@@ -10,8 +14,10 @@ typedef struct {
1014
UObject *ue_object;
1115
} ue_PyUObject;
1216

17+
UCLASS()
1318
class UPyObject : public UObject
1419
{
20+
GENERATED_BODY()
1521
public:
1622
ue_PyUObject *py_object;
1723
};

UnrealEnginePython.uplugin

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
"MarketplaceURL": "",
1212
"SupportURL": "",
1313
"EnabledByDefault": false,
14-
"CanContainContent": false,
14+
"CanContainContent": true,
1515
"IsBetaVersion": true,
1616
"Installed": false,
1717
"Modules": [
1818
{
1919
"Name": "UnrealEnginePython",
20-
"Type": "Developer",
20+
"Type": "Runtime",
2121
"LoadingPhase": "PreDefault"
2222
}
2323
]

0 commit comments

Comments
 (0)