Best way to define connected 'Nodes' in a tree?

What is the most effective and flexible method for defining connections between nodes in a folder of parts?

To better explain what I’m talking about here’s an image:


So as you can see the ‘start node’ would have 2 connected nodes. One path a dead end and the other branches and then branches again. Hopefully this shows what I’m going for.

What I’m unsure of is how I should ‘define’ which nodes are connected to each other. I could possibly have hundreds of these nodes and want to be able to easily change them around if need be so defining them manually in code would be way too much of a pain.

Best idea I had so far is to set an attribute for each node called ‘ConnectedNodes’ and that will be either 1,2,3,4, etc depending on how many nodes can be branched to from it. Then make a for loop that goes through all the nodes, and depending on the number of connected nodes (let’s say it’s 2) find the 2 nearest nodes and put them inside a table or something along those lines. Now the problem I see with this method is if 1 branch has nodes much closer together then the other branch it could incorrectly set 2 nodes in the same branch to it’s ‘connectednodes’ if the second one is closer then the first node of the other branch. That said I could work with this limitation if need be.

Now I don’t know if there’s a better solution then this that’s not just defining them all manually but I’d be real curious to know how others would approach this.

1 Like

Do these nodes get generated at runtime using some script or did you manually place them? If they’re programmatically generated it would be a lot easier to keep track of the connections.

1 Like

I would just have a pointer to where the next node should point to. Much like this:

The only difference is that I would generate it using a script but edit it manually as well.

1 Like

I would have a Folder called “Neighbors” and put ObjectValues inside referencing each neighbor, and then make a plugin to visualize the graph and make it easier to edit.

1 Like

A hilariously simple solution would just be to put ObjectValues inside each node, call each value “Neighbour” or something, with each ObjectValue having its Value property set to the other node it’s connected to

If I were creating node trees out of parts on ROBLOX without defining them in code explicitly, I’d probably do something like that : P

You could probably design a simple plugin or command-line script to automate connecting and visualising nodes, like what Emma suggested!

1 Like

If these nodes are static & aren’t dynamically created you could also use weldConstraints to connect these nodes to the adjacent nodes. That way you can call node:GetConnectedParts() and directly get the adjacent nodes as instances.

To “easily” create these weldConstraints (better than placing them by hand):
Place a part between every node that you want to be connected (basically parts instead of the red lines you drew). Make sure these part connectors touch the two nodes you want to connect. Place all the connectors in one folder then you could run a script like this to automatically create the weldConstraints:

local connectors = YOUR_FOLDER_PATH_HERE
for _, connector in pairs(connectors:GetChildren()) do
    local touchingParts = connector:GetTouchingParts()
    local weld ="WeldConstraint", touchingParts[1])
    weld.Part0 = touchingParts[1]
    weld.Part1 = touchingParts[2]

It suffices if one part has that weld, if you created a weld in touchingPart[1] and [2] then every connection would be double with :GetConnectedParts().
Make sure every connector only touches two nodes and no other part.

1 Like