LuaPlayer::get_hand_index / LuaPlayer::set_hand_index or similar
Posted: Wed Feb 27, 2019 12:48 am
Currently, there is no mechanism for a mod to select an item in the player's inventory using the exact same logic as a player clicking it. The current 'cleanest' method appears to be some variation upon:
However, this has a few problems:
Note: I had previously requested a LuaPlayer::set_selected_index. There used to be a LuaPlayer::get_selected_index, but that appears to be gone as of 0.17
Code: Select all
stack = player.get_inventory(defines.inventory.inventory_main)[index]
if player.clean_cursor() then
player.cursor_stack.swap_stack(index)
end
- There is no hand in the inventory, thus the space for the newly selected item is not reserved.
- There is no guarantee the selected item will be returned to the same inventory slot after it is 'dropped'. (Mostly pertinent to manually-sorted inventories.)
- It's far too easy for a mod author to accidentally destroy inventory contents (i.e. by forgetting to check if clean_cursor() actually succeeded.)
Note: I had previously requested a LuaPlayer::set_selected_index. There used to be a LuaPlayer::get_selected_index, but that appears to be gone as of 0.17