on_player_main_inventory_changed fires before sorting the inventory

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
zysnarch
Burner Inserter
Burner Inserter
Posts: 10
Joined: Thu Jan 10, 2019 4:26 am
Contact:

on_player_main_inventory_changed fires before sorting the inventory

Post by zysnarch »

This feels like a bug to me, but I'm not sure. If you have an item stack in your cursor, then "drop" it back to your inventory by pressing "q", the on_player_main_inventory_changed event fires, as expected. However, the item you just dropped appears at the end of the inventory list (when indexed by the [] operator). Immediately after (before the next tick), the inventory is then sorted - without generating another on_player_main_inventory_changed event. This matters if you rely on the inventory order later to grab the item you just dropped - it will have moved to a different index with no way of knowing.

I can work around this by adding a temporary on_tick listener[1], but it would be nice if the event only fired after the inventory was fully settled.

[1] my workaround:

Code: Select all

script.on_event(defines.events.on_player_main_inventory_changed, function(event)
  -- inventory not sorted yet
  local player = game.players[event.player_index]
  script.on_event(defines.events.on_tick, function(event)
    -- now inventory is sorted and won't change out from under us
    script.on_event(defines.events.on_tick, nil)
    updateInventoryInfo(player)
  end)
end)
Rseding91
Factorio Staff
Factorio Staff
Posts: 14363
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: on_player_main_inventory_changed fires before sorting the inventory

Post by Rseding91 »

The order of the items in the inventory can change multiple times during a tick before the event is fired so you can never rely on an item being in an exact location. You always have to go over the inventory and find what you're looking for.
If you want to get ahold of me I'm almost always on Discord.
Post Reply

Return to “Modding interface requests”