Page 1 of 1

Ability to create out-of-world LuaItemStacks.

Posted: Tue Oct 09, 2018 9:19 am
by eradicator
What?
The ability to create / store a LuaItemStack via script without the stack being associated to an entity on the map.

Why?
For mods it would often be benefitial to be able to store a non-simple ItemStack (i.e. armor-with-grid, item-with-tags, item-with-inventory, etc) without having to worry about the stack becoming invalid from outside influence. The current "hack" i have to use for this is an invisible, indestructible chest, but as that has to exist somewhere on the map it's not safe from other mods that e.g. delete "unused" chunks.
Other examples are building a blueprint from string, which currently requires an itemstack to get access to the blueprint methods, or managing a temporary bonus inventory.

How?
Stacks might be created via game.create_stack() and stored for as long as the lua state holds a reference to the stack - as obviously mods can't be trusted to properly call .destroy() on things they don't use anymore. Obviously stacks would have to be storable in global.

Again, i realize this would probably be a lot of work to implement, so if it's unfeasible i'd still be happy just knowing why, or how good of an example i'd have to come up with to make it feasible :).

Re: Ability to create out-of-world LuaItemStacks.

Posted: Mon Oct 15, 2018 5:08 am
by dewiniaid
I'd like to vote for this as a request too -- some sort of item storage for mods.

The current pre-release version of Blueprint Extensions works around this for now with some trickery: See Util.get_dummy_surface(), Util.store_item() and Util.clear_item() in the 0.2.2 source code. That said, this is entirely a hack and it'd be great if it were unnecessary.

Re: Ability to create out-of-world LuaItemStacks.

Posted: Mon Oct 29, 2018 10:40 pm
by TelemakFactorio
An itemstack is basically an array of size 2 containing a prototype name and a quantity.
So that you can create itemstacks everywhere anytime.
Maybe I have not understood the request ?

Re: Ability to create out-of-world LuaItemStacks.

Posted: Mon Oct 29, 2018 10:45 pm
by dewiniaid
TelemakFactorio wrote: Mon Oct 29, 2018 10:40 pm An itemstack is basically an array of size 2 containing a prototype name and a quantity.
So that you can create itemstacks everywhere anytime.
Maybe I have not understood the request ?
This alone is not sufficient for a lot of cases, like if the item in question is a blueprint (book).

Re: Ability to create out-of-world LuaItemStacks.

Posted: Tue Oct 30, 2018 11:41 am
by eradicator
TelemakFactorio wrote: Mon Oct 29, 2018 10:40 pm An itemstack is basically an array of size 2 containing a prototype name and a quantity.
So that you can create itemstacks everywhere anytime.
Maybe I have not understood the request ?
See the Api doc for a full list of item stack properties. Ammo, durability, blueprints, armor grids, etcpp. There are quite a few things and it would be nice if mods weren't forced to use ugly hacks or build a script only item decoder...

Re: Ability to create out-of-world LuaItemStacks.

Posted: Wed Oct 31, 2018 5:34 pm
by TelemakFactorio
Right ! An itemStack controller would be appreciated for sure.

Re: Ability to create out-of-world LuaItemStacks.

Posted: Thu Nov 22, 2018 9:08 am
by H8UL
Perhaps this is already possible? An item with inventory that has the hidden flag? That would avoid the invisible on map hack and its problems.

Edit: on second thoughts then you have the problem that it ties to a player

Re: Ability to create out-of-world LuaItemStacks.

Posted: Thu Nov 22, 2018 11:49 am
by eradicator
H8UL wrote: Thu Nov 22, 2018 9:08 am Perhaps this is already possible? An item with inventory that has the hidden flag? That would avoid the invisible on map hack and its problems.
Any stack that has to "exist" in the "world" somehow has some sort of problem. That's why i wrote this request for lua-state stack support :).

Re: Ability to create out-of-world LuaItemStacks.

Posted: Tue Apr 14, 2020 11:23 am
by Boodals
One of my oldest pull requests just got accepted which allows this. It should be in the next version. Main additions are: LuaGameScript::create_inventory, LuaInventory::resize, LuaInventory::destroy. Obviously once you've created an inventory you can use its slots as you wish. You can't destroy or resize inventories other than those created from create_inventory.

Re: Ability to create out-of-world LuaItemStacks.

Posted: Tue Apr 14, 2020 9:33 pm
by eradicator
Boodals wrote: Tue Apr 14, 2020 11:23 am One of my oldest pull requests just got accepted which allows this. It should be in the next version. Main additions are: LuaGameScript::create_inventory, LuaInventory::resize, LuaInventory::destroy. Obviously once you've created an inventory you can use its slots as you wish. You can't destroy or resize inventories other than those created from create_inventory.
That sounds awesome. Just out of curiosity, *how* old was the pull? Also i'm guessing that if i "lose" the reference to such an inventory it's auto-destroyed? And all inventories are "private" (i.e. other mods can't mess them up)?

+ Thanks!!

Re: Ability to create out-of-world LuaItemStacks.

Posted: Wed Apr 15, 2020 12:41 pm
by Boodals
eradicator wrote: Tue Apr 14, 2020 9:33 pm That sounds awesome. Just out of curiosity, *how* old was the pull? Also i'm guessing that if i "lose" the reference to such an inventory it's auto-destroyed? And all inventories are "private" (i.e. other mods can't mess them up)?
I made the pull request on 16th Feb 2019, hoping it would make it into 0.17.
If you lose the reference, it isn't destroyed. I tried to make that work, but there was no way to do it that wouldn't cause desyncs (I think because garbage collection is not deterministic). Rseding added game.get_script_inventories, similar to rendering.get_all_ids, so they are not lost forever, but the potential to "lose" references is the main reason it wasn't accepted for so long.
Because of game.get_script_inventories, they are not strictly private, but i'd be surprised if a mod blindly interacted with any script inventories, similar to renderings.

Re: Ability to create out-of-world LuaItemStacks.

Posted: Wed Apr 15, 2020 1:02 pm
by PyroFire
What's wrong with just using a table, and re-applying the values you need on an as-needed basis?

Re: Ability to create out-of-world LuaItemStacks.

Posted: Wed Apr 15, 2020 10:46 pm
by Rseding91
PyroFire wrote: Wed Apr 15, 2020 1:02 pm What's wrong with just using a table, and re-applying the values you need on an as-needed basis?
You lose all the meta information about the item. Equipment in armor, blueprints in blueprint books, entities in blueprints, health, durability. Items in items-with-inventory and so on.

Re: Ability to create out-of-world LuaItemStacks.

Posted: Thu Apr 16, 2020 10:54 pm
by eradicator
Boodals wrote: Wed Apr 15, 2020 12:41 pm but i'd be surprised if a mod blindly interacted with any script inventories, similar to renderings.
"rendering.clear()" is pretty awesome destructive and i've heared of both accidential and deliberate usage of that. Though kinda off-topic, and with only "get them all" for inventories at least the accidential part can't (shouln't...) happen. I'm curious though, how would one use get_script_inventories without a method to identify which inventory belongs to "me" and which doesn't? Assuming they're just standard inventories without a name or UID or anything?
PyroFire wrote: Wed Apr 15, 2020 1:02 pm What's wrong with just using a table, and re-applying the values you need on an as-needed basis?
I pondered the idea but was never desperate enough to do it. The sheer amount of code to completely reimplement all possible item properties of every item type is daring enough. And to top it off there's read-only stuff like item_number so the thing would be forever stuck being only 95% correct.

Re: Ability to create out-of-world LuaItemStacks.

Posted: Fri Apr 17, 2020 6:10 am
by Rseding91
eradicator wrote: Thu Apr 16, 2020 10:54 pm I'm curious though, how would one use get_script_inventories without a method to identify which inventory belongs to "me" and which doesn't? Assuming they're just standard inventories without a name or UID or anything?
Capture.PNG
Capture.PNG (16.1 KiB) Viewed 4750 times