Placing items from the player's inventory

Place to post guides, observations, things related to modding that are not mods themselves.
Post Reply
neoc
Long Handed Inserter
Long Handed Inserter
Posts: 83
Joined: Sun Apr 24, 2016 4:31 pm
Contact:

Placing items from the player's inventory

Post by neoc »

Since a LuaItemStack can have a .health < 1 for when you have a stack of damaged items, when placing an item from the player's inventory you can't just use LuaInventory.remove, because that item may be damaged, which should be reflected in the entity placed (by the way, having two ways of handling health, one being the percentage factor between 0 and 1, one being the hitpoints, is confusing).

So you have to remove the item from the specific LuaItemStack it is in, and I'm wondering what the 'standard' method of removing an item from it is (there is no .remove method of any kind in the latest API).

(For the reciprocal task of removing a placed entity and placing it in the player's inventory, there is LuaControl.mine_entity; but there doesn't seem to be an equally easy way to place instead of remove, so things are more complicated.)

1)

It seems that the only direct way of getting an item stack is to call LuaInventory.find_item_stack, which will only find the first stack in that inventory. Other than that you can only enumerate every single position of the inventory via .operator[], ignoring stacks of items you're not looking for. Is this correct?

2)

Since there is no .remove method on a LuaItemStack, the only way to lower the count of items in it seems to be to lower the .count by one (it is RW). This is a bit awkward, and the documentation doesn't have an example on this. Is this the right way to do it or am I overlooking something?

Any suggestion about the way I'm doing things while using the API is appreciated, I'm not very experienced with it.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Placing items from the player's inventory

Post by eradicator »

First: The better question is usually to explain your problem, rather than explaining the problems with what you think is the solution.

Yes, decreasing the count is the expected way to reduce stack size. I'm a bit confused why you're trying to build from a given stack without having a stack though (you state that you need to iterate the whole inventory, without explaining why find_item_stack doesn't work for you).

As an "easy" approach it might be feasible to:
1) Get the source item stack
2) Use LuaItemStack.swap_stack to swap the source stack to the players cursor (if cursor is non-empty)
3) Use LuaPlayer.build_from_cursor to build the entity (this should handle health/count/events)
4) Swap the original cursor_stack back.
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.

neoc
Long Handed Inserter
Long Handed Inserter
Posts: 83
Joined: Sun Apr 24, 2016 4:31 pm
Contact:

Re: Placing items from the player's inventory

Post by neoc »

eradicator wrote:
Wed Sep 26, 2018 8:19 am
First: The better question is usually to explain your problem, rather than explaining the problems with what you think is the solution.

Yes, decreasing the count is the expected way to reduce stack size. I'm a bit confused why you're trying to build from a given stack without having a stack though (you state that you need to iterate the whole inventory, without explaining why find_item_stack doesn't work for you).
Well, I tried to be explicit about that, and I thought my explanation about why I have these questions is comprehensible.

The reason why it matters which item stack you build from is that you might want to build from a stack with .health = 1. Factorio separates damaged entities from undamaged ones when you mine them (most common example are turrets). When you place entities and have to make a decision, you might want to prefer undamaged entities. In that case you can't use LuaInventory.find_item_stack, because those entities might be in another stack.
As an "easy" approach it might be feasible to:
1) Get the source item stack
2) Use LuaItemStack.swap_stack to swap the source stack to the players cursor (if cursor is non-empty)
3) Use LuaPlayer.build_from_cursor to build the entity (this should handle health/count/events)
4) Swap the original cursor_stack back.
Is this a 'best practice'? I can't use it in my case, because I'm not actually placing items, but reviving ghosts (from the player's inventory, considering .health), but it's good to know.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Placing items from the player's inventory

Post by eradicator »

neoc wrote:
Wed Sep 26, 2018 8:38 am
Well, I tried to be explicit about that, and I thought my explanation about why I have these questions is comprehensible.
And i thought i was pretty obvious about telling you that you didn't (And still haven't). Now that you mention ghosts i can do a wild guess that you might be trying to do some sort of automatic ghost reviving while prefering to use non-damaged items first...? In that case iterating the inventory is your only option, though you can at least stop when you find the first non-damaged stack.
neoc wrote:
Wed Sep 26, 2018 8:38 am
Is this a 'best practice'? I can't use it in my case, because I'm not actually placing items, but reviving ghosts (from the player's inventory, considering .health), but it's good to know.
No, that's a quick hack i came up with after reading your post, which is hopefully less error prone than re-implement all the details manually.
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.

neoc
Long Handed Inserter
Long Handed Inserter
Posts: 83
Joined: Sun Apr 24, 2016 4:31 pm
Contact:

Re: Placing items from the player's inventory

Post by neoc »

eradicator wrote:
Wed Sep 26, 2018 8:57 am
neoc wrote:
Wed Sep 26, 2018 8:38 am
Well, I tried to be explicit about that, and I thought my explanation about why I have these questions is comprehensible.
And i thought i was pretty obvious about telling you that you didn't (And still haven't).
Might also be you failing to understand comprehensive explanations.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Placing items from the player's inventory

Post by eradicator »

neoc wrote:
Wed Sep 26, 2018 9:03 am
eradicator wrote:
Wed Sep 26, 2018 8:57 am
neoc wrote:
Wed Sep 26, 2018 8:38 am
Well, I tried to be explicit about that, and I thought my explanation about why I have these questions is comprehensible.
And i thought i was pretty obvious about telling you that you didn't (And still haven't).
Might also be you failing to understand comprehensive explanations.
Have fun modding. Maybe someone else can help you. Bye.
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.

neoc
Long Handed Inserter
Long Handed Inserter
Posts: 83
Joined: Sun Apr 24, 2016 4:31 pm
Contact:

Re: Placing items from the player's inventory

Post by neoc »

eradicator wrote:
Wed Sep 26, 2018 9:49 am
neoc wrote:
Wed Sep 26, 2018 9:03 am
eradicator wrote:
Wed Sep 26, 2018 8:57 am
neoc wrote:
Wed Sep 26, 2018 8:38 am
Well, I tried to be explicit about that, and I thought my explanation about why I have these questions is comprehensible.
And i thought i was pretty obvious about telling you that you didn't (And still haven't).
Might also be you failing to understand comprehensive explanations.
Have fun modding. Maybe someone else can help you. Bye.
I was already out. I wasn't aware of bitching being a problem in these forums.

Post Reply

Return to “Modding discussion”