Custom recipe category is showing in player inventory

Place to get help with not working mods / modding interface.
Post Reply
MineGame159
Inserter
Inserter
Posts: 38
Joined: Sun Apr 16, 2017 1:54 pm
Contact:

Custom recipe category is showing in player inventory

Post by MineGame159 »

I created custom recipe category and then assigned it to my recipes and a custom assembling machine. My custom assembling machine are only showing recipes from this category and normal assembling machine is not showing my recipe category, this it good. But player inventory is showing recipes for my custom recipe category too and I dont want this. Can someone help me?

Sorry for my bad english, english is not my native language.

User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: Custom recipe category is showing in player inventory

Post by bobingabout »

This is normal. you'll probably notice things like oil processing and stuff made in chemical plants also appears on the crafting menu, even though they specifically need to be made in the oil refinery and chemical plant.
You won't be able to hand craft it, but if you point to it, a tooltip should pop up saying "Made in" and show an image of your new factory's icon.

It's your job to put it in an appropriate section of the player's crafting menu.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.

MineGame159
Inserter
Inserter
Posts: 38
Joined: Sun Apr 16, 2017 1:54 pm
Contact:

Re: Custom recipe category is showing in player inventory

Post by MineGame159 »

bobingabout wrote:This is normal. you'll probably notice things like oil processing and stuff made in chemical plants also appears on the crafting menu, even though they specifically need to be made in the oil refinery and chemical plant.
You won't be able to hand craft it, but if you point to it, a tooltip should pop up saying "Made in" and show an image of your new factory's icon.

It's your job to put it in an appropriate section of the player's crafting menu.
Oh yes thanks, but for later purposes can I add my own recipe category to player?

User avatar
mexmer
Filter Inserter
Filter Inserter
Posts: 869
Joined: Wed Aug 03, 2016 2:00 pm
Contact:

Re: Custom recipe category is showing in player inventory

Post by mexmer »

MineGame159 wrote:
bobingabout wrote:This is normal. you'll probably notice things like oil processing and stuff made in chemical plants also appears on the crafting menu, even though they specifically need to be made in the oil refinery and chemical plant.
You won't be able to hand craft it, but if you point to it, a tooltip should pop up saying "Made in" and show an image of your new factory's icon.

It's your job to put it in an appropriate section of the player's crafting menu.
Oh yes thanks, but for later purposes can I add my own recipe category to player?
yes you can, that's what most mods does

create subgroup (for example "my crafting recipes"), assign it icon, put all your recipes into that subgroup and they will appear in there
i also suggest to assign same subgroup to item protypes (eg. results of your recipes), that way players will have easier way finding your products in logistic signal menu.

IMO having items in same group on craft menu and signal menu is good.

MineGame159
Inserter
Inserter
Posts: 38
Joined: Sun Apr 16, 2017 1:54 pm
Contact:

Re: Custom recipe category is showing in player inventory

Post by MineGame159 »

mexmer wrote:
MineGame159 wrote:
bobingabout wrote:This is normal. you'll probably notice things like oil processing and stuff made in chemical plants also appears on the crafting menu, even though they specifically need to be made in the oil refinery and chemical plant.
You won't be able to hand craft it, but if you point to it, a tooltip should pop up saying "Made in" and show an image of your new factory's icon.

It's your job to put it in an appropriate section of the player's crafting menu.
Oh yes thanks, but for later purposes can I add my own recipe category to player?
yes you can, that's what most mods does

create subgroup (for example "my crafting recipes"), assign it icon, put all your recipes into that subgroup and they will appear in there
i also suggest to assign same subgroup to item protypes (eg. results of your recipes), that way players will have easier way finding your products in logistic signal menu.

IMO having items in same group on craft menu and signal menu is good.
Sorry I meant I have all my items and recipes in subgroup but when it have my own recipe category it cannot be crafted in hand.

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

Re: Custom recipe category is showing in player inventory

Post by eradicator »

MineGame159 wrote: Sorry I meant I have all my items and recipes in subgroup but when it have my own recipe category it cannot be crafted in hand.
table.insert(data.raw.player.player.crafting_categories,'your-crafting-category')

MineGame159
Inserter
Inserter
Posts: 38
Joined: Sun Apr 16, 2017 1:54 pm
Contact:

Re: Custom recipe category is showing in player inventory

Post by MineGame159 »

eradicator wrote:table.insert(data.raw.player.player.crafting_categories,'your-crafting-category')
Thanks

User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: Custom recipe category is showing in player inventory

Post by bobingabout »

eradicator wrote:
MineGame159 wrote: Sorry I meant I have all my items and recipes in subgroup but when it have my own recipe category it cannot be crafted in hand.
table.insert(data.raw.player.player.crafting_categories,'your-crafting-category')
although that works, it is incomplete, because it doesn't take into account player entities added by other mods (such as my bobclasses), or the god controller.

instead, you should iterate through all data.raw.player entities, and add your new category.

a function that does what you need that you can use is quoted below.

Code: Select all

function add_crafting_category_to_entity_type(entity_type, category)
  for i, entity in pairs(data.raw[entity_type]) do
    if not entity.crafting_categories then
      entity.crafting_categories = {category}
    else
      table.insert(entity.crafting_categories, category)
    end
  end
end
The reason to use a function, is because you'll be doing the same thing twice, for the player, and the god controller, so you could just type everything twice, or re-use the same code by making it a function.
You will need to call this function on entity types player, and god-controller, as follows:

Code: Select all

add_crafting_category_to_entity_type("player", "your-category")
add_crafting_category_to_entity_type("god-controller", "your-category")
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.

MineGame159
Inserter
Inserter
Posts: 38
Joined: Sun Apr 16, 2017 1:54 pm
Contact:

Re: Custom recipe category is showing in player inventory

Post by MineGame159 »

bobingabout wrote: although that works, it is incomplete, because it doesn't take into account player entities added by other mods (such as my bobclasses), or the god controller.

instead, you should iterate through all data.raw.player entities, and add your new category.

a function that does what you need that you can use is quoted below.

Code: Select all

function add_crafting_category_to_entity_type(entity_type, category)
  for i, entity in pairs(data.raw[entity_type]) do
    if not entity.crafting_categories then
      entity.crafting_categories = {category}
    else
      table.insert(entity.crafting_categories, category)
    end
  end
end
The reason to use a function, is because you'll be doing the same thing twice, for the player, and the god controller, so you could just type everything twice, or re-use the same code by making it a function.
You will need to call this function on entity types player, and god-controller, as follows:

Code: Select all

add_crafting_category_to_entity_type("player", "your-category")
add_crafting_category_to_entity_type("god-controller", "your-category")
Oh I didn't realize that, thanks for post.

User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: Custom recipe category is showing in player inventory

Post by bobingabout »

oh, yeah, you want to do this in the data-updates phase, because if you do it in data phase, depending on mod loading order, it might be before the other mods add their player entities.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.

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

Re: Custom recipe category is showing in player inventory

Post by eradicator »

bobingabout wrote:
eradicator wrote:
MineGame159 wrote: Sorry I meant I have all my items and recipes in subgroup but when it have my own recipe category it cannot be crafted in hand.
table.insert(data.raw.player.player.crafting_categories,'your-crafting-category')
although that works, it is incomplete, because it doesn't take into account player entities added by other mods (such as my bobclasses), or the god controller.

instead, you should iterate through all data.raw.player entities, and add your new category.
I'd be pretty furious if someone added crafting categories to my custom player instances. The whole point i use different player instances is to have control over which player type can craft what. Adding something to all of them completly breaks the concept. That's literally like iterating over data.raw['assembling-machine'] and adding crafting categories to everything just because you're trying to add something to assembling-machine-1/2/3. It is YOUR job as the modder who adds new player classes to make sure that those classes inherit the default player categories if you want that.

You're right about the god controller though, so i say:

Code: Select all

for _,tbl in pairs{data.raw.player.player, data.raw['god-controller'].default} do
  table.insert(tbl.crafting_categories,'your-crafting-category')
  end
Which has to be done in data.lua so the change is available to all mods that want to read it in -updates or -final-fixes.

User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: Custom recipe category is showing in player inventory

Post by bobingabout »

eradicator wrote:
bobingabout wrote:
eradicator wrote:
MineGame159 wrote: Sorry I meant I have all my items and recipes in subgroup but when it have my own recipe category it cannot be crafted in hand.
table.insert(data.raw.player.player.crafting_categories,'your-crafting-category')
although that works, it is incomplete, because it doesn't take into account player entities added by other mods (such as my bobclasses), or the god controller.

instead, you should iterate through all data.raw.player entities, and add your new category.
I'd be pretty furious if someone added crafting categories to my custom player instances. The whole point i use different player instances is to have control over which player type can craft what. Adding something to all of them completly breaks the concept. That's literally like iterating over data.raw['assembling-machine'] and adding crafting categories to everything just because you're trying to add something to assembling-machine-1/2/3. It is YOUR job as the modder who adds new player classes to make sure that those classes inherit the default player categories if you want that.

You're right about the god controller though, so i say:

Code: Select all

for _,tbl in pairs{data.raw.player.player, data.raw['god-controller'].default} do
  table.insert(tbl.crafting_categories,'your-crafting-category')
  end
Which has to be done in data.lua so the change is available to all mods that want to read it in -updates or -final-fixes.
Well, it does depend what the player entity is for. Mine are intended to be able to craft everything the normal player can.

A safer way to do it (Which is actually what I do in my mod) is to check what crafting categories currently exist on the entity before adding new ones, for example, make sure "crafting" exists on the list.

The problem with doing this is that the function is then pretty involved, and I thought it would be too big of a leap from a one liner adding it to the player.

if you're interested, this is the code in my mod, that will do it all correctly.
Don't just use it directly, at the very least change the function names to not include my bobmods table.

Code: Select all

function bobmods.lib.machine.has_category(machine, category_in)
  local hasit = false
  if machine and machine.crafting_categories then
    for i, category in pairs(machine.crafting_categories) do
      if category == category_in then
        hasit = true
      end
    end
  end
  return hasit
end

function bobmods.lib.machine.add_category(machine, category)
  if machine and data.raw["recipe-category"][category] then
    if not machine.crafting_categories then
      machine.crafting_categories = {category}
    elseif not bobmods.lib.machine.has_category(machine, category) then
      table.insert(machine.crafting_categories, category)
    end
  else
    if not data.raw["recipe-category"][category] then
      log("Crafting category " .. category .. " does not exist.")
    end
  end
end

function bobmods.lib.machine.if_add_category(machine, category, category_to_add)
  if machine and data.raw["recipe-category"][category] and data.raw["recipe-category"][category_to_add] then
    if bobmods.lib.machine.has_category(machine, category) then
      bobmods.lib.machine.add_category(machine, category_to_add)
    end
  else
    if not data.raw["recipe-category"][category] then
      log("Crafting category " .. category .. " does not exist.")
    end
    if not data.raw["recipe-category"][category_to_add] then
      log("Crafting category " .. category_to_add .. " does not exist.")
    end
  end
end

function bobmods.lib.machine.type_if_add_category(machine_type, category, category_to_add)
  if data.raw["recipe-category"][category] and data.raw["recipe-category"][category_to_add] then
    for i, machine in pairs(data.raw[machine_type]) do
      bobmods.lib.machine.if_add_category(machine, category, category_to_add)
    end
  else
    if not data.raw["recipe-category"][category] then
      log("Crafting category " .. category .. " does not exist.")
    end
    if not data.raw["recipe-category"][category_to_add] then
      log("Crafting category " .. category_to_add .. " does not exist.")
    end
  end
end

...

-- add Assembling Machine catagory.
bobmods.lib.machine.type_if_add_category("assembling-machine", "crafting", "crafting-machine")

-- add new electronics crafting categories
bobmods.lib.machine.type_if_add_category("player", "crafting", "electronics")
if data.raw["god-controller"] then
  bobmods.lib.machine.type_if_add_category("god-controller", "crafting", "electronics")
end
bobmods.lib.machine.type_if_add_category("assembling-machine", "crafting", "electronics")
bobmods.lib.machine.type_if_add_category("assembling-machine", "crafting", "electronics-machine")
Also, do you remember how I said only people who don't trust their code use a pcall()? Yeah, my code includes safety checks, and a log output if the check fails.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.

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

Re: Custom recipe category is showing in player inventory

Post by eradicator »

bobingabout wrote:Well, it does depend what the player entity is for. Mine are intended to be able to craft everything the normal player can.
No. The procedure doesn't depend on what you want. Because a mod that adds new categories can not possibly know what you want. On the other side it is trivial for the mod adding new player prototypes to also copy the crafting_categories table from the original player object if that is the desired behavior.

Also while i can see where you're coming from with the "add category B only if object also has category A" (much better than the brute force approach) there are still some edge cases where even that would be wrong (admittedly those are probably quite edgy indeed >_>).

Additionally i don't agree with your error handling at all. Technically it's not an error to add a crafting_category to a machine before the crafting_category itself has been defined. The problem with your code is thus that it takes a hard error (=game does not start because engine detects missing crafting_category (if it's actually missing at the end)) into a soft error (=game does start but some bits of code haven't been executed). So a previously immediately obvious error is now hidden in a log entry that doesn't even tell you what prototype caused the error.

Post Reply

Return to “Modding help”