Better quickbar support for ItemWithEntityData
Posted: Mon Aug 31, 2020 11:33 am
I've been trying to manipulate the quickbar, but it is quite hard to do with the current interface, which only has 4 functions related to it, of which only and are really useful to me.
`convert_remote` correctly replaces the remote with a new remote and transfers the `connected_entity`. Without `get_previous_quickbar` and `fill_in_quickbar`, if the old remote was in the player's quickbar, then it would just be removed.
I've managed to get around this for my usecase by writing `get_previous_quickbar()` to store the contents of every quickbar slot, and then after the switch `fill_in_quickbar` iterates through the new quickbar contents, and any slots that are now empty and weren't previously are assumed to have been belonging to the old item.
Note that similar to point 2 below, this is made much harder by the fact that I cannot tell from get_quick_bar_slot() if that slot is connected to a specific spidertron remote, I can only tell if it is a spidertron remote. If I could tell which spidertron remote it was, then I wouldn't need this feature as much because I'd only have to iterate through the quickbar slot indexes once, which seems much more sensible.
2. `player.set_quick_bar_slot(i, new_stack)` does not work correctly for items such as spidertron remotes. Even though I am passing in a LuaItemStack instead of a LuaItemPrototype, it is only interpreted as a generic prototype, so it behaves really weirdly (because spidertron remotes don't really have a 'generic' state.
Related: 68374
For reference: my mod https://mods.factorio.com/mod/SpidertronWaypoints, specifically https://github.com/tburrows13/Spidertro ... lua#L1-L40
Code: Select all
get_quick_bar_slot(index) → LuaItemPrototype
Code: Select all
set_quick_bar_slot(index, filter)
Aim
I want to be able to swap out a vanilla spidertron remote for a modded one whilst it is in the player's hand. For this I have written the following code:Code: Select all
local function get_previous_quickbar(player)
local quickbar_slots = {}
for i = 1, 100 do
quickbar_slots[i] = player.get_quick_bar_slot(i)
end
return quickbar_slots
end
local function fill_in_quickbar(player, previous_quickbar, new_stack)
for i = 1, 100 do
if previous_quickbar[i] and not player.get_quick_bar_slot(i) then
-- The quickbar filter has been lost since last check, therefore it contained the replaced item
player.set_quick_bar_slot(i, new_stack)
end
end
end
]]
local function convert_remote(stack, old_name, new_name, player)
if stack and stack.valid_for_read and stack.name == old_name then
local connected_spidertron = stack.connected_entity
local previous_quickbar = get_previous_quickbar(player)
stack.set_stack{name=new_name, count=1}
stack.connected_entity = connected_spidertron
fill_in_quickbar(player, previous_quickbar, stack)
end
end
Requested features
1. A way to get all quickbar slot indexes connected to a specific item, such as `LuaItemStack.get_quickbar_slots() -> array of numbers`I've managed to get around this for my usecase by writing `get_previous_quickbar()` to store the contents of every quickbar slot, and then after the switch `fill_in_quickbar` iterates through the new quickbar contents, and any slots that are now empty and weren't previously are assumed to have been belonging to the old item.
Note that similar to point 2 below, this is made much harder by the fact that I cannot tell from get_quick_bar_slot() if that slot is connected to a specific spidertron remote, I can only tell if it is a spidertron remote. If I could tell which spidertron remote it was, then I wouldn't need this feature as much because I'd only have to iterate through the quickbar slot indexes once, which seems much more sensible.
2. `player.set_quick_bar_slot(i, new_stack)` does not work correctly for items such as spidertron remotes. Even though I am passing in a LuaItemStack instead of a LuaItemPrototype, it is only interpreted as a generic prototype, so it behaves really weirdly (because spidertron remotes don't really have a 'generic' state.
Related: 68374
For reference: my mod https://mods.factorio.com/mod/SpidertronWaypoints, specifically https://github.com/tburrows13/Spidertro ... lua#L1-L40