如何连续执行 for 循环而不是卡在单个重复函数上?
How to execute a for loop continously instead of getting stuck on a single repeating function?
我正在尝试 运行 一个模型的所有子项的函数,但该函数本质上无限循环,阻止调用 for
循环中的下一个项目,我需要更好的方法或解决方法。
主要代码:
local wave = workspace["Wave model"]
local move = require(script.Parent.moveModule)
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function()
for _, child in ipairs(wave:GetChildren()) do
move.moveUp(child)
wait(math.random(0.1,0.3))
print("exec")
end
print("executed")
end)
我调用的模块:
local functions = {}
function functions.callNext(part, which)
if which == "up" then
functions.moveUp(part)
else
functions.moveDown(part)
end
end
function functions.moveUp(part)
local Tween = game:GetService("TweenService")
local Objective = {}
Objective.Position = Vector3.new(part.Position.X, part.Position.Y + 10, part.Position.Z)
local tweenInfo = TweenInfo.new(5)
local tweenie = Tween:Create(part, tweenInfo, Objective)
print("played")
tweenie:Play()
print("done")
tweenie.Completed:Connect(function()
print("Fcuk")
end)
wait(5)
functions.callNext(part, "down")
end
function functions.moveDown(part)
local Tween = game:GetService("TweenService")
local Objective = {}
Objective.Position = Vector3.new(part.Position.X, part.Position.Y - 10, part.Position.Z)
local tweenInfo = TweenInfo.new(5)
local tweenie = Tween:Create(part, tweenInfo, Objective)
print("played")
tweenie:Play()
print("done")
wait(5)
functions.callNext(part, "up")
end
return functions
有什么想法吗?
您可以将主脚本编辑为 运行 单独线程上的 moveup() 函数,如下所示:
local wave = workspace["Wave model"]
local move = require(script.Parent.moveModule)
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function()
for _, child in ipairs(wave:GetChildren()) do
spawn(function() move.moveUp(child) end) --Using an anonymous function as the spawn() function doesn't accept functions with parameters
wait(math.random(0.1,0.3))
print("exec")
end
print("executed")
end)
这是有效的,但是在长 运行 中你不应该这样做,因为无限循环 运行如果不优化的话在多个线程上会导致显着的延迟
代码已更改:
move.moveUp(child)
到
spawn(function() move.moveUp(child) end)
你没有无限循环,而是递归循环:模块中的函数相互调用,直到堆栈溢出。在不同的线程中生成它们会使它成倍增加。
我会通过补间的回调函数触发你的方向改变,那样使用异步性:
local functions = {}
function functions.moveUp(part)
local Tween = game:GetService("TweenService")
local Objective = {}
Objective.Position = Vector3.new(part.Position.X, part.Position.Y + 10, part.Position.Z)
local tweenInfo = TweenInfo.new(5)
local tweenie = Tween:Create(part, tweenInfo, Objective)
tweenie:Play()
tweenie.Completed:Connect(function()
functions.moveDown(part)
end)
end
function functions.moveDown(part)
local Tween = game:GetService("TweenService")
local Objective = {}
Objective.Position = Vector3.new(part.Position.X, part.Position.Y - 10, part.Position.Z)
local tweenInfo = TweenInfo.new(5)
local tweenie = Tween:Create(part, tweenInfo, Objective)
tweenie:Play()
tweenie.Completed:Connect(function()
functions.moveUp(part)
end)
end
return functions
顺便说一句 -
您应该阅读有关补间服务的文档,它非常简洁。您可以使用它在一个 运行 中上下移动。这样您就不必切换这两个功能。您也可以使用正弦波平滑运动。
我正在尝试 运行 一个模型的所有子项的函数,但该函数本质上无限循环,阻止调用 for
循环中的下一个项目,我需要更好的方法或解决方法。
主要代码:
local wave = workspace["Wave model"]
local move = require(script.Parent.moveModule)
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function()
for _, child in ipairs(wave:GetChildren()) do
move.moveUp(child)
wait(math.random(0.1,0.3))
print("exec")
end
print("executed")
end)
我调用的模块:
local functions = {}
function functions.callNext(part, which)
if which == "up" then
functions.moveUp(part)
else
functions.moveDown(part)
end
end
function functions.moveUp(part)
local Tween = game:GetService("TweenService")
local Objective = {}
Objective.Position = Vector3.new(part.Position.X, part.Position.Y + 10, part.Position.Z)
local tweenInfo = TweenInfo.new(5)
local tweenie = Tween:Create(part, tweenInfo, Objective)
print("played")
tweenie:Play()
print("done")
tweenie.Completed:Connect(function()
print("Fcuk")
end)
wait(5)
functions.callNext(part, "down")
end
function functions.moveDown(part)
local Tween = game:GetService("TweenService")
local Objective = {}
Objective.Position = Vector3.new(part.Position.X, part.Position.Y - 10, part.Position.Z)
local tweenInfo = TweenInfo.new(5)
local tweenie = Tween:Create(part, tweenInfo, Objective)
print("played")
tweenie:Play()
print("done")
wait(5)
functions.callNext(part, "up")
end
return functions
有什么想法吗?
您可以将主脚本编辑为 运行 单独线程上的 moveup() 函数,如下所示:
local wave = workspace["Wave model"]
local move = require(script.Parent.moveModule)
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function()
for _, child in ipairs(wave:GetChildren()) do
spawn(function() move.moveUp(child) end) --Using an anonymous function as the spawn() function doesn't accept functions with parameters
wait(math.random(0.1,0.3))
print("exec")
end
print("executed")
end)
这是有效的,但是在长 运行 中你不应该这样做,因为无限循环 运行如果不优化的话在多个线程上会导致显着的延迟
代码已更改:
move.moveUp(child)
到
spawn(function() move.moveUp(child) end)
你没有无限循环,而是递归循环:模块中的函数相互调用,直到堆栈溢出。在不同的线程中生成它们会使它成倍增加。
我会通过补间的回调函数触发你的方向改变,那样使用异步性:
local functions = {}
function functions.moveUp(part)
local Tween = game:GetService("TweenService")
local Objective = {}
Objective.Position = Vector3.new(part.Position.X, part.Position.Y + 10, part.Position.Z)
local tweenInfo = TweenInfo.new(5)
local tweenie = Tween:Create(part, tweenInfo, Objective)
tweenie:Play()
tweenie.Completed:Connect(function()
functions.moveDown(part)
end)
end
function functions.moveDown(part)
local Tween = game:GetService("TweenService")
local Objective = {}
Objective.Position = Vector3.new(part.Position.X, part.Position.Y - 10, part.Position.Z)
local tweenInfo = TweenInfo.new(5)
local tweenie = Tween:Create(part, tweenInfo, Objective)
tweenie:Play()
tweenie.Completed:Connect(function()
functions.moveUp(part)
end)
end
return functions
顺便说一句 - 您应该阅读有关补间服务的文档,它非常简洁。您可以使用它在一个 运行 中上下移动。这样您就不必切换这两个功能。您也可以使用正弦波平滑运动。