Jump to content

Recommended Posts

I guess you'd have to set priority of your mod to lower than the mod you wanna edit and check if said mod is enabled, and then make a postinit function.

Edit: Not sure about the priority part, because it's different for some things and I don't know the sequence in which game loads them. You gotta check - if it won't work with lower priority, set it to higher and it should work, probably.

Edited by PanAzej
16 minutes ago, PanAzej said:

I guess you'd have to set priority of your mod to lower than the mod you wanna edit and check if said mod is enabled, and then make a postinit function.

Edit: Not sure about the priority part, because it's different for some things and I don't know the sequence in which game loads them. You gotta check - if it won't work with lower priority, set it to higher and it should work, probably.

I've tried set priority but not work. It's crash with cooking_replica nil value or cooking_replica not found error. Here is my code in modmain.lua:

local cooking = require "cooking"
local cooking_replica = require "cooking_replica"
local old_GetCandidateRecipes = cooking_replica.GetCandidateRecipes
cooking_replica.GetCandidateRecipes = function(cooker, ingdata)
   if cooker ~= nil and cooker.prefab ~= nil and cooker.prefab == "portablecookpot" then
      return cooking:GetCandidateRecipes(cooker.prefab, ingdata)
   end
   return old_GetCandidateRecipes(cooker, ingdata)
end

And the mod I want to override is cooking_replica.lua in Smarter Crock Pot mod.

Thanks you.

I'll assume you have the code above inside if SmartCrockPot then ... end.

The code above would have worked if cooking_replica was creating a class and returning it. But it does not. It overwrites previous global GetCandidateRecipes defined in cooking.lua. The only temporary solution I see is to overwrite the function again.

I haven't looked into SmartCrockPot that deeply, so I have no idea why its version of GetCandidateRecipes function need to ignore the cooker parameter and use hardcoded cooker value instead. The only place that seems to come into effect is in cooker_replica.lua|PredictMany or PredictRecipes, where it confuses cooker instance with cooker prefab name.

Anyways, try

local old_GetCandidateRecipes = GLOBAL.GetCandidateRecipes
local cooking_replica = require "cooking_replica"
GLOBAL.GetCandidateRecipes = old_GetCandidateRecipes
function GLOBAL.PredictRecipes(cooker, names)		
    local ingdata = GLOBAL.GetIngredientValues(names)
    local candidates = GLOBAL.GetCandidateRecipes(cooker.prefab, ingdata)
    table.sort(candidates, function(a,b) return (a.weight or 1) > (b.weight or 1) end)
    return candidates
end

Note that this assumes cooking_replica is not loaded yet at the time of execution. Usually it is loaded when components/predicter loads, which is loaded with first AddComponent("predicter") encountered, which in turn is executed with first creation of a cooker prefab (i.e. on first build of cooker on clean world or loading cooker as a part of the existing world). If there is anothed mod with higher priorty that loads cooking_replica, this trick would not work.

Edited by Muche
6 minutes ago, Muche said:

I'll assume you have the code above inside if SmartCrockPot then ... end.

The code above would have worked if cooking_replica was creating a class and returning it. But it does not. It overwrites previous global GetCandidateRecipes defined in cooking.lua. The only temporary solution I see is to overwrite the function again.

I haven't looked into SmartCrockPot that deeply, so I have no idea why its version of GetCandidateRecipes function need to ignore the cooker parameter and use hardcoded cooker value instead. The only place that seems to come into effect is in cooker_replica.lua|PredictMany or PredictRecipes, where it confuses cooker instance with cooker prefab name.

Anyways, try


local old_GetCandidateRecipes = GLOBAL.GetCandidateRecipes
local cooking_replica = require "cooking_replica"
GLOBAL.GetCandidateRecipes = old_GetCandidateRecipes
function GLOBAL.PredictRecipes(cooker,names)		
    local ingdata = GLOBAL.GetIngredientValues(names)
    local candidates = GLOBAL.GetCandidateRecipes(cooker.prefab, ingdata)
    table.sort( candidates, function(a,b) return (a.weight or 1) > (b.weight or 1) end )
    return candidates
end

Note that this assumes cooking_replica is not loaded yet at the time of execution. Usually it is loaded when components/predicter loads, which is loaded with first AddComponent("predicter") encountered, which in turn is executed with first creation of a cooker prefab (i.e. on first build of cooker on clean world or loading cooker as a part of the existing world). If there is anothed mod with higher priorty that loads cooking_replica, this trick would not work.

It's work. Many many many thanks you @Muche. You are my hero.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...