Get the total count of Item prototypes

Place to get help with not working mods / modding interface.
Post Reply
User avatar
Gergely
Filter Inserter
Filter Inserter
Posts: 607
Joined: Sun Apr 10, 2016 8:31 pm
Contact:

Get the total count of Item prototypes

Post by Gergely »

I'm trying to achive that the player's main inventory can only hold a maximum of 1 stack of every item type, but in return, gets a size of the total number of Item types there are. (Both rules only apply to main inventory only, hotbar and logistic trash slots are unaffected.) I'm asking this, because the mod loading order messes things up.

Is there a way to count every Item prototype from every enabled mod, and set the inventory size to that value?

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

Re: Get the total count of Item prototypes

Post by eradicator »

If you count the number of keys in data.raw['item'] in data-final-fixes.lua you should get a mostly correct value for the number of item types. Be aware tho that:
  • even tho uncommon mods are technically allowed to add more items even in -final-fixes, which will mess up your count if they load after your mod
  • some mods add hidden items that are not actually optainable in-game (e.g. to fix blueprinting of invisible entities)
  • there is more than one "item" type prototype. Doing a quick search for 'type = "item-' in the base game files i can see at least these additional types: "item-with-entity-data", "item-with-tags", "item-with-label","item-with-inventory".
If you want to be accurate you could try counting the prototypes in control.lua, because at that time no additional prototypes can be added anymore. But then you'd have to try to adjust the inventory size via something like character_inventory_slots_bonus which isn't available to players without a character.

Having said all that there's a pretty similar mod which might or might not have code you can take inspiration from :P.
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
Klonan
Factorio Staff
Factorio Staff
Posts: 5156
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Get the total count of Item prototypes

Post by Klonan »

Gergely wrote:I'm trying to achive that the player's main inventory can only hold a maximum of 1 stack of every item type, but in return, gets a size of the total number of Item types there are. (Both rules only apply to main inventory only, hotbar and logistic trash slots are unaffected.) I'm asking this, because the mod loading order messes things up.

Is there a way to count every Item prototype from every enabled mod, and set the inventory size to that value?

Code: Select all

local count = 0
for name, prototype in pairs (game.item_prototypes) doesn
  count = count + 1
 end
game.print(count)
for k, force in pairs (game.forces) do
  force.character_inventory_slots_bonus = count -  60
end

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Get the total count of Item prototypes

Post by Nexela »

Code: Select all

for k, force in pairs (game.forces) do
  force.character_inventory_slots_bonus = #game.item_prototypes -  60
end

Bilka
Factorio Staff
Factorio Staff
Posts: 3228
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Get the total count of Item prototypes

Post by Bilka »

Code: Select all

for k, force in pairs (game.forces) do
  force.character_inventory_slots_bonus = table_size(game.item_prototypes) -  60
end
# isn't reliable, the table isn't indexed by numbers afaik. Table size does what klonan did, just on the c++ side of things. See https://reddit.com/r/factorio/comments/ ... ion_01524/ and the comments in that thread
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Get the total count of Item prototypes

Post by Nexela »

# is reliable if there is a __len metatable for it, which is the case for custom tables. And table_size won't work because of custom __len method

http://lua-api.factorio.com/latest/LuaCustomTable.html

Bilka
Factorio Staff
Factorio Staff
Posts: 3228
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Get the total count of Item prototypes

Post by Bilka »

Nexela wrote:# is reliable if there is a __len metatable for it, which is the case for custom tables. And table_size won't work because of custom __len method

http://lua-api.factorio.com/latest/LuaCustomTable.html
Thanks for the info
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2904
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Get the total count of Item prototypes

Post by darkfrei »

Gergely wrote:I'm trying to achive that the player's main inventory can only hold a maximum of 1 stack of every item type, but in return, gets a size of the total number of Item types there are. (Both rules only apply to main inventory only, hotbar and logistic trash slots are unaffected.) I'm asking this, because the mod loading order messes things up.

Is there a way to count every Item prototype from every enabled mod, and set the inventory size to that value?
See this code
https://mods.factorio.com/mods/darkfrei ... eInventory

User avatar
Gergely
Filter Inserter
Filter Inserter
Posts: 607
Joined: Sun Apr 10, 2016 8:31 pm
Contact:

Re: Get the total count of Item prototypes

Post by Gergely »

Klonan wrote:
Gergely wrote:I'm trying to achive that the player's main inventory can only hold a maximum of 1 stack of every item type, but in return, gets a size of the total number of Item types there are. (Both rules only apply to main inventory only, hotbar and logistic trash slots are unaffected.) I'm asking this, because the mod loading order messes things up.

Is there a way to count every Item prototype from every enabled mod, and set the inventory size to that value?

Code: Select all

local count = 0
for name, prototype in pairs (game.item_prototypes) doesn
  count = count + 1
 end
game.print(count)
for k, force in pairs (game.forces) do
  force.character_inventory_slots_bonus = count -  60
end
Wow... was the slots_bonus added in a new update? It seems I completely missed it.

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

Re: Get the total count of Item prototypes

Post by Rseding91 »

As stated before but just to confirm: # works for any of the game.*_prototypes to get how many prototypes there are.
If you want to get ahold of me I'm almost always on Discord.

Post Reply

Return to “Modding help”