Page 1 of 1

on_player_main_inventory_changed fires before sorting the inventory

Posted: Thu Jan 24, 2019 5:49 am
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)

Re: on_player_main_inventory_changed fires before sorting the inventory

Posted: Thu Jan 24, 2019 6:17 am
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.