Jump to content

Recommended Posts

Hi, I'm having issues using a global function (it returns nil when I believe it shouldn't be).

In builder.lua, there is code that determines if the crafting player already has a pet: 

function Builder:DoBuild(recname, pt, rotation, skin)
    local recipe = GetValidRecipe(recname)
    if recipe ~= nil and (self:IsBuildBuffered(recname) or self:CanBuild(recname)) then
        if recipe.placer ~= nil and
            self.inst.components.rider ~= nil and
            self.inst.components.rider:IsRiding() then
            return false, "MOUNTED"
        elseif recipe.level.ORPHANAGE > 0 and (
                self.inst.components.petleash == nil or
                self.inst.components.petleash:IsFull() or
                self.inst.components.petleash:HasPetWithTag("critter")
            ) then
            return false, "HASPET"
        end  ...

 

I'm attempting to do something similar to this, but I wanted to avoid modifying builder.lua directly. So I'm using AddComponentPostInit to modify Builder:DoBuild. 

AddComponentPostInit("builder", function(self)
	local _DoBuild = self.DoBuild
	
	self.DoBuild = function(recname, ...)
		local recipe = GLOBAL.GetValidRecipe(recname) --This always seems to return nil, even though it works within builder.lua
		
		...
	
		return _DoBuild(recname, ...)
	end	
end)

 

What am I doing incorrectly with GetValidRecipe?

Thanks.

Link to comment
https://forums.kleientertainment.com/forums/topic/71945-usage-of-global-functions/
Share on other sites

49 minutes ago, Farxodor said:

Hi, I'm having issues using a global function (it returns nil when I believe it shouldn't be).

In builder.lua, there is code that determines if the crafting player already has a pet: 

 

I'm attempting to do something similar to this, but I wanted to avoid modifying builder.lua directly. So I'm using AddComponentPostInit to modify Builder:DoBuild. 

 

What am I doing incorrectly with GetValidRecipe?

Thanks.

AddComponentPostInit("builder", function(self)
	local _DoBuild = self.DoBuild
	
	self.DoBuild = function(self, recname, ...)
		local recipe = GLOBAL.GetValidRecipe(recname) --This always seems to return nil, even though it works within builder.lua
		
		...
	
		return _DoBuild(self, recname, ...)
	end	
end)

In LUA the object:funcname(arg1) is shorthand for object.funcname(object, arg1)

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