DelegateProperty
表示 Unreal Engine 中的单播委托属性。
继承关系
说明
DelegateProperty 封装了 Unreal Engine 的 FDelegateProperty,后者表示可绑定一组对象与函数的单播委托。委托属性通过其属性包装器(使用 GetPropertyByName)执行元数据操作;而委托值可通过 Object.PropertyName 直接访问,并返回简单的表。
读取委托值
可直接在 UObject 上访问委托值。返回的表包含 Object 和 FunctionName 字段:
local PlayerPawn = GetPlayerController().Pawn
-- Read single-cast delegate value
local delegate = PlayerPawn.OnSomeDelegate
if delegate then
print("Bound to:", delegate.Object:GetFullName())
print("Function:", tostring(delegate.FunctionName))
end
设置委托值
为单播委托赋值时,可以提供包含 Object 和 FunctionName 字段的表;也可以赋值 nil 来清除:
-- Bind a delegate
PlayerPawn.OnSomeDelegate = {
Object = SomeObject,
FunctionName = FName("MyCallbackFunction")
}
-- Clear a delegate
PlayerPawn.OnSomeDelegate = nil
注意事项
- 单播委托没有
Add、Remove或Clear方法(这些操作应使用多播委托) - 属性包装器本身没有超出从 Property 继承的方法
- 委托值是普通 Lua 表,而非 userdata 对象
原文:docs/lua-api/classes/delegateproperty.md · 固定提交 560411f18512 · 来源与 MIT 许可