Page 1 of 1
How to move item from inventory to cursor?
Posted: Mon Jan 06, 2020 6:48 pm
by Jorn86
I want to move a specific (stack of) item(s) from the player's inventory to their cursor. I would do:
Code: Select all
local count = inventory.remove{name=item_name, count=game.item_prototypes[item_name].stack_size}
if count > 0 then
player.cursor_stack.set_stack{name=item_name, count=count}
end
This works for simple items, but not for complicated ones like power armor or deconstruction planners. The armor loses its equipment, and the planner its filters. How do I properly move the item?
If not possible, how do I detect an item that's not safe to move?
Re: How to move item from inventory to cursor?
Posted: Mon Jan 06, 2020 6:51 pm
by DaveMcW
https://lua-api.factorio.com/latest/Lua ... sfer_stack
Code: Select all
for i = 1, #inventory do
if inventory[i].name == item_name then
player.cursor_stack.transfer_stack(inventory[i])
break
end
end
Re: How to move item from inventory to cursor?
Posted: Mon Jan 06, 2020 7:14 pm
by Jorn86
Thanks! I needed to add a
check, but otherwise this works perfectly.
Re: How to move item from inventory to cursor?
Posted: Sun Jan 12, 2020 7:23 am
by eradicator
Jorn86 wrote: ↑Mon Jan 06, 2020 7:14 pm
but otherwise this works perfectly.
Keep in mind that transfer_stack does not reserve the source inventory slot like when the player picks up the item manually because the api doesn't support that currently. So if the player has a full inventory you might end up triggering the very annoying "i have a stack on the cursor but i can't put it anywhere" problem.
Re: How to move item from inventory to cursor?
Posted: Sun Jan 12, 2020 7:38 am
by Jorn86
That's a good point! Is there something I can do (with the LUA api) to prevent that?
Re: How to move item from inventory to cursor?
Posted: Sun Jan 12, 2020 2:13 pm
by eradicator
Jorn86 wrote: ↑Sun Jan 12, 2020 7:38 am
That's a good point! Is there something I can do (with the LUA api) to prevent that?
As i said: no. Reserved slots have very limited api support (and i can't find the thread where i requested better support...).
Re: How to move item from inventory to cursor?
Posted: Sat Jan 18, 2020 12:05 pm
by Honktown
My first thought is throwing something on the ground if it can't fit in the inventory: luasurface->spill_item_stack
Re: How to move item from inventory to cursor?
Posted: Wed Jan 22, 2020 3:26 pm
by robot256
I'm just running into the same issue. I need to make a "reusable" capsule item, so I re-insert to the player's cursor stack after every use, but it loses its reserved slot in inventory. Another author's solution was to make the capsule stack-size=2, so that the second item in the stack reserves the slot while you re-insert the first item. But it's clunky and awkward if you drop one somewhere.
In this case, I might have the script temporarily set actual item filters in the inventory to make sure there is space to put it back.
Edit: There is already an
interface request posted on the subject. Hopefully it will be added at some point in 0.18!
Re: How to move item from inventory to cursor?
Posted: Wed Jan 22, 2020 3:34 pm
by eradicator
Off topic, so minimal answer: Capsule prototype supports "uses_stack = false" for infinite stacks. See
Tapeline.
Re: How to move item from inventory to cursor?
Posted: Wed Jan 22, 2020 4:06 pm
by Honktown
I had another idea: as long as the armor/blueprint wasn't destroyed in the process, you should be able to transfer the item stack, if it's a "full specification" (luaitemstack) or create a new item stack with the grid/blueprint settings copied over (that'd be some work).
If the entity isn't valid when you want, then you can listen for on_player_cursor_stack_changed when an item with a known breakable type is in the cursor, and if it's one of those 'hazardous' things to copy, save the information needed to restore after it is invalid. Have to watch for armor without grids, or blueprints that aren't valid for read, whatever else.
Re: How to move item from inventory to cursor?
Posted: Wed Jan 22, 2020 5:53 pm
by robot256
eradicator wrote: ↑
Off topic, so minimal answer: Capsule prototype supports "uses_stack = false" for infinite stacks. See
Tapeline.
Brilliant! I was scouring the documentation for a list of flags that might include something like that but hadn't found it yet.