Jump to content

Recommended Posts

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.

20181221201526_1.jpg

Edited by Skittish Robot
  • Developer

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?

?

  • Developer

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.

 

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
)

 

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...