Hi, I have one big table named tbl, and keys titled as Blocks. Here's an example of how I have each Block set up:
tbl.Block4 = {
{
{
Sensor = sensors.X3,
Signal = {
Model = signals.S3A,
PathOptions = {
{
Name = "Station",
Switches = {
[switches.V2] = 2
}
},
{
Name = "Main Line",
Switches = {
[switches.V2] = 1
}
}
}
}
}
},
{
{
Sensor = sensors.X4B,
Signal = {
Model = signals.S4B
}
},
{
Sensor = sensors.X4A,
Signal = {
Model = signals.S4A
}
}
}
}
I know, the tables go super deep, but I have it that way for a reason. My goal is to give each table with the sensor and signal inside it another table called "Adjacent", which contains the other table that has the same sensor value. I have a recursive function that I got with the help of the community here:
module.tableRec = function(tbl, func, level) --Add an OPTIONAL paremeter to be used in the recursion loop.
--If we weren't passed a recursion level, assume we are starting a recursion loop and set to 0
if not level then level = 0 end
--Add printouts for debugging. Also, use typeof - (it's more compatible with ROBLOX)
if typeof(tbl) ~= "table" then warn("Passed table not a table!") return end
for i, v in pairs(tbl) do
func(i, v, level)
if typeof(v) == "table" then --If we have found a table, call the recursive function on it!
module.tableRec(v, func, level + 1) --Add one to the current level to indicate a new level of recursion
end
end
end
Finally, this is what I'm doing to get the adjacent points. It seems to work, until a stack overflow happens.
function findAdjacent(value)
functions.tableRec(tbl, function(i, v, level)
local block
local endPoint
if level == 0 then
block = v
elseif level == 1 then
endPoint = v
elseif level == 2 then
if v ~= value and v.Sensor == value.Sensor then
print("found adjacent")
value.Adjacent = {block, endPoint, v}
end
end
end)
end
functions.tableRec(tbl, function(index, value, level)
if level == 2 then
findAdjacent(value)
end
end)
I would really appreciate how I could identify which tables share the sensors without putting too much stress on the game. Thanks!

1 comment
Thanks a lot, I thought that nested for loops automatically meant inefficiency, but it turns out it's the better way to go.