entity.neighbours [answered]

Place to get help with not working mods / modding interface.
matjojo
Filter Inserter
Filter Inserter
Posts: 338
Joined: Wed Jun 17, 2015 6:08 pm
Contact:

entity.neighbours [answered]

Post by matjojo »

So, I was fed up with not being able to find the disconnected underground belts I placed and decided I needed to make something to fix it. I worked on the logic for a while, but there is a piece with the neighbours tag that I just can't get to work.

This is the code:

Code: Select all

local function Basic_detection(UBS_table)

	for UBS_var_1 = 1, 1000000 do
	game.player.print("first part")
		if UBS_table[UBS_var_1] then
			game.player.print("second part")
			if UBS_table[UBS_var_1].neighbours == nil then
				game.player.print("fifth part")
				game.player.print("UBS has detected a disconnected basic underground belt at: " .. "X = " .. tostring(UBS_table[UBS_var_1].position.x) .. ", Y = " .. tostring(UBS_table[UBS_var_1].position.y) )
			else
				--a neigbour has been detected
				game.player.print("third part")
				game.player.print(tostring(game.tick))
			end
		game.player.print("fourth part")
			else
		game.player.print("Detection has stopped, above this message you can see all the hits" .. tostring(UBS_var_1))
		break
		end
	end
end
First when you say you want to check the complete world for disconnected belts this piece of code runs:

Code: Select all

local function BuildTableBTPBTG()

	UBS_table_BTBTG = game.get_surface(1).find_entities_filtered{area = {{-10000, -10000}, {10000, 10000}}, name= "basic-transport-belt-to-ground"}
	game.player.print("UBS: Table has been built, checking for non connected underground belts now.")
	Basic_detection(UBS_table_BTBTG)

end
it uses the find_entities_filtered function to get all entities that are called basic-transport-belt-to-ground.

The created table then gets passed on to the Basic_detection function that is in the code block on top.

That all works. BUT: this does not work:
I check if UBS_table[UBS_var_1].neighbours == nil, but this seems to never be the case, even IF it does not have any connection. so according to the wiki it should be nil then.
When called on an underground transport belt, this is the other end of the underground belt connection, or nil if none.
But, like I said, the thing never returns nil, like never never.
There are some parts added in between printing some messages, most of those will be stripped as they are only for debugging purposes. If you wish to run the code yourselves, This code snippet contains it all:

Code: Select all

require("defines")


local function Basic_detection(UBS_table)

	for UBS_var_1 = 1, 1000000 do
	game.player.print("first part")
		if UBS_table[UBS_var_1] then
			game.player.print("second part")
			if UBS_table[UBS_var_1].neighbours == nil then
				game.player.print("fifth part")
				game.player.print("UBS has detected a disconnected basic underground belt at: " .. "X = " .. tostring(UBS_table[UBS_var_1].position.x) .. ", Y = " .. tostring(UBS_table[UBS_var_1].position.y) )
			else
				--a neigbour has been detected
				game.player.print("third part")
				game.player.print(tostring(game.tick))
			end
		game.player.print("fourth part")
			else
		game.player.print("Detection has stopped, above this message you can see all the hits" .. tostring(UBS_var_1))
		break
		end
	end
end

local function Fast_detection(UBS_table)

	for UBS_var_1 = 1, 1000000 do
	
		if UBS_table.UBS_var_1 then
			if UBS_table.UBS_var_1.neighbours then
				--a niegbour has been detected
			else
				game.player.print("UBS has detected a not-connected fast underground belt at: " .. "X = " .. tostring(UBS_table.UBS_var_1.position.x) .. "Y = " .. tostring(UBS_table.UBS_var_1.position.y) )
			end
		else
			break
		end
	
	end

end

local function Express_detection(UBS_table)

	for UBS_var_1 = 1, 1000000 do
	
		if UBS_table.UBS_var_1 then
			if UBS_table.UBS_var_1.neighbours then
				--a niegbour has been detected
			else
				game.player.print("UBS has detected a not-connected express underground belt at: " .. "X = " .. tostring(UBS_table.UBS_var_1.position.x) .. "Y = " .. tostring(UBS_table.UBS_var_1.position.y) )
			end
		else
			break
		end
	
	end

end
--]]



local function BuildTableBTPBTG()

		UBS_table_BTBTG = game.get_surface(1).find_entities_filtered{area = {{-10000, -10000}, {10000, 10000}}, name= "basic-transport-belt-to-ground"}
		game.player.print("UBS: Table has been built, checking for non connected underground belts now.")
		Basic_detection(UBS_table_BTBTG)

end

local function BuildTableFTPBTG()

		UBS_table_FTBTG = game.surfaces["nauvis"].find_entities_filtered{area = {{-100000, -100000}, {100000, 100000}}, name= "fast-transport-belt-to-ground"}
		Fast_detection(UBS_table_FTBTG)


end

local function BuildTableETPBTG()

		UBS_table_ETBTG = game.surfaces["nauvis"].find_entities_filtered{area = {{-100000, -100000}, {100000, 100000}}, name= "express-transport-belt-to-ground"}
		Express_detection(UBS_table_ETBTG)


end


script.on_event(defines.events.on_tick, function(event)

	if not UBS_var_dns then
		if not UBS_var2 then
			UBS_var2 = 0
		end

		if UBS_var2 == 60 then
			if not game.player.gui.left.UBS_frame then
				game.player.gui.left.add{type = "frame", name = "UBS_frame", caption = "Do you want to check for non-connected underground belt?", direction = "vertical"}
				game.player.gui.left.UBS_frame.add{type = "label", name = "UBS_lbl_warning", caption = "Warning, this might take longer than 2 minutes, depending on the speed of your machine."}
				game.player.gui.left.UBS_frame.add{type = "button", name = "UBS_button_basic", caption = "Check basic belts"}
				game.player.gui.left.UBS_frame.add{type = "button", name = "UBS_button_fast", caption = "Check fast belts"}
				game.player.gui.left.UBS_frame.add{type = "button", name = "UBS_button_express", caption = "Check express belts"}
				game.player.gui.left.UBS_frame.add{type = "button", name = "UBS_button_not", caption = "Do not Check"}
			end
		else
			UBS_var2 = UBS_var2 + 1
		end
	end

end)

script.on_event(defines.events.on_gui_click, function(event)
	if event.element.name == "UBS_button_basic" then
	game.player.gui.left.UBS_frame.destroy()
	UBS_var_dns = true
	BuildTableBTPBTG()
	elseif event.element.name == "UBS_button_fast" then
	game.player.gui.left.UBS_frame.destroy()
	UBS_var_dns = true
	BuildTableFTPBTG()
	elseif event.element.name == "UBS_button_express" then
	game.player.gui.left.UBS_frame.destroy()
	UBS_var_dns = true
	BuildTableETPBTG()
	elseif event.element.name == "UBS_button_not" then
	game.player.gui.left.UBS_frame.destroy()
	UBS_var_dns = true
	end
end)
Keep in mind I'm only working on the piece of code that is associated with the basic transport belts. once that is finished I'll copy the code and edit it a bit to make it work with the other two underground belt types.
Last edited by matjojo on Tue Jan 19, 2016 7:55 pm, edited 1 time in total.
Rseding91
Factorio Staff
Factorio Staff
Posts: 15913
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: entity.neighbours

Post by Rseding91 »

The documentation is indeed incorrect in this case.

It will return a table which has no contents or the connecting opposite side of the underground belt.

So either entity.neighbours[1] == nil or entity.neighbours[1] == the other end of the underground belt.
If you want to get ahold of me I'm almost always on Discord.
matjojo
Filter Inserter
Filter Inserter
Posts: 338
Joined: Wed Jun 17, 2015 6:08 pm
Contact:

Re: entity.neighbours

Post by matjojo »

Rseding91 wrote:The documentation is indeed incorrect in this case.

It will return a table which has no contents or the connecting opposite side of the underground belt.

So either entity.neighbours[1] == nil or entity.neighbours[1] == the other end of the underground belt.
Thanks, that worked. This is the working code snippet for anyone interested, (Round is just a basic function meant to round numbers to 0 decimals.)

Code: Select all

local function Basic_detection(UBS_table)

	for UBS_var_1 = 1, 1000000 do
		if UBS_table[UBS_var_1] then
			if UBS_table[UBS_var_1].neighbours[1] == nil then
				game.player.print("UBS has detected a disconnected basic underground belt at: " .. "X = " .. tostring(UBS_table[UBS_var_1].position.x) .. ", Y = " .. tostring(UBS_table[UBS_var_1].position.y) )
			else
				--a neighbour has been detected
			end
		else
		        game.player.print("Detection has stopped, above this message you can see all the hits")
		        game.player.print("You are now at: " .. "X = " .. tostring(Round(game.player.position.x)) .. ", Y = " .. tostring(Round(game.player.position.y)))
		        break
		end
	end
end
Post Reply

Return to “Modding help”