How to move item from inventory to cursor?

Place to get help with not working mods / modding interface.
Jorn86
Long Handed Inserter
Long Handed Inserter
Posts: 70
Joined: Sun May 01, 2016 7:08 pm
Contact:

How to move item from inventory to cursor?

Post 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?
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3716
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: How to move item from inventory to cursor?

Post 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
Jorn86
Long Handed Inserter
Long Handed Inserter
Posts: 70
Joined: Sun May 01, 2016 7:08 pm
Contact:

Re: How to move item from inventory to cursor?

Post by Jorn86 »

Thanks! I needed to add a

Code: Select all

inventory[i].valid_for_read
check, but otherwise this works perfectly.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5207
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: How to move item from inventory to cursor?

Post 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.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Jorn86
Long Handed Inserter
Long Handed Inserter
Posts: 70
Joined: Sun May 01, 2016 7:08 pm
Contact:

Re: How to move item from inventory to cursor?

Post by Jorn86 »

That's a good point! Is there something I can do (with the LUA api) to prevent that?
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5207
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: How to move item from inventory to cursor?

Post 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...).
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Honktown
Smart Inserter
Smart Inserter
Posts: 1042
Joined: Thu Oct 03, 2019 7:10 am
Contact:

Re: How to move item from inventory to cursor?

Post by Honktown »

My first thought is throwing something on the ground if it can't fit in the inventory: luasurface->spill_item_stack
I have mods! I guess!
Link
robot256
Filter Inserter
Filter Inserter
Posts: 926
Joined: Sun Mar 17, 2019 1:52 am
Contact:

Re: How to move item from inventory to cursor?

Post 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!
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5207
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: How to move item from inventory to cursor?

Post by eradicator »

robot256 wrote: Wed Jan 22, 2020 3:26 pm
Off topic, so minimal answer: Capsule prototype supports "uses_stack = false" for infinite stacks. See Tapeline.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Honktown
Smart Inserter
Smart Inserter
Posts: 1042
Joined: Thu Oct 03, 2019 7:10 am
Contact:

Re: How to move item from inventory to cursor?

Post 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.
I have mods! I guess!
Link
robot256
Filter Inserter
Filter Inserter
Posts: 926
Joined: Sun Mar 17, 2019 1:52 am
Contact:

Re: How to move item from inventory to cursor?

Post 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.
Post Reply

Return to “Modding help”