Skip to content

Commit 2729167

Browse files
author
ikrima
committed
Adding ability to find object based on specific world (useful for PIE).
Syntax is piegameworld.find_object('MyLevelName:PersistentLevel.BP_ActorName')
1 parent 43bfda6 commit 2729167

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

Source/UnrealEnginePython/Private/UEPyEngine.h

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

3030
PyObject *py_unreal_engine_engine_tick(PyObject *, PyObject *);
3131

32-
PyObject *py_unreal_engine_find_object(PyObject *, PyObject *);
33-
3432
PyObject *py_unreal_engine_all_classes(PyObject *, PyObject *);
3533

3634

Source/UnrealEnginePython/Private/UEPyModule.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,7 @@ static PyMethodDef ue_PyUObject_methods[] = {
552552
{ "get_physics_linear_velocity", (PyCFunction)py_ue_get_physics_linear_velocity, METH_VARARGS, "" },
553553
{ "set_physics_angular_velocity", (PyCFunction)py_ue_set_physics_angular_velocity, METH_VARARGS, "" },
554554
{ "get_physics_angular_velocity", (PyCFunction)py_ue_get_physics_angular_velocity, METH_VARARGS, "" },
555+
{ "find_object", (PyCFunction)py_ue_find_object, METH_VARARGS, "" },
555556
{ "get_world", (PyCFunction)py_ue_get_world, METH_VARARGS, "" },
556557
{ "has_world", (PyCFunction)py_ue_has_world, METH_VARARGS, "" },
557558

Source/UnrealEnginePython/Private/UEPyWorld.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,31 @@ PyObject *py_ue_all_actors(ue_PyUObject * self, PyObject * args) {
121121
return ret;
122122
}
123123

124+
PyObject *py_ue_find_object(ue_PyUObject *self, PyObject * args) {
125+
126+
ue_py_check(self);
127+
128+
char *name;
129+
if (!PyArg_ParseTuple(args, "s:find_object", &name)) {
130+
return NULL;
131+
}
132+
133+
UWorld *world = ue_get_uworld(self);
134+
if (!world)
135+
return PyErr_Format(PyExc_Exception, "unable to retrieve UWorld from uobject");
136+
137+
UObject *u_object = FindObject<UObject>(world->GetOutermost(), UTF8_TO_TCHAR(name));
138+
139+
if (!u_object)
140+
return PyErr_Format(PyExc_Exception, "unable to find object %s", name);
141+
142+
ue_PyUObject *ret = ue_get_python_wrapper(u_object);
143+
if (!ret)
144+
return PyErr_Format(PyExc_Exception, "uobject is in invalid state");
145+
Py_INCREF(ret);
146+
return (PyObject *)ret;
147+
}
148+
124149
PyObject *py_ue_get_world(ue_PyUObject *self, PyObject * args) {
125150

126151
ue_py_check(self);

Source/UnrealEnginePython/Private/UEPyWorld.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ PyObject *py_ue_world_tick(ue_PyUObject *, PyObject *);
1414

1515
PyObject *py_ue_all_objects(ue_PyUObject *, PyObject *);
1616
PyObject *py_ue_all_actors(ue_PyUObject *, PyObject *);
17+
PyObject *py_ue_find_object(ue_PyUObject *, PyObject *);
1718
PyObject *py_ue_get_world(ue_PyUObject *, PyObject *);
1819
PyObject *py_ue_has_world(ue_PyUObject *, PyObject *);
1920

0 commit comments

Comments
 (0)