Skip to content

Commit 8ca62b5

Browse files
author
Roberto De Ioris
committed
allow passing arguments to multicast delegates
1 parent 2216cad commit 8ca62b5

File tree

2 files changed

+63
-7
lines changed

2 files changed

+63
-7
lines changed

Source/UnrealEnginePython/Private/UEPyModule.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,7 @@ static PyObject *ue_PyUObject_call(ue_PyUObject *self, PyObject *args, PyObject
14271427
}
14281428
return py_ue_new_owned_uscriptstruct_zero_copy(u_script_struct, data);
14291429
}
1430+
14301431
return PyErr_Format(PyExc_Exception, "the specified uobject has no __call__ support");
14311432
}
14321433

@@ -2953,8 +2954,7 @@ PyObject *py_ue_ufunction_call(UFunction *u_function, UObject *u_obj, PyObject *
29532954
if (ret)
29542955
return ret;
29552956

2956-
Py_INCREF(Py_None);
2957-
return Py_None;
2957+
Py_RETURN_NONE;
29582958
}
29592959

29602960
PyObject *ue_bind_pyevent(ue_PyUObject *u_obj, FString event_name, PyObject *py_callable, bool fail_on_wrong_property)

Source/UnrealEnginePython/Private/UObject/UEPyObject.cpp

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -985,22 +985,78 @@ PyObject *py_ue_broadcast(ue_PyUObject *self, PyObject *args)
985985

986986
ue_py_check(self);
987987

988-
char *property_name;
989-
if (!PyArg_ParseTuple(args, "s:broadcast", &property_name))
988+
Py_ssize_t args_len = PyTuple_Size(args);
989+
if (args_len < 1)
990990
{
991-
return nullptr;
991+
return PyErr_Format(PyExc_Exception, "you need to specify the event to trigger");
992+
}
993+
994+
PyObject *py_property_name = PyTuple_GetItem(args, 0);
995+
if (!PyUnicodeOrString_Check(py_property_name))
996+
{
997+
return PyErr_Format(PyExc_Exception, "event name must be a unicode string");
992998
}
993999

1000+
const char *property_name = UEPyUnicode_AsUTF8(py_property_name);
1001+
9941002
UProperty *u_property = self->ue_object->GetClass()->FindPropertyByName(FName(UTF8_TO_TCHAR(property_name)));
9951003
if (!u_property)
9961004
return PyErr_Format(PyExc_Exception, "unable to find event property %s", property_name);
9971005

9981006
if (auto casted_prop = Cast<UMulticastDelegateProperty>(u_property))
9991007
{
1000-
Py_BEGIN_ALLOW_THREADS;
10011008
FMulticastScriptDelegate multiscript_delegate = casted_prop->GetPropertyValue_InContainer(self->ue_object);
10021009
uint8 *parms = (uint8 *)FMemory_Alloca(casted_prop->SignatureFunction->PropertiesSize);
10031010
FMemory::Memzero(parms, casted_prop->SignatureFunction->PropertiesSize);
1011+
1012+
uint32 argn = 1;
1013+
1014+
// initialize args
1015+
for (TFieldIterator<UProperty> IArgs(casted_prop->SignatureFunction); IArgs && IArgs->HasAnyPropertyFlags(CPF_Parm); ++IArgs)
1016+
{
1017+
UProperty *prop = *IArgs;
1018+
if (!prop->HasAnyPropertyFlags(CPF_ZeroConstructor))
1019+
{
1020+
prop->InitializeValue_InContainer(parms);
1021+
}
1022+
1023+
if ((IArgs->PropertyFlags & (CPF_Parm | CPF_ReturnParm)) == CPF_Parm)
1024+
{
1025+
if (!prop->IsInContainer(casted_prop->SignatureFunction->ParmsSize))
1026+
{
1027+
return PyErr_Format(PyExc_Exception, "Attempting to import func param property that's out of bounds. %s", TCHAR_TO_UTF8(*casted_prop->SignatureFunction->GetName()));
1028+
}
1029+
1030+
PyObject *py_arg = PyTuple_GetItem(args, argn);
1031+
if (!py_arg)
1032+
{
1033+
PyErr_Clear();
1034+
#if WITH_EDITOR
1035+
FString default_key = FString("CPP_Default_") + prop->GetName();
1036+
FString default_key_value = casted_prop->SignatureFunction->GetMetaData(FName(*default_key));
1037+
if (!default_key_value.IsEmpty())
1038+
{
1039+
#if ENGINE_MINOR_VERSION >= 17
1040+
prop->ImportText(*default_key_value, prop->ContainerPtrToValuePtr<uint8>(parms), PPF_None, NULL);
1041+
#else
1042+
prop->ImportText(*default_key_value, prop->ContainerPtrToValuePtr<uint8>(buffer), PPF_Localized, NULL);
1043+
#endif
1044+
}
1045+
#endif
1046+
}
1047+
else if (!ue_py_convert_pyobject(py_arg, prop, parms, 0))
1048+
{
1049+
return PyErr_Format(PyExc_TypeError, "unable to convert pyobject to property %s (%s)", TCHAR_TO_UTF8(*prop->GetName()), TCHAR_TO_UTF8(*prop->GetClass()->GetName()));
1050+
}
1051+
1052+
1053+
}
1054+
1055+
argn++;
1056+
1057+
}
1058+
1059+
Py_BEGIN_ALLOW_THREADS;
10041060
multiscript_delegate.ProcessMulticastDelegate<UObject>(parms);
10051061
Py_END_ALLOW_THREADS;
10061062
}
@@ -1907,7 +1963,7 @@ PyObject *py_ue_save_package(ue_PyUObject * self, PyObject * args)
19071963
package = CreatePackage(nullptr, UTF8_TO_TCHAR(name));
19081964
if (!package)
19091965
return PyErr_Format(PyExc_Exception, "unable to create package");
1910-
1966+
19111967
package->FileName = *FPackageName::LongPackageNameToFilename(UTF8_TO_TCHAR(name), bIsMap ? FPackageName::GetMapPackageExtension() : FPackageName::GetAssetPackageExtension());
19121968
if (has_package)
19131969
{

0 commit comments

Comments
 (0)