zUsername Posted March 2, 2016 Share Posted March 2, 2016 My situation is: I want to override some function in scripts/aaaa.lua from the other mod. So is it possible to do that ? Link to comment https://forums.kleientertainment.com/forums/topic/65037-help-override-function-belong-to-other-mods/ Share on other sites More sharing options...
PanAzej Posted March 2, 2016 Share Posted March 2, 2016 (edited) 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 March 2, 2016 by PanAzej Link to comment https://forums.kleientertainment.com/forums/topic/65037-help-override-function-belong-to-other-mods/#findComment-729117 Share on other sites More sharing options...
zUsername Posted March 2, 2016 Author Share Posted March 2, 2016 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. Link to comment https://forums.kleientertainment.com/forums/topic/65037-help-override-function-belong-to-other-mods/#findComment-729121 Share on other sites More sharing options...
Muche Posted March 2, 2016 Share Posted March 2, 2016 (edited) 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 March 2, 2016 by Muche Link to comment https://forums.kleientertainment.com/forums/topic/65037-help-override-function-belong-to-other-mods/#findComment-729168 Share on other sites More sharing options...
zUsername Posted March 2, 2016 Author Share Posted March 2, 2016 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. Link to comment https://forums.kleientertainment.com/forums/topic/65037-help-override-function-belong-to-other-mods/#findComment-729172 Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now