"Attempt to get length of nil value"?

I’m making a Roguelike game and I am currently working on the Dungeon Generation using an open source Dungeon Generation page I found on GitHub. So far it’s working fine, but I’ve encountered an error that seems very simple, I just don’t know how to go about fixing it. Here’s a screenshot:


Here is the function that is causing the error:

function Level:getRoomTree()
	if #self.rooms < 1 then error("Can't generate room tree, no rooms exists") end
	
	local root, lastLeaf = prims(table.clone(self.rooms)) -- This is the line causing the error
	self.rootRoom = root
	self.endRoom = lastLeaf

	return root
end

I decided I’d print out the value that the prims function is receiving and it returned nil,
image
Here is the code for the prims function:

function HelpFunctions:prims(unvisited)
	print(unvisited)
	local len = #unvisited
	local root = remove(unvisited, 1)
	if #unvisited==0 then return root, root end

	local visited={}
	insert(visited, root)
	local v0
	local endIndex
	repeat
		local dist = 1e309    -- ~inf
		for i = 1, #visited do
			for j = 1, #unvisited do
				if (unvisited[j]:distanceTo(visited[i]) < dist) then
					dist = unvisited[j]:distanceTo(visited[i])
					v0 = visited[i]
					endIndex = j
				end
			end
		end
		local v1 = remove(unvisited, endIndex)
		v0:addNeighbour(v1)
		insert(visited, v1)
	until #visited == len

	return visited[1], visited[#visited]
end

I’m just a bit confused, help is appreciated! Thanks!

2 Likes

Directly use HelpFunctions:prims, making a variable pointing to it’s function changes the meaning.

function HelpFunctions:prims(unvisited) --> HelpFunctions.prims(self, unvisited)

The self is implicit, the calling object. So you’d have to do prims(HelpFunctions, unvisited), but why would you do that in this case?

2 Likes

This actually worked perfectly! Thank you so much!

To note, you could look at all of the helper functions, and look if the keyword self is used, if it’s not you can change all the function definitions to be HelperFunctions.func_name, and store references as you wish. I’d actually assume by it’s name it’s containing pure functions, that you should be able to do that; but going and editing the modules like that sort of defeats the off-the-shelf nature of it.

1 Like

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