If I’m calling the same functions in the example below over and over without stopping, would that be bad? To my understanding it’s not good if a thread is ongoing for a long time without ending. Would creating a new thread with a coroutine alleviate this in my case or does the MoveToFinished connection creating a new thread make it ok.
function move(Humanoid)
Humanoid:MoveTo(Vec3)
Humanoid.MoveToFinished:Connect(function()
action(Humanoid)
end)
end
function action(Humanoid)
-- Does something for a bit
move(Humanoid)
end
move(Humanoid)
This is recursion, recursion is fine for certain use cases, for others you should just use loops. For this case I think recursion should be fine
Recursion is some of the most beautiful and elegant code. However, they do typically take up more runtime memory than iteration.
For standard practice, when you have recursion, you want to have a way to exit out of the code. So for the main code loop move() function,
It should have two paths to take:
- If the player can keep moving, then keep doing move()
- Else, if the player can’t keep moving, let’s say humanoid is gone or something, then you need to have something that breaks off and stops the endless recursive loop.
Hope it helps!
2 Likes