Get the total count of Item prototypes
Get the total count of Item prototypes
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?
Is there a way to count every Item prototype from every enabled mod, and set the inventory size to that value?
- eradicator
- Smart Inserter
- Posts: 5206
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: Get the total count of Item prototypes
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:
Having said all that there's a pretty similar mod which might or might not have code you can take inspiration from .
- 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".
Having said all that there's a pretty similar mod which might or might not have code you can take inspiration from .
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.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Re: Get the total count of Item prototypes
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
Re: Get the total count of Item prototypes
Code: Select all
for k, force in pairs (game.forces) do
force.character_inventory_slots_bonus = #game.item_prototypes - 60
end
Re: Get the total count of Item prototypes
Code: Select all
for k, force in pairs (game.forces) do
force.character_inventory_slots_bonus = table_size(game.item_prototypes) - 60
end
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
Re: Get the total count of Item prototypes
# 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
http://lua-api.factorio.com/latest/LuaCustomTable.html
Re: Get the total count of Item prototypes
Thanks for the infoNexela 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
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
Re: Get the total count of Item prototypes
See this codeGergely 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?
https://mods.factorio.com/mods/darkfrei ... eInventory
Re: Get the total count of Item prototypes
Wow... was the slots_bonus added in a new update? It seems I completely missed it.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
Re: Get the total count of Item prototypes
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.