Skip to content

Commit 592ef4c

Browse files
author
Roberto De Ioris
committed
added create_widget()
1 parent 29a7863 commit 592ef4c

3 files changed

Lines changed: 35 additions & 3 deletions

File tree

Source/UnrealEnginePython/Private/UEPyModule.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,7 @@ static PyMethodDef ue_PyUObject_methods[] = {
629629

630630
// Widget
631631
{ "take_widget", (PyCFunction)py_ue_take_widget, METH_VARARGS, "" },
632+
{ "create_widget", (PyCFunction)py_ue_create_widget, METH_VARARGS, "" },
632633

633634
// WidgetComponent
634635
{ "set_slate_widget", (PyCFunction)py_ue_set_slate_widget, METH_VARARGS, "" },

Source/UnrealEnginePython/Private/UObject/UEPyWidget.cpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,41 @@ PyObject *py_ue_take_widget(ue_PyUObject * self, PyObject * args) {
88

99
ue_py_check(self);
1010

11-
UWidget *widget = ue_py_check_type<UWidget>(self);
11+
UWidget *widget = ue_py_check_type<UWidget>(self);
1212
if (!widget)
1313
return PyErr_Format(PyExc_Exception, "uobject is not a UWidget");
1414

1515
ue_PySWidget *s_widget = ue_py_get_swidget(widget->TakeWidget());
1616

17-
return (PyObject *)s_widget;
17+
return (PyObject *)s_widget;
18+
}
19+
20+
PyObject *py_ue_create_widget(ue_PyUObject * self, PyObject * args) {
21+
22+
ue_py_check(self);
23+
24+
PyObject *py_class;
25+
if (!PyArg_ParseTuple(args, "O", &py_class))
26+
return nullptr;
27+
28+
APlayerController *player = ue_py_check_type<APlayerController>(self);
29+
if (!player)
30+
return PyErr_Format(PyExc_Exception, "uobject is not a APlayerController");
31+
32+
UClass *u_class = ue_py_check_type<UClass>(py_class);
33+
if (!u_class)
34+
return PyErr_Format(PyExc_Exception, "argument is not a UClass");
35+
36+
if (!u_class->IsChildOf<UUserWidget>())
37+
return PyErr_Format(PyExc_Exception, "argument is not a child of UUserWidget");
38+
39+
UUserWidget *widget = CreateWidget<UUserWidget>(player, u_class);
40+
if (!widget)
41+
return PyErr_Format(PyExc_Exception, "unable to create new widget");
42+
43+
ue_PyUObject *ret = ue_get_python_wrapper(widget);
44+
if (!ret)
45+
return PyErr_Format(PyExc_Exception, "PyUObject is in invalid state");
46+
Py_INCREF(ret);
47+
return (PyObject *)ret;
1848
}

Source/UnrealEnginePython/Private/UObject/UEPyWidget.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
#include "UnrealEnginePython.h"
55

66

7-
PyObject *py_ue_take_widget(ue_PyUObject *, PyObject *);
7+
PyObject *py_ue_take_widget(ue_PyUObject *, PyObject *);
8+
PyObject *py_ue_create_widget(ue_PyUObject *, PyObject *);

0 commit comments

Comments
 (0)