RegisterHook

RegisterHook

RegisterHook 为 UFunction 注册回调

执行 UFunction 时会触发回调。

回调参数为:UObject self、UFunctionParams..。

返回两个 ID。注销钩子时,必须将这两个 ID 一起传给 UnregisterHook。

通过 RegisterHook 注册的任何 UFunction,都必须在注册时已经存在于内存中。

RegisterHook 不支持委托函数!

RegisterHook 参数

# 类型 说明
1 string 要挂钩的 UFunction 的完整名称。类型前缀不影响行为。
2 function 如果 UFunction 路径以 /Script/ 开头:在 UFunction 执行前调用的回调。;否则:在 UFunction 执行后调用的回调。
3 function (可选);如果 UFunction 路径以 /Script/ 开头:在 UFunction 执行后调用的回调。;否则:此参数无效。

RegisterHook 返回值

# 类型 说明
1 integer 钩子的 PreId
2 integer 钩子的 PostId

回调参数

# 类型 说明
1 RemoteUnrealParam 函数的 "this" 指针(Lua 中的 "self")的对象表示,也称 "Context"。其中包含调用此函数的对象,该对象被包装为 RemoteUnrealParam。
2..N RemoteUnrealParam... 全部函数参数,均包装为 RemoteUnrealParam

回调返回值

# 类型 说明
1 any 会尝试将此值自动转换为 UE 值,并用它覆盖原函数的返回值。;返回 nil(或不写 return 语句)会保留原返回值。

示例

PreId, PostId = RegisterHook("/Script/Engine.PlayerController:ClientRestart", function(Context, NewPawn)
    local PlayerController = Context:get()
    local Pawn = NewPawn:get()

    print(string.format("PlayerController FullName: %s\n", PlayerController:GetFullName()))
    if Pawn:IsValid() then
        print(string.format("NewPawn FullName: %s\n", Pawn:GetFullName()))
    end

    if PreId then
        -- Unhook once the function has been called
        UnregisterHook("/Script/Engine.PlayerController:ClientRestart", PreId, PostId)
    end
end)

原文:docs/lua-api/global-functions/registerhook.md · 固定提交 560411f18512 · 来源与 MIT 许可