The ROBLOX Wiki display's this code for the Algorithm and adds a procedure I need help understanding what the :neighbors() is used on and how the nodes work. I am using the algorithm for pathfinding for NPC's. I understand this is very unreasonable question to be posted on the ScriptingHelpers website but if I don't ask here where else am I supposed to ask? And ROBLOX Forums don't count because of the fact that no one will answer and the question will be gone down the page in seconds.
ROBLOX Wiki Procedure:
- Add starting node to closed set.
- For every node in the closed set, find all the adjacent nodes and calculate the distance to that node. **** If the node is already in the closed set, ignore it.
- Choose the node nearest to a node in the closed set
- Add it to the closed set.
- Repeat from step 2 until all nodes have been visited.
ROBLOX Wiki Code:
function Prim(start)
local closed = {}
closed[start] = true
--search until all nodes have been found
while nodes_to_be_found do
local current_best
--go through the closed set and find their neighbors
for parent in pairs(closed) do
for _, node in ipairs(parent:neighbors()) do
--make sure node is not in closed set already
if not closed[node] then
--compare to what is currently the best node
if not current_best or node:distance(parent) < current_best:distance(current_best.parent) then
node.parent = parent
current_best = node
end
end
end
end
--add the selected node to closed set
closed[current_best] = true
end
end

2 comments
Simple question that will make me sound stupid but what is a node and how do I set one up
Pathfinding is finding a path. That path is made up of points. We call those points nodes. In my example,
newNodeturns a position into a node. Read the code and the comments.