get_max_inventory_index on a transport belt returns 8 even though all eight inventories return nil

Bugs that are actually features.
kruemmelspalter
Manual Inserter
Manual Inserter
Posts: 4
Joined: Tue May 12, 2026 11:37 am
Contact:

get_max_inventory_index on a transport belt returns 8 even though all eight inventories return nil

Post by kruemmelspalter »

I'm trying to make a special conveyor belt that modifies its contents' spoilage, but this is also the same with the vanilla/base transport belts.
Script to test this out:
Iterating over all inventories just like the example in the LuaControl::get_max_inventory_index documentation

Code: Select all

--control.lua
script.on_event(defines.events.on_built_entity, function(event)
	local entity = event.entity
	if entity.name == "transport-belt" then
		for i = 1, entity.get_max_inventory_index() do
			local inventory = entity.get_inventory(i)
			game.print("inv " .. i .. ": " .. type(inventory))
		end
	end
end)
then place a (yellow) transport belt anywhere and it will print that all eight inventories are (of the type) nil
Expected Behaviour
I would expect one of:
  • get_inventory(i) not to return nil but an actual LuaInventory instead, which then could be modified
  • get_max_inventory_index() to return 0
Of course, the former would be preferable, but I think either way this is unwanted behaviour

If I've made a mistake and this is actually working as intended, please tell me so and excuse me for bothering with this
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 4541
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: get_max_inventory_index on a transport belt returns 8 even though all eight inventories return nil

Post by boskid »

This falls onto the same explanation as given in 131799 - this is just an upper bound, there can be gaps, your code needs to handle a nil inventory. Some values could be adjusted but this is not a rule to be followed. I am going to classify this as a Not a bug.
kruemmelspalter
Manual Inserter
Manual Inserter
Posts: 4
Joined: Tue May 12, 2026 11:37 am
Contact:

Re: get_max_inventory_index on a transport belt returns 8 even though all eight inventories return nil

Post by kruemmelspalter »

my problem is not that there are some inventories that are nil, but all inventories with indices between 1 and 8 (i haven't looked at other indices) are nil, so there is no way for me to access the inventory of a transport belt. is that just not possible at all? or have i missed something and there is an inventory that is accessible with an index > 8? or do the indices start at 0 and the documentation just has a wrong start index?
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 4541
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: get_max_inventory_index on a transport belt returns 8 even though all eight inventories return nil

Post by boskid »

Inventories can only have indexes from 1 up to get_max_inventory_index() [inclusive]. If there would be an inventory with an index that is larger than value returned by get_max_inventory_index() then this would be considered a bug.

Transport belt does not have any inventories.
kruemmelspalter
Manual Inserter
Manual Inserter
Posts: 4
Joined: Tue May 12, 2026 11:37 am
Contact:

Re: get_max_inventory_index on a transport belt returns 8 even though all eight inventories return nil

Post by kruemmelspalter »

so I can insert, check for and remove items on a belt, but it just isn't possible to modify for example the spoil percent of an existing item?
or can i just remove one item and insert it again with a changed parameter? this would probably not preserve item order and lane side
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 4541
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: get_max_inventory_index on a transport belt returns 8 even though all eight inventories return nil

Post by boskid »

Items on transport belt are not organized into inventories, they are organized into transport lines, each containing item stacks. You can query amount of transport lines given transport belt connectable has through LuaEntity::get_max_transport_line_index() (this time there are no gaps and all indexes from 1 up to max_transport_line_index are present), then get a transport line using LuaEntity::get_transport_line and from this you can check how many stacks there are on belt connectable and change their content.
kruemmelspalter
Manual Inserter
Manual Inserter
Posts: 4
Joined: Tue May 12, 2026 11:37 am
Contact:

Re: get_max_inventory_index on a transport belt returns 8 even though all eight inventories return nil

Post by kruemmelspalter »

aaah okay, thank you ^^
did not realize transport lines existed

i think it would still be good to make get_max_inventory_index return 0 instead so this kind of confusion doesn't happen
User avatar
hgschmie
Fast Inserter
Fast Inserter
Posts: 186
Joined: Tue Feb 06, 2024 5:18 am
Contact:

Re: get_max_inventory_index on a transport belt returns 8 even though all eight inventories return nil

Post by hgschmie »

boskid wrote: Tue May 12, 2026 5:36 pm Items on transport belt are not organized into inventories, they are organized into transport lines, each containing item stacks. You can query amount of transport lines given transport belt connectable has through LuaEntity::get_max_transport_line_index() (this time there are no gaps and all indexes from 1 up to max_transport_line_index are present), then get a transport line using LuaEntity::get_transport_line and from this you can check how many stacks there are on belt connectable and change their content.
I read “and change their content”. I tried that a while back here: viewtopic.php?p=652037#p652037 and it did not work for me but never got a response to the post. Can you really change the content of a transport line or just read it?
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 4541
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: get_max_inventory_index on a transport belt returns 8 even though all eight inventories return nil

Post by boskid »

hgschmie wrote: Thu May 14, 2026 2:16 pm I read “and change their content”. I tried that a while back here: viewtopic.php?p=652037#p652037 and it did not work for me but never got a response to the post. Can you really change the content of a transport line or just read it?
It is possible to add and change content. In the linked topic, problem is related to understanding that insert_at_back is allowed to fail (not insert item if it would be too close to existing item) and that it inserts item at the back, which is a position where the items are moving from on that line. First item is inserted but second item fails to be inserted because it would be too close to existing item. Because of this in 2.0.35 there was LuaTransportLine::force_insert_at added to make it possible to insert items at arbitrary position even if they would be too close to other items and this method is specifically not allowed to fail.

The api for transport lines may appear slightly chaotic when trying to change existing items but there is a relatively simple rule: game tries to keep one invariant true, that there are no empty item stacks. If you try modifying LuaItemStack related to an item on a transport line in a way that makes the item stack empty, that item stack is instantly deleted and related LuaItemStack becomes invalid (other LuaItemStacks on the same transport line may also get invalidated due to item stacks reorganized). It is possible to swap LuaItemStack content with another stack which will not invalidate the stack because that operation is atomic on the engine side and invariant is maintained only after the stack swap ends.
Post Reply

Return to “Not a bug”