How to make a node tree system?

every part welded to the green main part (to the left of the bottom most yellow part) needs to turn green.

every part welded to the yellow main part needs to turn yellow

if blocks are by themselves or welded between themselves, but aren’t welded to a yellow or green block, then they’re gray.

if a yellow tree gets one of its parts welded to a green tree, the entire thing turns green.

I don’t even know where to start.

Thanks!

you can traverse the tree of nodes with recursion (function calls itself)
you can also google implementations of this because its a really common algorithm

example for if green node is connected:

create a function that checks if one node is connected to a green node
if none of the other nodes are green, then inside that function, call itself with the other nodes as the argument to check if those nodes are connected to a green node

skip the current node if its already visited

--example pseudocode, implement some parts of it yourself

--this visited table should be reset when isConnectedToGreen is called non-recursively
local visited = {}

local function isConnectedToGreen(node) 

--tables in roblox can store anything as a key including instances, so this works
  if visited[node] then return end
  visited[node] = true

  if thisNodeIsGreen then return true end

  for _, child in ipairs(node:GetChildren()) do
    --return true if any related node is green
    if isConnectedToGreen(child) then return true end
  end

  --return false if no nodes in this entire tree is green
  return false

end

Okay, this looks promising. Give me like 30 minutes while I try to wrap my pea brain around this :sweat_smile:

What do you mean by for _, child in ipairs(node:GetChildren()) do?
I don’t think that’ll work for my case, because it can only go down the tree, to the children, not up the tree, to the ancestors. What else could I do? Thanks!

I used this script, but modified it, and it ended up looking like this:

image

Thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.