Skip to content

Commit ef8e0c1

Browse files
author
Roberto De Ioris
committed
added advanced struct creator
1 parent f4d048d commit ef8e0c1

5 files changed

Lines changed: 54 additions & 2 deletions

File tree

‎Source/UnrealEnginePython/Private/UEPyModule.cpp‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ static PyMethodDef ue_PyUObject_methods[] = {
342342

343343
{ "post_edit_change", (PyCFunction)py_ue_post_edit_change, METH_VARARGS, "" },
344344
{ "pre_edit_change", (PyCFunction)py_ue_pre_edit_change, METH_VARARGS, "" },
345+
{ "modify", (PyCFunction)py_ue_modify, METH_VARARGS, "" },
345346

346347
#if WITH_EDITOR
347348
{ "save_config", (PyCFunction)py_ue_save_config, METH_VARARGS, "" },
@@ -775,6 +776,37 @@ static PyObject *ue_PyUObject_call(ue_PyUObject *self, PyObject *args, PyObject
775776
#if WITH_EDITOR
776777
u_script_struct->InitializeDefaultValue(data);
777778
#endif
779+
if (kw) {
780+
PyObject *struct_keys = PyObject_GetIter(kw);
781+
for (;;) {
782+
PyObject *key = PyIter_Next(struct_keys);
783+
if (!key) {
784+
if (PyErr_Occurred())
785+
return PyErr_Format(PyExc_Exception, "unable to build struct from dictionary");
786+
break;
787+
}
788+
if (!PyUnicodeOrString_Check(key))
789+
continue;
790+
char *struct_key = PyUnicode_AsUTF8(key);
791+
792+
PyObject *value = PyDict_GetItem(kw, key);
793+
if (!value) {
794+
if (PyErr_Occurred())
795+
return PyErr_Format(PyExc_Exception, "unable to build struct from dictionary");
796+
break;
797+
}
798+
799+
UProperty *u_property = ue_struct_get_field_from_name(u_script_struct, struct_key);
800+
if (u_property) {
801+
if (!ue_py_convert_pyobject(value, u_property, data)) {
802+
return PyErr_Format(PyExc_Exception, "invalid value for UProperty");
803+
}
804+
}
805+
else {
806+
return PyErr_Format(PyExc_Exception, "UProperty %s not found", struct_key);
807+
}
808+
}
809+
}
778810
return py_ue_new_uscriptstruct(u_script_struct, data);
779811
}
780812
return PyErr_Format(PyExc_Exception, "the specified uobject has no __call__ support");

‎Source/UnrealEnginePython/Private/UEPyObject.cpp‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,19 @@ PyObject *py_ue_post_edit_change(ue_PyUObject *self, PyObject * args) {
141141
return Py_None;
142142
}
143143

144+
PyObject *py_ue_modify(ue_PyUObject *self, PyObject * args) {
145+
ue_py_check(self);
146+
147+
if (!self->ue_object) {
148+
return PyErr_Format(PyExc_Exception, "uobject is not valid");
149+
}
150+
#if WITH_EDITOR
151+
self->ue_object->Modify();
152+
#endif
153+
Py_INCREF(Py_None);
154+
return Py_None;
155+
}
156+
144157
PyObject *py_ue_pre_edit_change(ue_PyUObject *self, PyObject * args) {
145158
ue_py_check(self);
146159

‎Source/UnrealEnginePython/Private/UEPyObject.h‎

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

5050
PyObject *py_ue_post_edit_change( ue_PyUObject *, PyObject * );
5151
PyObject *py_ue_pre_edit_change(ue_PyUObject *, PyObject *);
52+
PyObject *py_ue_modify(ue_PyUObject *, PyObject *);
5253

5354

5455
#if WITH_EDITOR

‎Source/UnrealEnginePython/Private/UEPyUScriptStruct.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ static UProperty *get_field_from_name(UScriptStruct *u_struct, char *name) {
9292
return nullptr;
9393
}
9494

95+
UProperty *ue_struct_get_field_from_name(UScriptStruct *u_struct, char *name) {
96+
return get_field_from_name(u_struct, name);
97+
}
98+
9599
static PyObject *ue_PyUScriptStruct_getattro(ue_PyUScriptStruct *self, PyObject *attr_name) {
96100
PyObject *ret = PyObject_GenericGetAttr((PyObject *)self, attr_name);
97101
if (!ret) {

‎Source/UnrealEnginePython/Private/UEPyUScriptStruct.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
typedef struct {
66
PyObject_HEAD
7-
/* Type-specific fields go here. */
8-
UScriptStruct *u_struct;
7+
/* Type-specific fields go here. */
8+
UScriptStruct *u_struct;
99
uint8 *data;
1010
} ue_PyUScriptStruct;
1111

1212
PyObject *py_ue_new_uscriptstruct(UScriptStruct *, uint8 *);
1313
ue_PyUScriptStruct *py_ue_is_uscriptstruct(PyObject *);
1414

15+
UProperty *ue_struct_get_field_from_name(UScriptStruct *, char *);
16+
1517
void ue_python_init_uscriptstruct(PyObject *);

0 commit comments

Comments
 (0)