Repository navigation
Expand file tree
/
Copy pathUEPyWorld.cpp
More file actions
324 lines (229 loc) · 7.16 KB
/
Copy pathUEPyWorld.cpp
File metadata and controls
324 lines (229 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include "UnrealEnginePythonPrivatePCH.h"
#include "Runtime/Engine/Classes/Kismet/KismetSystemLibrary.h"
PyObject *py_ue_world_exec(ue_PyUObject *self, PyObject * args)
{
ue_py_check(self);
char *command;
if (!PyArg_ParseTuple(args, "s:world_exec", &command))
{
return NULL;
}
UWorld *world = ue_get_uworld(self);
if (!world)
return PyErr_Format(PyExc_Exception, "unable to retrieve UWorld from uobject");
if (world->Exec(world, UTF8_TO_TCHAR(command)))
Py_RETURN_TRUE;
Py_RETURN_FALSE;
}
PyObject *py_ue_quit_game(ue_PyUObject *self, PyObject * args)
{
ue_py_check(self);
UWorld *world = ue_get_uworld(self);
if (!world)
return PyErr_Format(PyExc_Exception, "unable to retrieve UWorld from uobject");
// no need to support multiple controllers
APlayerController *controller = world->GetFirstPlayerController();
if (!controller)
return PyErr_Format(PyExc_Exception, "unable to retrieve the first controller");
UKismetSystemLibrary::QuitGame(world, controller, EQuitPreference::Quit);
Py_RETURN_NONE;
}
PyObject *py_ue_get_world_type(ue_PyUObject *self, PyObject * args)
{
ue_py_check(self);
UWorld *world = ue_get_uworld(self);
if (!world)
return PyErr_Format(PyExc_Exception, "unable to retrieve UWorld from uobject");
return PyLong_FromUnsignedLong(world->WorldType);
}
PyObject *py_ue_play(ue_PyUObject *self, PyObject * args)
{
ue_py_check(self);
UWorld *world = ue_get_uworld(self);
if (!world)
return PyErr_Format(PyExc_Exception, "unable to retrieve UWorld from uobject");
world->BeginPlay();
Py_RETURN_NONE;
}
// mainly used for testing
PyObject *py_ue_world_tick(ue_PyUObject *self, PyObject * args)
{
ue_py_check(self);
float delta_time;
PyObject *py_increase_fc = nullptr;
if (!PyArg_ParseTuple(args, "f|O:world_tick", &delta_time, &py_increase_fc))
{
return nullptr;
}
UWorld *world = ue_get_uworld(self);
if (!world)
return PyErr_Format(PyExc_Exception, "unable to retrieve UWorld from uobject");
world->Tick(LEVELTICK_All, delta_time);
if (py_increase_fc && PyObject_IsTrue(py_increase_fc))
GFrameCounter++;
Py_RETURN_NONE;
}
PyObject *py_ue_all_objects(ue_PyUObject * self, PyObject * args)
{
ue_py_check(self);
UWorld *world = ue_get_uworld(self);
if (!world)
return PyErr_Format(PyExc_Exception, "unable to retrieve UWorld from uobject");
PyObject *ret = PyList_New(0);
for (TObjectIterator<UObject> Itr; Itr; ++Itr)
{
UObject *u_obj = *Itr;
if (u_obj->GetWorld() != world)
continue;
ue_PyUObject *py_obj = ue_get_python_wrapper(u_obj);
if (!py_obj)
continue;
PyList_Append(ret, (PyObject *)py_obj);
}
return ret;
}
PyObject *py_ue_all_actors(ue_PyUObject * self, PyObject * args)
{
ue_py_check(self);
UWorld *world = ue_get_uworld(self);
if (!world)
return PyErr_Format(PyExc_Exception, "unable to retrieve UWorld from uobject");
PyObject *ret = PyList_New(0);
for (TActorIterator<AActor> Itr(world); Itr; ++Itr)
{
UObject *u_obj = *Itr;
ue_PyUObject *py_obj = ue_get_python_wrapper(u_obj);
if (!py_obj)
continue;
PyList_Append(ret, (PyObject *)py_obj);
}
return ret;
}
PyObject *py_ue_find_object(ue_PyUObject *self, PyObject * args)
{
ue_py_check(self);
char *name;
if (!PyArg_ParseTuple(args, "s:find_object", &name))
{
return NULL;
}
UWorld *world = ue_get_uworld(self);
if (!world)
return PyErr_Format(PyExc_Exception, "unable to retrieve UWorld from uobject");
UObject *u_object = FindObject<UObject>(world->GetOutermost(), UTF8_TO_TCHAR(name));
if (!u_object)
return PyErr_Format(PyExc_Exception, "unable to find object %s", name);
ue_PyUObject *ret = ue_get_python_wrapper(u_object);
if (!ret)
return PyErr_Format(PyExc_Exception, "uobject is in invalid state");
Py_INCREF(ret);
return (PyObject *)ret;
}
PyObject *py_ue_get_world(ue_PyUObject *self, PyObject * args)
{
ue_py_check(self);
UWorld *world = ue_get_uworld(self);
if (!world)
return PyErr_Format(PyExc_Exception, "unable to retrieve UWorld from uobject");
ue_PyUObject *ret = ue_get_python_wrapper(world);
if (!ret)
return PyErr_Format(PyExc_Exception, "PyUObject is in invalid state");
Py_INCREF(ret);
return (PyObject *)ret;
}
PyObject *py_ue_get_game_viewport(ue_PyUObject *self, PyObject * args)
{
ue_py_check(self);
UWorld *world = ue_get_uworld(self);
if (!world)
return PyErr_Format(PyExc_Exception, "unable to retrieve UWorld from uobject");
UGameViewportClient *viewport_client = world->GetGameViewport();
if (!viewport_client)
return PyErr_Format(PyExc_Exception, "world has no GameViewportClient");
Py_RETURN_UOBJECT(viewport_client);
}
PyObject *py_ue_has_world(ue_PyUObject *self, PyObject * args)
{
ue_py_check(self);
if (ue_get_uworld(self))
Py_RETURN_TRUE;
Py_RETURN_FALSE;
}
PyObject *py_ue_set_view_target(ue_PyUObject * self, PyObject * args)
{
ue_py_check(self);
UWorld *world = ue_get_uworld(self);
if (!world)
return PyErr_Format(PyExc_Exception, "unable to retrieve UWorld from uobject");
PyObject *py_obj;
int controller_id = 0;
if (!PyArg_ParseTuple(args, "O|i:set_view_target", &py_obj, &controller_id))
{
return NULL;
}
AActor *actor = ue_py_check_type<AActor>(py_obj);
if (!actor)
{
return PyErr_Format(PyExc_Exception, "argument is not an actor");
}
APlayerController *controller = UGameplayStatics::GetPlayerController(world, controller_id);
if (!controller)
return PyErr_Format(PyExc_Exception, "unable to retrieve controller %d", controller_id);
controller->SetViewTarget(actor);
Py_RETURN_NONE;
}
PyObject *py_ue_get_world_delta_seconds(ue_PyUObject * self, PyObject * args)
{
ue_py_check(self);
UWorld *world = ue_get_uworld(self);
if (!world)
return PyErr_Format(PyExc_Exception, "unable to retrieve UWorld from uobject");
return Py_BuildValue("f", UGameplayStatics::GetWorldDeltaSeconds(world));
}
PyObject *py_ue_get_levels(ue_PyUObject * self, PyObject * args)
{
ue_py_check(self);
UWorld *world = ue_get_uworld(self);
if (!world)
return PyErr_Format(PyExc_Exception, "unable to retrieve UWorld from uobject");
PyObject *ret = PyList_New(0);
for (ULevel *level : world->GetLevels())
{
ue_PyUObject *py_obj = ue_get_python_wrapper(level);
if (!py_obj)
continue;
PyList_Append(ret, (PyObject *)py_obj);
}
return ret;
}
PyObject *py_ue_get_current_level(ue_PyUObject *self, PyObject * args)
{
ue_py_check(self);
UWorld *world = ue_get_uworld(self);
if (!world)
return PyErr_Format(PyExc_Exception, "unable to retrieve UWorld from uobject");
ULevel *level = world->GetCurrentLevel();
if (!level)
Py_RETURN_NONE;
ue_PyUObject *ret = ue_get_python_wrapper(level);
if (!ret)
return PyErr_Format(PyExc_Exception, "PyUObject is in invalid state");
Py_INCREF(ret);
return (PyObject *)ret;
}
PyObject *py_ue_set_current_level(ue_PyUObject *self, PyObject * args)
{
ue_py_check(self);
PyObject *py_level;
if (!PyArg_ParseTuple(args, "O", &py_level))
return nullptr;
UWorld *world = ue_get_uworld(self);
if (!world)
return PyErr_Format(PyExc_Exception, "unable to retrieve UWorld from uobject");
ULevel *level = ue_py_check_type<ULevel>(py_level);
if (!level)
return PyErr_Format(PyExc_Exception, "argument is not a ULevel");
if (world->SetCurrentLevel(level))
Py_RETURN_TRUE;
Py_RETURN_FALSE;
}