I’m trying to make a wire system.
In this system, the wires are more like nodes, each of them connected to another wire/node, until it eventually reaches the output.
I’ve decided to use recursion to loop through the node and see where they lead (each node has data on where it leads to).
However, with complex giga-machines/cable hubs I’m worried that the 20000 recursion limit won’t be enough for my needs. Is there a way to bypass this limit?
Yup. Don’t use recursion.
The easiest method would probably be to follow an openList/closedList design where you start with things that have changed being in the open list and keep pulling something out of that list. Once you’ve solved it and extracted it’s neighbors to also be in the open list, add it to the closed list. Your loop stops once the open list is empty. And you can’t add an item to the open list if it’s already in the closed list. Use dictionaries for these lists so you can consider the checking speeds constant instead of linear like a table would be.
so it would be a while true do loop?
If so, I would probably have to implement a failsafe like this right?
local Iterations = 1
while true do
if Iterations > 100000 then
task.wait()
Iterations = 1
end
-- rest of the function
Iterations += 1
end
you should use repeat
:
repeat <code> until <specified condition is met>
repeat Iterations += 1 until Iterations == 2000
while #openList > 0 do
local current = table.remove(openList)
table.insert(closedList, current)
--do work on current node here
for i,v in pairs(getNeighbors(current)) do
if not table.find(openList, v) then
table.insert(openList, v)
end
end
end
I did implement it with tables instead of dictionaries though because I’d probably have to reference the docs or count references for the dictionary implementation.
And of course get neighbors(node) needs to be defined.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.