entity.neighbours [answered]
Posted: Mon Jan 18, 2016 7:44 pm
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:
First when you say you want to check the complete world for disconnected belts this piece of code runs:
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.
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:
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.
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
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
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.
But, like I said, the thing never returns nil, like never never.When called on an underground transport belt, this is the other end of the underground belt connection, or nil if none.
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)