[0.17.34] inserter.drop_target, and LuaTransportLine.input_lines sometimes not available on building an inserter

Bugs that are actually features.
Post Reply
LeonSkills
Burner Inserter
Burner Inserter
Posts: 14
Joined: Mon Feb 11, 2019 12:37 pm
Contact:

[0.17.34] inserter.drop_target, and LuaTransportLine.input_lines sometimes not available on building an inserter

Post by LeonSkills »

Not sure if this should be in bug reports or interface requests. But there is some (seemingly inconsistent) weird behaviour.

LuaEntity.drop_target and LuaEntity.pickup_target of inserters are nil when building inserters in on_built_entity. Even though the entities to pickup from/drop of to are already placed.
You can still still get the same behaviour by inserter.surface.find_entities_filtered{position = inserter.drop_position}. So not a big problem.

And probably related
The LuaTransportLine.input_lines and LuaTransportLine.output_lines of the transport lines of the transport belt the inserter drops to/picks up from sometimes return as empty lists when building the inserter. They aren't empty when building the belts.

To recreate
in control.lua

Code: Select all

script.on_event(defines.events.on_built_entity, function(event)
  if event.created_entity.type == "inserter" then
    local inserter = event.created_entity
    print("Building an inserter at", inserter.position.x, inserter.position.y)
    print("This one shouldn't be nil on creation if the targets are there:",
        inserter.drop_target, inserter.pickup_target)
    local drop_entity = inserter.surface.find_entities_filtered{
      position = inserter.pickup_position,  --or inserter.drop_position
    }
    if drop_entity and #drop_entity == 1 then
      local belt = drop_entity[1]
      if belt and belt.type == "transport-belt" then
        print("Amount of input lines 1:", #belt.get_transport_line(1).input_lines)
        print("Amount of output lines 1:", #belt.get_transport_line(1).output_lines)
        print("Amount of input lines 2:", #belt.get_transport_line(2).input_lines)
        print("Amount of output lines 2:", #belt.get_transport_line(2).output_lines)
      end
    end
  elseif event.created_entity.type == "transport-belt" then
    local belt = event.created_entity
    print("Building a transport belt at", belt.position.x, belt.position.y)
    print("Amount of input lines 1:", #belt.get_transport_line(1).input_lines)
    print("Amount of output lines 1:", #belt.get_transport_line(1).output_lines)
    print("Amount of input lines 2:", #belt.get_transport_line(2).input_lines)
    print("Amount of output lines 2:", #belt.get_transport_line(2).output_lines)
  end
end)
Build some beautiful transport belts and then place an inserter in the middle.
Image
The belts correctly indicate that the transport_lines of previous belts are connected when #output_lines or #input_lines are 1. But when building the inserter and calling it on the same exact entity, the input/output_lines are sometimes empty.

Sometimes? Yes. It's inconsistent.
Sometimes it does give the correct number of input_lines/output_lines. (I can't find a pattern on when this is, might have to retry a few times).
But then repetitively deleting and rebuilding the inserter will eventually turn it all to 0, but not back to 1.

Here is the output of where I first built 3 belts, then an inserter pickup up from the belt, deleted the inserter, and rebuilt it:

Code: Select all

Building a transport belt at    5.5     0.5
Amount of input lines 1:        0
Amount of output lines 1:       0
Amount of input lines 2:        0
Amount of output lines 2:       0
Building a transport belt at    5.5     2.5
Amount of input lines 1:        0
Amount of output lines 1:       0
Amount of input lines 2:        0
Amount of output lines 2:       0
Building a transport belt at    5.5     1.5
Amount of input lines 1:        1
Amount of output lines 1:       1
Amount of input lines 2:        1
Amount of output lines 2:       1
Building an inserter at 4.5     1.5
This one shouldn't be nil on creation if the targets are there: nil     nil
Amount of input lines 1:        1
Amount of output lines 1:       1
Amount of input lines 2:        1
Amount of output lines 2:       1
Building an inserter at 4.5     1.5
This one shouldn't be nil on creation if the targets are there: nil     nil
Amount of input lines 1:        0
Amount of output lines 1:       0
Amount of input lines 2:        0
Amount of output lines 2:       0
On the first build the amount of input and output lines of both lines are all four 1 as expected. But then deleting and rebuilding the inserter all the input and output lines are empty.

I haven't tested it with underground belts/splitters/loaders/drills, but I can imagine the same thing happens there

I'm trying to figure out to which transport line an inserter inserts items to. Which is fine as it's usually top right, except for that one exception where it inserts top left, for which I need to check the input_lines of the transportline.
Related interface request for that: viewtopic.php?t=59076

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

Re: [0.17.34] inserter.drop_target, and LuaTransportLine.input_lines sometimes not available on building an inserter

Post by Rseding91 »

Thanks for the report. Neither of the things you describe are bugs.

Inserters don't have a pickup target or drop target when they're first built. They find both of them during their ::update() function and or runtime as the things they interact with come and go.

The reason you're getting 0 for #input_lines and or #output_lines is because the array they return is not guaranteed to be sequential and so the # operator sucks and gives you 0 back when the array is actually something like: {[2]= .., [3] = ...}. You want table_size() which will correctly give you the number of elements in the table regardless of what the elements are.
If you want to get ahold of me I'm almost always on Discord.

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

Re: [0.17.34] inserter.drop_target, and LuaTransportLine.input_lines sometimes not available on building an inserter

Post by Rseding91 »

Correction: the reason you get 0 for output_lines or input_lines is because the transport line has no outputs or inputs depending on the merged state. When it's merged fully there is no input because the "back" of the belt has no input and there is no output because the "front" of the belt has no output. Transport lines refer to the entire merged transport line and not the specific belt piece. However building inserters causes the belts to unmerge/remerge and that is spread over some ticks.
If you want to get ahold of me I'm almost always on Discord.

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

Re: [0.17.34] inserter.drop_target, and LuaTransportLine.input_lines sometimes not available on building an inserter

Post by Bilka »

More info:
The reason the input/output lines are not empty when building the belts is that the different lines are not merged immediately - you can observe this if you turn on the debug option to see the transport lines. In that case, the reported input and output lines are simply the belts in front/behind the just built belt. If you wait long enough, those lines will merge and you will get no input/output lines again.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

Post Reply

Return to “Not a bug”