Build graphs using constraints

Here’s a quick tip: You can build graphs using constraints at edit time and then parse those constraints into a data structure on runtime. This is nice because

  1. Constraints are visible in edit mode
  2. Constraints are easy to create and destroy
  3. Doesn’t require a plugin

WeldConstraints are a good option because they don’t require intermediate Attachments.

If applicable, you can take this one step further by using hoarcekat to draw the object that the graph represents.


Here’s a snippet for how I parsed the line graph in the bottom image.

local function GetPoints(part, points)
	table.insert(points, part.Position)

	local foundChild = false
	for _, joint in part:GetJoints() do
		if joint.ClassName == "WeldConstraint" and joint.Part0 == part and joint.Part1 then
			if foundChild then
				error("Node has multiple children")
			else
				foundChild = true
				GetPoints(joint.Part1, points)
			end
		end
	end
end

local points = {}
GetPoints(workspace.Start, points)

Note that the WeldConstraints are treated as directed edges that point from Part0 to Part1.

4 Likes