Neighbours

Place to get help with not working mods / modding interface.
Post Reply
User avatar
Mango
Long Handed Inserter
Long Handed Inserter
Posts: 95
Joined: Fri Feb 22, 2013 6:27 pm
Contact:

Neighbours

Post by Mango »

I'm having problems with entity.neighbours parameter.
In documentation it says this:

When called on a pipe-connectable entity, this is an array of entity arrays of all entities a given fluidbox is connected to.

As I understand it it looks like this:

E = entity
p = parameter

|E1|E2|E3|....
|p1|p1|p1|
|p2|p2|p2|
.
.
.

But when I try to count how many neighbours does that middle pipe have, it says its 1.

Image

I count it like this:

Code: Select all

function tableLength(anyTable)
  local count = 0
  for _ in pairs(anyTable) do count = count + 1 end
  return count
end
Edit: Or is there and array for every entity type?
Hm.... so we have a mystery donor... intriguing.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13229
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Neighbours

Post by Rseding91 »

For fluidboxes the result is an array of array of entities.

Because 1 entity can have multiple fluidboxes of which each can be connected to multiple entities.
If you want to get ahold of me I'm almost always on Discord.

User avatar
Mango
Long Handed Inserter
Long Handed Inserter
Posts: 95
Joined: Fri Feb 22, 2013 6:27 pm
Contact:

Re: Neighbours

Post by Mango »

Thanks for the reply, but I think I do not undestand.

An entity can have multiple fluidboxes, thats obvious,
but how can 1 fluidbox connect to more entities?
Hm.... so we have a mystery donor... intriguing.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13229
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Neighbours

Post by Rseding91 »

Mango wrote:Thanks for the reply, but I think I do not undestand.

An entity can have multiple fluidboxes, thats obvious,
but how can 1 fluidbox connect to more entities?
The storage tank is 1 fluidbox but has 4 different in/outs. The standard pipe is 1 fluidbox but has 4 different in/outs.

A "fluid box" is the thing that holds the actual fluid. The little connection symbol you see with alt-info enabled is the connections to that 1 fluidbox.
If you want to get ahold of me I'm almost always on Discord.

User avatar
Mango
Long Handed Inserter
Long Handed Inserter
Posts: 95
Joined: Fri Feb 22, 2013 6:27 pm
Contact:

Re: Neighbours

Post by Mango »

Oooh... I thought fluidbox is a pipe connection :oops:
Sorry and thanks.
Hm.... so we have a mystery donor... intriguing.

User avatar
KroshkaRoo
Inserter
Inserter
Posts: 20
Joined: Wed Dec 14, 2016 8:46 am
Contact:

Re: Neighbours

Post by KroshkaRoo »

Hello.
I have a question about this same subject.
There is a mod, Subterrain_modified. Its main feature is long underground conveyors and pipes. Moreover, during construction, the distance between the entrance and the exit is calculated, and the necessary number of conveyor belts or pipes, depending on the distance, is taken from the player's inventory. And when dismantling - returns.
With conveyors, everything is fine, but with pipes ...
In Factorio 0.15 this script worked

Code: Select all

--actions for subterrain pipes on built
function subterrainPipesOnBuilt(event)
    if settings.global["underground-pipe-cost-multiplier"].value <= 0 then return nil end --no pipe cost

    local entity = event.created_entity
    local player = game.players[event.player_index]
    entity.rotatable = false --Prevent rotation, as that can be used to game the system

    local otherPipe = entity.neighbours[2] or nil

    if not otherPipe then
        return nil --no need to do anything if this is the first placed
    end

    local distance = getDistance(entity, otherPipe)
    local calc = (math.floor(distance * settings.global["underground-pipe-cost-multiplier"].value))

    otherPipe.force = "player"

    if checkCount(player, calc, "pipe") then
        entity.destroy()
        player.print("You don't have enough pipes in your inventory to create this length.")
        player.print("You need " .. checkCount(player, calc, "pipe") ..  " more. ")

        restoreDeniedItem(player, sp)
    else
        player.remove_item{name = "pipe", count = calc} --charge the player the pipes
    end
end
And in 0.16 it does not work anymore.
How to make it work?
Thank you.

P. S. Sorry, it is Google Translate.

User avatar
KroshkaRoo
Inserter
Inserter
Posts: 20
Joined: Wed Dec 14, 2016 8:46 am
Contact:

Re: Neighbours

Post by KroshkaRoo »

I found the solution to the problem.
Just a table with coordinates placed in another table.
The solution looks like this:

Code: Select all

    local otherPipe = entity.neighbours[1][1] or nil
Thanks to all! :D

orzelek
Smart Inserter
Smart Inserter
Posts: 3911
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: Neighbours

Post by orzelek »

For the future when you are not sure whats in property I recommend this:

Code: Select all

log(serpent.block(interesting_thing))
It's really useful for debugging too - especially of raw data on data stage.

Bilka
Factorio Staff
Factorio Staff
Posts: 3158
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Neighbours

Post by Bilka »

orzelek wrote:For the future when you are not sure whats in property I recommend this:

Code: Select all

log(serpent.block(interesting_thing))
That returns {} when printing entity.neighbours[1] in this case...
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

User avatar
KroshkaRoo
Inserter
Inserter
Posts: 20
Joined: Wed Dec 14, 2016 8:46 am
Contact:

Re: Neighbours

Post by KroshkaRoo »

orzelek wrote:For the future when you are not sure whats in property I recommend this:

Code: Select all

log(serpent.block(interesting_thing))
It's really useful for debugging too - especially of raw data on data stage.
This is helpful, thanks!

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2904
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Neighbours

Post by darkfrei »

KroshkaRoo wrote:
orzelek wrote:For the future when you are not sure whats in property I recommend this:

Code: Select all

log(serpent.block(interesting_thing))
It's really useful for debugging too - especially of raw data on data stage.
This is helpful, thanks!
or without comment

Code: Select all

my_text = serpent.block(my_table, {comment = false})

Post Reply

Return to “Modding help”