UGCProjects/TwoOnTwo/Script/Global/System/UGCSendRPCSystem.lua
2025-01-04 23:00:19 +08:00

114 lines
5.7 KiB
Lua
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

--[[
需要委托绑定初始化:InitUGCSendRPCSystem
服务器客户端处均可初始化仅一方初始化则只能在该位置发起RPC 建议在服务器和客户端均执行InitUGCSendRPCSystem
InTargetObj内的目标RPC函数必须调用UGCSendRPCSystem.RPCFunc(...)如下
function UGCGameState:UGCSendRPCSystemFunc(...) UGCSendRPCSystem.RPCFunc(...) end
]]--
UGCSendRPCSystem = UGCSendRPCSystem or {}
UGCSendRPCSystem.DelegationObj = nil
UGCSendRPCSystem.DelegationFuncName = ""
---@param InTargetObj AActor*
---@param InUGCSendRPCSystemFuncName string
function UGCSendRPCSystem.InitUGCSendRPCSystem(InTargetObj, InUGCSendRPCSystemFuncName)
if UE.IsValid(InTargetObj) and type(InTargetObj[InUGCSendRPCSystemFuncName]) == "function" then
UGCSendRPCSystem.DelegationObj = InTargetObj
UGCSendRPCSystem.DelegationFuncName = InUGCSendRPCSystemFuncName
end
end
--- RPC自动发送 TargetPlayer为nil则广播到所有客户端为table则发送到包含的客户端为int32则发送到目标客户端
---@param RPCFuncName UGCSendRPCSystem的函数名称
---@param InTargetPlayer multi nil/table/int32
---@param ... any 事件输入的可变参数
function UGCSendRPCSystem.AutoTriggerRPC(RPCFuncName, InTargetPlayer, ...)
if not UGCSendRPCSystem.CheckUGCSendRPCSystem() then
UGCLogSystem.LogError("[UGCSendRPCSystem_AutoTriggerRPC] not initialized")
return false
end
if InTargetPlayer == nil then
if UGCGameSystem.IsServer() then
UnrealNetwork.CallUnrealRPC_Multicast(UGCSendRPCSystem.DelegationObj, UGCSendRPCSystem.DelegationFuncName, RPCFuncName, ...)
else
local TargetController = STExtraGameplayStatics.GetFirstPlayerController(UGCGameSystem.GameState)
if UE.IsValid(TargetController) then UnrealNetwork.CallUnrealRPC(TargetController, UGCSendRPCSystem.DelegationObj, UGCSendRPCSystem.DelegationFuncName, RPCFuncName, ...) end
end
elseif type(InTargetPlayer) == "number" then
local TargetController = nil
if UGCGameSystem.IsServer() then
TargetController = UGCGameSystem.GetPlayerControllerByPlayerKey(InTargetPlayer)
else
TargetController = STExtraGameplayStatics.GetFirstPlayerController(UGCGameSystem.GameState)
end
if UE.IsValid(TargetController) then UnrealNetwork.CallUnrealRPC(TargetController, UGCSendRPCSystem.DelegationObj, UGCSendRPCSystem.DelegationFuncName, RPCFuncName, ...) end
elseif type(InTargetPlayer) == "table" and UGCGameSystem.IsServer() then
for _, PlayerKey in pairs(InTargetPlayer) do
local TargetController = UGCGameSystem.GetPlayerControllerByPlayerKey(PlayerKey)
if UE.IsValid(TargetController) then UnrealNetwork.CallUnrealRPC(TargetController, UGCSendRPCSystem.DelegationObj, UGCSendRPCSystem.DelegationFuncName, RPCFuncName, ...) end
end
end
end
function UGCSendRPCSystem.RPCFunc(FuncName, ...) UGCSendRPCSystem[FuncName](...) end
function UGCSendRPCSystem.CheckUGCSendRPCSystem() return UE.IsValid(UGCSendRPCSystem.DelegationObj) end
------------------------------------------- Delegation Func -------------------------------------------
--- RPC委托调用事件
function UGCSendRPCSystem.RPCEvent(TargetPlayer, EventType, ...) UGCSendRPCSystem.AutoTriggerRPC("RPC_RPCEvent", TargetPlayer, EventType, ... ) end
function UGCSendRPCSystem.RPC_RPCEvent(EventType, ...) UGCEventSystem.SendEvent(EventType, ...) end
--- RPC委托触发Actor的函数
function UGCSendRPCSystem.ActorRPCNotify(TargetPlayer, TargetActor, FuncName, ...) UGCSendRPCSystem.AutoTriggerRPC("RPC_ActorRPCNotify", TargetPlayer, TargetActor, FuncName, ... ) end
function UGCSendRPCSystem.RPC_ActorRPCNotify(TargetActor, FuncName, ...)
if UE.IsValid(TargetActor) then
TargetActor[FuncName](TargetActor, ...)
else
UGCLogSystem.LogError("[UGCSendRPCSystem_RPC_ActorRPCNotify] Actor is nil. FuncName:%s", FuncName)
end
end
--- RPC委托触发模式编辑器的事件
function UGCSendRPCSystem.PlayActionEvent(TargetPlayer, EventName, ...) UGCSendRPCSystem.AutoTriggerRPC("RPC_PlayActionEvent", TargetPlayer, EventName, ... ) end
function UGCSendRPCSystem.RPC_PlayActionEvent(EventName, ...) UGCGameSystem.SendModeCustomEvent(EventName, ...) end
--- 客户端显示隐藏UI
function UGCSendRPCSystem.ClientShowUI(TargetPlayer, UIType, bShow, NeedClosePeerPanel, ...)
if UGCGameSystem.IsServer() then
if bShow == nil then bShow = true end
if NeedClosePeerPanel == nil then NeedClosePeerPanel = false end
UGCSendRPCSystem.AutoTriggerRPC("RPC_ClientShowUI", TargetPlayer, UIType, bShow, NeedClosePeerPanel, ... )
else
UGCLogSystem.LogError("[UGCSendRPCSystem_ClientShowUI] 仅服务器调用生效 UIType:%s", tostring(UIType))
end
end
function UGCSendRPCSystem.RPC_ClientShowUI(UIType, bShow, NeedClosePeerPanel, ...)
if bShow then WidgetManager:ShowPanel(UIType, NeedClosePeerPanel, ...) else WidgetManager:ClosePanel(UIType) end
end
function UGCSendRPCSystem.ExeStaticLogic(TargetPlayer, LogicType, FuncName, ...)
UGCSendRPCSystem.AutoTriggerRPC("RPC_ExeStaticLogic", TargetPlayer, LogicType, FuncName, ... )
end
function UGCSendRPCSystem.RPC_ExeStaticLogic(LogicType, FuncName, ...)
if table.hasValue(LogicConfig.ELogicType, LogicType) then
UGCLogSystem.Log("[UGCSendRPCSystem_RPC_ExeStaticLogic] FuncName:%s", FuncName)
local LogicStatic = require(LogicConfig.RequireList[LogicType])
UGCLogSystem.LogTree("[UGCSendRPCSystem_RPC_ExeStaticLogic] ", LogicStatic)
LogicStatic[FuncName](...)
else
UGCLogSystem.LogError("[UGCSendRPCSystem_RPC_ExeStaticLogic] LogicConfig.ELogicType[%s] is nil ", tostring(LogicType))
end
end