Skip to content

Commit 3995c39

Browse files
author
Roberto De Ioris
committed
added get_hit_result_under_cursor()
1 parent a61f5f9 commit 3995c39

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

‎Source/UnrealEnginePython/Private/UEPyModule.cpp‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,48 @@ static PyObject *py_ue_line_trace_multi_by_channel(ue_PyUObject * self, PyObject
12501250

12511251
}
12521252

1253+
static PyObject *py_ue_get_hit_result_under_cursor(ue_PyUObject * self, PyObject * args) {
1254+
1255+
ue_py_check(self);
1256+
1257+
int channel;
1258+
PyObject *trace_complex = NULL;
1259+
1260+
UWorld *world = ue_get_uworld(self);
1261+
if (!world)
1262+
return PyErr_Format(PyExc_Exception, "unable to retrieve UWorld from uobject");
1263+
1264+
1265+
if (!PyArg_ParseTuple(args, "i|O:get_hit_result_under_cursor", &channel, &trace_complex)) {
1266+
return NULL;
1267+
}
1268+
1269+
1270+
bool complex = false;
1271+
if (trace_complex && PyObject_IsTrue(trace_complex)) {
1272+
complex = true;
1273+
}
1274+
1275+
APlayerController *controller = world->GetFirstPlayerController();
1276+
1277+
if (!controller)
1278+
return PyErr_Format(PyExc_Exception, "unable to find first player controller");
1279+
1280+
FHitResult hit;
1281+
1282+
bool got_hit = controller->GetHitResultUnderCursor((ECollisionChannel)channel, complex, hit);
1283+
1284+
if (got_hit) {
1285+
PyObject *ret = (PyObject *)ue_get_python_wrapper(hit.GetActor());
1286+
if (!ret)
1287+
return PyErr_Format(PyExc_Exception, "uobject is in invalid state");
1288+
return Py_BuildValue("Offffff", ret, hit.ImpactPoint.X, hit.ImpactPoint.Y, hit.ImpactPoint.Z, hit.ImpactNormal.X, hit.ImpactNormal.Y, hit.ImpactNormal.Z);
1289+
}
1290+
1291+
return Py_BuildValue("Offffff", Py_None, 0, 0, 0, 0, 0, 0);
1292+
1293+
}
1294+
12531295

12541296

12551297
static PyObject *py_ue_is_a(ue_PyUObject *, PyObject *);
@@ -1299,6 +1341,7 @@ static PyMethodDef ue_PyUObject_methods[] = {
12991341
{ "get_actor_bounds", (PyCFunction)py_ue_get_actor_bounds, METH_VARARGS, "" },
13001342
{ "line_trace_single_by_channel", (PyCFunction)py_ue_line_trace_single_by_channel, METH_VARARGS, "" },
13011343
{ "line_trace_multi_by_channel", (PyCFunction)py_ue_line_trace_multi_by_channel, METH_VARARGS, "" },
1344+
{ "get_hit_result_under_cursor", (PyCFunction)py_ue_get_hit_result_under_cursor, METH_VARARGS, "" },
13021345
{ "show_mouse_cursor", (PyCFunction)py_ue_show_mouse_cursor, METH_VARARGS, "" },
13031346
{ "enable_click_events", (PyCFunction)py_ue_enable_click_events, METH_VARARGS, "" },
13041347
{ "enable_mouse_over_events", (PyCFunction)py_ue_enable_mouse_over_events, METH_VARARGS, "" },

‎Source/UnrealEnginePython/Public/PyPawn.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ void APyPawn::BeginPlay()
7575
UE_LOG(LogPython, Error, TEXT("Unable to set 'uobject' field in pawn wrapper class"));
7676
}
7777

78+
if (!PyObject_HasAttrString(py_pawn_instance, "begin_play"))
79+
return;
80+
7881
PyObject *bp_ret = PyObject_CallMethod(py_pawn_instance, "begin_play", NULL);
7982
if (!bp_ret) {
8083
unreal_engine_py_log_error();
@@ -92,6 +95,9 @@ void APyPawn::Tick(float DeltaTime)
9295
if (!py_pawn_instance)
9396
return;
9497

98+
if (!PyObject_HasAttrString(py_pawn_instance, "tick"))
99+
return;
100+
95101
PyObject *ret = PyObject_CallMethod(py_pawn_instance, "tick", "f", DeltaTime);
96102
if (!ret) {
97103
unreal_engine_py_log_error();
@@ -107,6 +113,9 @@ void APyPawn::PyOnActorBeginOverlap(AActor *overlapped, AActor *other)
107113
if (!py_pawn_instance)
108114
return;
109115

116+
if (!PyObject_HasAttrString(py_pawn_instance, "on_actor_begin_overlap"))
117+
return;
118+
110119
PyObject *ret = PyObject_CallMethod(py_pawn_instance, "on_actor_begin_overlap", "O", (PyObject *)ue_get_python_wrapper(other));
111120
if (!ret) {
112121
unreal_engine_py_log_error();
@@ -120,6 +129,9 @@ void APyPawn::PyOnActorClicked(AActor *touched_actor, FKey button_pressed)
120129
if (!py_pawn_instance)
121130
return;
122131

132+
if (!PyObject_HasAttrString(py_pawn_instance, "on_actor_clicked"))
133+
return;
134+
123135
PyObject *ret = PyObject_CallMethod(py_pawn_instance, "on_actor_clicked", "Os", (PyObject *)ue_get_python_wrapper(touched_actor), TCHAR_TO_UTF8(*button_pressed.ToString()));
124136
if (!ret) {
125137
unreal_engine_py_log_error();

0 commit comments

Comments
 (0)