Skittish Robot Posted December 22, 2018 Share Posted December 22, 2018 (edited) I would like to replace a function in playerhud.lua and by using this as a template, I came up with the following to put in modmain.lua: require = GLOBAL.require local GetPlayer = GLOBAL.GetPlayer local PlayerHud = require "screens/playerhud" local BetterUpdateLeaves = PlayerHud.UpdateLeaves function PlayerHud:UpdateLeaves(dt) local NewFunction = BetterUpdateLeaves(self, dt) old code new code old code return NewFunction end But things get a little glitchy. The background for the inventory stays on screen, and there is a "Buy Now" button at the top of the screen, etc. Even just putting the original code in gives the same result. Any help would be appreciated. Edited December 22, 2018 by Skittish Robot Link to comment https://forums.kleientertainment.com/forums/topic/100939-need-help-with-replacing-functions/ Share on other sites More sharing options...
Developer bizziboi Posted December 22, 2018 Developer Share Posted December 22, 2018 While I have no idea what is going wrong, couldn't you just say PlayerHud.UpdateLeaves = function(dt) -- whatever end But if anything, if you replace the function as in your code, you'd want it to call BetterUpdateLeaves, not return it: function PlayerHud:UpdateLeaves(dt) local NewFunction = BetterUpdateLeaves(self, dt) old code new code old code NewFunction() end Maybe that improves things? ? Link to comment https://forums.kleientertainment.com/forums/topic/100939-need-help-with-replacing-functions/#findComment-1135280 Share on other sites More sharing options...
Skittish Robot Posted December 22, 2018 Author Share Posted December 22, 2018 Sorry, neither of those options are working for me. If I edit the game files directly, my code works fine. I just can't seem to find a way to modify it otherwise. Link to comment https://forums.kleientertainment.com/forums/topic/100939-need-help-with-replacing-functions/#findComment-1135305 Share on other sites More sharing options...
Developer bizziboi Posted December 22, 2018 Developer Share Posted December 22, 2018 1: Open your debug log (CTRL-L) I suspect your hud is throwing errors, but it doesn't stop the game (UI is running in a different update than the game), or check the log.txt file to see stacktraces. 2: You probably need to add more "GLOBAL" references, like GetWorld() which the body of the function uses. 3: The code I typed there was reallllllly wrong. Guess it's a good thing it's Friday. Try: require = GLOBAL.require local GetPlayer = GLOBAL.GetPlayer local GetWorld = GLOBAL.GetWorld local GetClock = GLOBAL.GetClock local IS_CANOPY_TILE = GLOBAL.IS_CANOPY_TILE local PlayerHud = require "screens/playerhud" function PlayerHud:UpdateLeaves(dt) local function BetterUpdateLeaves(self, dt) -- whatever you want end BetterUpdateLeaves(self,dt) end Although you really don't need that local function definition, you can just put the body of the local function as the function body: require = GLOBAL.require local GetPlayer = GLOBAL.GetPlayer local GetWorld = GLOBAL.GetWorld local GetClock = GLOBAL.GetClock local IS_CANOPY_TILE = GLOBAL.IS_CANOPY_TILE local PlayerHud = require "screens/playerhud" function PlayerHud:UpdateLeaves(dt) -- whatever you want end Hope that helps. Link to comment https://forums.kleientertainment.com/forums/topic/100939-need-help-with-replacing-functions/#findComment-1135312 Share on other sites More sharing options...
CarlZalph Posted December 22, 2018 Share Posted December 22, 2018 From my mod here: https://steamcommunity.com/sharedfiles/filedetails/?id=368664169 A different method on doing this by using a post construct. Slightly edited to make it more clear: AddClassPostConstruct( "screens/playerhud", function(self, owner) if GLOBAL.IsDLCEnabled(GLOBAL.PORKLAND_DLC) then if self.UpdateLeaves -- Check just in case this function name changes, I hate crashes from mods. then local UpdateLeaves_old = self.UpdateLeaves self.UpdateLeaves = function(self, ...) UpdateLeaves_old(self, ...) -- For dynamic music handling. if self.leavesTop then self.leavesTop:Hide() end end end end end ) Link to comment https://forums.kleientertainment.com/forums/topic/100939-need-help-with-replacing-functions/#findComment-1135409 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