Page 1 of 1

entity.neighbours not working

Posted: Wed Sep 06, 2017 5:56 am
by withers
In the factorio api, there's an LuaEntity function called "neighbours". The documentation says "When called on a pipe-connectable entity, this is an array of all entities this pipe is connected to."

It doesn't seem to work. I have the following function, which is intended to return true if the pipe is an end pipe (has only one connection.)

Code: Select all

function weartear.is_end_pipe(entity)
	local result = false
	local neighbours = entity.neighbours
	if neighbours then 
		local the_real_size = 0
		game.print("size of this thing is " .. #neighbours)
		for i=#neighbours,1, -1 do
			game.print("examining neighbor#"..i)
			if neighbours[i] then
				game.print("neighbor#" .. i .. " is real")
				if neighbours[i].valid then
					game.print("neighbor#" .. i .. " is a " .. neighbours[i].name)
                                        the_real_size = the_real_size + 1
				end
			else
				game.print("neighbor#" .. i .. " was nil.")
			end
		end
		if the_real_size == 1 then result = true end
	end
	return result
end
Running this in game with a hotkey assigned to run it on whatever is selected, I get results all over the board.

Example: A pipe connected to a tank and another pipe reports only one neighbor, the tank. A horizontal pipe connected on either side reports 4 neighbors: 1 pipe and 3 "nils". I had to actually iterate through all the neightbors in the table and tabulate "the_real_size" by nil checking every item in the table, but even that's not working because in alot of cases it doesn't detect all the neighbors. eg: a pipe with 2 other pipes connected only returns 1 neighbor, giving a false positive. It's very inconsistent.

Am I looking at this wrong? Or is it a bug?

Re: entity.neighbours not working

Posted: Wed Sep 06, 2017 6:12 am
by Rseding91
Thanks for the report. It was actually pretty messed up.

A given fluidbox can have between 1 and N connections and a given entity can have between 0 and N fluidboxes. In most cases if an entity has a fluidbox it just has 1 but the assembling machine/furnace can have multiple (input and output with multiple types of fluids each).

I've fixed it for 0.16 so it produces the following:

An array of entity arrays: the outer array index is the fluidbox index in the entity and the inner array is an array of all entities that fluidbox is connected to.

So for a pipe you'd end up doing: #neighbours[1] == the number of entities the pipe is connected to.