当通过事件调用函数时将附加参数传递给函数,即:Connect()
passing an additional parameter to a function when function is called via event i.e :Connect()
我正在尝试使用 OOP 将我的无名函数转换为更好地工作,但由于我依赖于某个范围,所以遇到了一些困难。任何建议,新旧代码如下:
旧代码 - 有效
for _,portal in pairs(script.Parent:GetChildren())do
if portal:IsA("Script") then continue end
portal.Touched:Connect(function(part)
local HRP = part.Parent:FindFirstChild("HumanoidRootPart")
if not HRP then return end
local DestinationName = portal:FindFirstChildOfClass("Attachment").Name
local Destination = script.Parent:FindFirstChild(DestinationName):FindFirstChild(portal.Name)
HRP.CFrame = Destination.WorldCFrame
end)
end
新代码 - 错误 OBVS
local function portalTouched(part,portal)
local HRP = part.Parent:FindFirstChild("HumanoidRootPart")
if not HRP then return end
local DestinationName = portal:FindFirstChildOfClass("Attachment").Name
local Destination = script.Parent:FindFirstChild(DestinationName):FindFirstChild(portal.Name)
HRP.CFrame = Destination.WorldCFrame
end
for _,portal in pairs(script.Parent:GetChildren())do
if portal:IsA("Script") then continue end
portal.Touched:Connect(portalTouched(portal))
end
将函数连接到事件的问题是您不能传递任何附加参数,
我怎样才能解决这个范围问题?任何和所有建议表示赞赏!
您可以使用通过 portal
调用的高阶函数,然后 returns 可以使用封闭函数中的参数的函数:
local function portalTouched(portal)
return function(part)
-- We can use portal in this function
local HRP = part.Parent:FindFirstChild("HumanoidRootPart")
if not HRP then return end
local DestinationName = portal:FindFirstChildOfClass("Attachment").Name
local Destination = script.Parent:FindFirstChild(DestinationName):FindFirstChild(portal.Name)
HRP.CFrame = Destination.WorldCFrame
end
end
for _,portal in pairs(script.Parent:GetChildren())do
if portal:IsA("Script") then continue end
portal.Touched:Connect(portalTouched(portal))
end
我正在尝试使用 OOP 将我的无名函数转换为更好地工作,但由于我依赖于某个范围,所以遇到了一些困难。任何建议,新旧代码如下:
旧代码 - 有效
for _,portal in pairs(script.Parent:GetChildren())do
if portal:IsA("Script") then continue end
portal.Touched:Connect(function(part)
local HRP = part.Parent:FindFirstChild("HumanoidRootPart")
if not HRP then return end
local DestinationName = portal:FindFirstChildOfClass("Attachment").Name
local Destination = script.Parent:FindFirstChild(DestinationName):FindFirstChild(portal.Name)
HRP.CFrame = Destination.WorldCFrame
end)
end
新代码 - 错误 OBVS
local function portalTouched(part,portal)
local HRP = part.Parent:FindFirstChild("HumanoidRootPart")
if not HRP then return end
local DestinationName = portal:FindFirstChildOfClass("Attachment").Name
local Destination = script.Parent:FindFirstChild(DestinationName):FindFirstChild(portal.Name)
HRP.CFrame = Destination.WorldCFrame
end
for _,portal in pairs(script.Parent:GetChildren())do
if portal:IsA("Script") then continue end
portal.Touched:Connect(portalTouched(portal))
end
将函数连接到事件的问题是您不能传递任何附加参数, 我怎样才能解决这个范围问题?任何和所有建议表示赞赏!
您可以使用通过 portal
调用的高阶函数,然后 returns 可以使用封闭函数中的参数的函数:
local function portalTouched(portal)
return function(part)
-- We can use portal in this function
local HRP = part.Parent:FindFirstChild("HumanoidRootPart")
if not HRP then return end
local DestinationName = portal:FindFirstChildOfClass("Attachment").Name
local Destination = script.Parent:FindFirstChild(DestinationName):FindFirstChild(portal.Name)
HRP.CFrame = Destination.WorldCFrame
end
end
for _,portal in pairs(script.Parent:GetChildren())do
if portal:IsA("Script") then continue end
portal.Touched:Connect(portalTouched(portal))
end