MulticastDelegateProperty
表示 Unreal Engine 中的内联多播委托属性。
继承关系
说明
MulticastDelegateProperty 封装了 Unreal Engine 的 FMulticastDelegateProperty,后者表示可绑定多组对象与函数的多播委托。该委托使用内联存储来保存调用列表。
访问委托属性
访问 UObject 上的多播委托属性时,会返回一个属性包装器对象,该对象提供修改委托的方法:
local PlayerController = GetPlayerController()
-- Access delegate property (returns property wrapper)
local delegateProp = PlayerController.OnPossessedPawnChanged
-- The wrapper has Add, Remove, and Clear methods
delegateProp:Add(MyCustomObject, FName("OnPawnChanged"))
方法
Add(targetObject, functionName)
向多播委托的调用列表添加一个委托绑定。
- 参数:
targetObject(UObject):要绑定委托的对象functionName(FName | string):要在目标对象上调用的函数名称
-- Access the delegate property wrapper
local prop = PlayerController.OnPossessedPawnChanged
-- Add a delegate binding using FName
prop:Add(MyCustomObject, FName("OnPawnChanged"))
-- Or use a string directly
prop:Add(MyCustomObject, "OnPawnChanged")
Remove(targetObject, functionName)
从调用列表中移除指定的委托绑定。
- 参数:
targetObject(UObject):委托绑定到的对象functionName(FName | string):已绑定函数的名称
-- Remove a specific binding
prop:Remove(MyCustomObject, FName("OnPawnChanged"))
-- Or use a string
prop:Remove(MyCustomObject, "OnPawnChanged")
Clear()
从调用列表中移除所有委托绑定。
-- Clear all bindings
prop:Clear()
GetBindings()
返回当前所有委托绑定组成的数组。
- 返回值: 表数组,每个表含有
Object(UObject)和FunctionName(FName)字段;若不存在绑定则返回nil
-- Get all current bindings
local bindings = prop:GetBindings()
if bindings then
print("Delegate has", #bindings, "bindings")
for i, binding in ipairs(bindings) do
print(string.format("[%d] %s::%s",
i,
binding.Object:GetFullName(),
binding.FunctionName:ToString()))
end
end
Broadcast(...)
触发委托,并使用给定参数调用所有已绑定函数。
- 参数: 可变参数,取决于委托的签名函数
- 注意: 参数的类型和顺序必须与委托签名的预期一致
-- For a delegate with no parameters
prop:Broadcast()
-- For a delegate with parameters (e.g., OnDamage(float Amount, AActor* Causer))
prop:Broadcast(25.0, CauserActor)
注意事项
- 访问委托属性的
Object.PropertyName会返回属性包装器,而不是委托值 - 容器对象已隐式存储在包装器中,因此调用方法时无需传入容器对象
- 函数名称可使用
FName或字符串字面量 - 另请参阅使用稀疏存储的变体:MulticastSparseDelegateProperty
原文:docs/lua-api/classes/multicastdelegateproperty.md · 固定提交 560411f18512 · 来源与 MIT 许可