Page 1 of 1

Get the total count of Item prototypes

Posted: Sun Sep 10, 2017 7:46 pm
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?

Re: Get the total count of Item prototypes

Posted: Sun Sep 10, 2017 9:03 pm
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.

Re: Get the total count of Item prototypes

Posted: Sun Sep 10, 2017 9:12 pm
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

Re: Get the total count of Item prototypes

Posted: Sun Sep 10, 2017 9:34 pm
by Nexela

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

Posted: Sun Sep 10, 2017 10:18 pm
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

Re: Get the total count of Item prototypes

Posted: Mon Sep 11, 2017 7:41 am
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

Re: Get the total count of Item prototypes

Posted: Mon Sep 11, 2017 9:07 am
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

Re: Get the total count of Item prototypes

Posted: Mon Sep 11, 2017 10:32 am
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

Re: Get the total count of Item prototypes

Posted: Sun Nov 05, 2017 12:46 pm
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.

Re: Get the total count of Item prototypes

Posted: Sun Nov 05, 2017 1:56 pm
by Rseding91
As stated before but just to confirm: # works for any of the game.*_prototypes to get how many prototypes there are.