Ability to create out-of-world LuaItemStacks.

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

Ability to create out-of-world LuaItemStacks.

Post 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 :).
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.

dewiniaid
Long Handed Inserter
Long Handed Inserter
Posts: 96
Joined: Tue Mar 07, 2017 8:50 pm
Contact:

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

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

User avatar
TelemakFactorio
Long Handed Inserter
Long Handed Inserter
Posts: 53
Joined: Fri Oct 14, 2016 4:30 pm
Contact:

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

Post 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 ?

dewiniaid
Long Handed Inserter
Long Handed Inserter
Posts: 96
Joined: Tue Mar 07, 2017 8:50 pm
Contact:

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

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

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

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

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

User avatar
TelemakFactorio
Long Handed Inserter
Long Handed Inserter
Posts: 53
Joined: Fri Oct 14, 2016 4:30 pm
Contact:

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

Post by TelemakFactorio »

Right ! An itemStack controller would be appreciated for sure.

User avatar
H8UL
Fast Inserter
Fast Inserter
Posts: 114
Joined: Mon May 15, 2017 4:02 pm
Contact:

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

Post 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
Shameless mod plugging: Ribbon Maze

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

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

Post 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 :).
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.

Boodals
Fast Inserter
Fast Inserter
Posts: 129
Joined: Sun Feb 11, 2018 7:10 pm
Contact:

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

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

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

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

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

Boodals
Fast Inserter
Fast Inserter
Posts: 129
Joined: Sun Feb 11, 2018 7:10 pm
Contact:

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

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

PyroFire
Filter Inserter
Filter Inserter
Posts: 356
Joined: Tue Mar 08, 2016 8:18 am
Contact:

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

Post by PyroFire »

What's wrong with just using a table, and re-applying the values you need on an as-needed basis?

Rseding91
Factorio Staff
Factorio Staff
Posts: 13198
Joined: Wed Jun 11, 2014 5:23 am
Contact:

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

Post 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.
If you want to get ahold of me I'm almost always on Discord.

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

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

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

Rseding91
Factorio Staff
Factorio Staff
Posts: 13198
Joined: Wed Jun 11, 2014 5:23 am
Contact:

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

Post 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 3920 times
If you want to get ahold of me I'm almost always on Discord.

Post Reply

Return to “Implemented mod requests”