How to most-UPS-friendly check how much of a stack would be insertable into an inventory

Place to get help with not working mods / modding interface.
Post Reply
User avatar
Oktokolo
Filter Inserter
Filter Inserter
Posts: 883
Joined: Wed Jul 12, 2017 5:45 pm
Contact:

How to most-UPS-friendly check how much of a stack would be insertable into an inventory

Post by Oktokolo »

I know about can_insert but it only tells me whether at least one item could be inserted. I need to know how much items of a stack could be inserted.
I don't actually want to insert the stack into the target inventory at the time i want to know how much could be inserted (need it for controlling the pickup of a scripted inserter that never gets clogged when used as only inserter feeding a target).

My ideas so far:
- Perform an insert, store the returned amount and immediately remove the inserterted items (insert and remove are probably heavily optimized, but does not feel right somehow).
- Iterate over the content of the target inventory and count for each stack how much of the potential stack could be added (sounds slow, but there is a high probability, that all the target inventories will only have 1 to 3 slots, so the O(N) looks a lot like O(1) to me).

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: How to most-UPS-friendly check how much of a stack would be insertable into an inventory

Post by DaveMcW »

That sounds like all your options, unless you want to make a modding interface request.

Benchmark them both using /measured-command and report the results here.

Honktown
Smart Inserter
Smart Inserter
Posts: 1026
Joined: Thu Oct 03, 2019 7:10 am
Contact:

Re: How to most-UPS-friendly check how much of a stack would be insertable into an inventory

Post by Honktown »

I'd use the stack size, and modulo how many are in the inventory, or find the last inventory space with that item in it and modulo that.

I was going to post a semi-related question, how do I FIND data during run-time, or save it during data-final-fixes? I need the stack size for something I'm trying to do, but data.raw is discarded, and game.data doesn't exist, and global.data is either being overwritten or doesn't exist in the first place for some changes I'm making to a mod.

I tried saving data to global during data-final-fixes, but it's not there during on_load.
I have mods! I guess!
Link

Honktown
Smart Inserter
Smart Inserter
Posts: 1026
Joined: Thu Oct 03, 2019 7:10 am
Contact:

Re: How to most-UPS-friendly check how much of a stack would be insertable into an inventory

Post by Honktown »

Forgot to mention, the mod I'm messing with DOES do exactly that, but it doesn't track stack difference. It inserts the item, and insert returns how many were inserted. For the mod, if it inserted too many, it spills out the rest onto the ground. It's easy enough to do inventory.remove instead of inventory.insert.
I have mods! I guess!
Link

Honktown
Smart Inserter
Smart Inserter
Posts: 1026
Joined: Thu Oct 03, 2019 7:10 am
Contact:

Re: How to most-UPS-friendly check how much of a stack would be insertable into an inventory

Post by Honktown »

Okay, I figured out my stupid issue. For whatever reason using serpent() on in-game tables tends to produce crap. Items are stored in game.item_prototypes, with the key being the item-name-key thing.

It doesn't store them like data.raw in different categories. All the things are in game.item_prototypes. I don't know if they're available during a 'global' phase like on_load, but it's easy enough to do local items = nil at the top of a file, and if items = nil then items = game.item_prototypes end in a function.

Edit: don't store the item table like that, it causes the data to be non-serializable and you can't save :/
I have mods! I guess!
Link

Post Reply

Return to “Modding help”