How do you hook in to functions?

IronsoulIronsoul Join Date: 2011-03-12 Member: 86048Members
Hello again, here I am seeking aid from people more knowledgable in the arts of coding than I.

This day, I'm trying to add some functionality to an existing function in the ns2 code. Specifically the ResourcePoint:Reset() function. Here's the code I tried:
Script.Load("lua/ResourcePoint.lua")

do
    local oldResourcePointReset = ResourcePoint:Reset()
    ResourcePoint:Reset = function()
        oldResourcePointReset()
        Print("Resource Point detected")
        //attempted hook in
    end
end

Obviously, this isn't working, meaning I've done something wrong.

It would be fantastic if someone would explain to me what I've done wrong, and optionally why what I tried was wrong.

All the best,

Ironsoul.

Comments

  • SamusDroidSamusDroid Colorado Join Date: 2013-05-13 Member: 185219Members, Forum Moderators, NS2 Developer, NS2 Playtester, Squad Five Gold, Subnautica Playtester, NS2 Community Developer, Pistachionauts
    I'd like to know how to do this too!
  • fsfodfsfod uk Join Date: 2004-04-09 Member: 27810Members, NS2 Developer, Constellation, NS2 Playtester, Squad Five Blue, Squad Five Silver, Squad Five Gold, Subnautica Playtester, NS2 Community Developer, Pistachionauts
    Ironsoul wrote: »
    Hello again, here I am seeking aid from people more knowledgable in the arts of coding than I.

    This day, I'm trying to add some functionality to an existing function in the ns2 code. Specifically the ResourcePoint:Reset() function. Here's the code I tried:
    Script.Load("lua/ResourcePoint.lua")
    
    do
        local oldResourcePointReset = ResourcePoint:Reset()
        ResourcePoint:Reset = function()
            oldResourcePointReset()
            Print("Resource Point detected")
            //attempted hook in
        end
    end
    

    Obviously, this isn't working, meaning I've done something wrong.

    It would be fantastic if someone would explain to me what I've done wrong, and optionally why what I tried was wrong.

    All the best,

    Ironsoul.

    Youre mixing up the self call syntax ":" with the normal "." style access
    local oldResourcePointReset = ResourcePoint:Reset() 
    
    should be
    local oldResourcePointReset = ResourcePoint.Reset
    
    and you need to capture and pass the self parameter to the original function since you're not defining the function with self call syntax which implicitly includes the self parameter
    ResourcePoint.Reset = function(self)
            oldResourcePointReset(self)
    

  • IronsoulIronsoul Join Date: 2011-03-12 Member: 86048Members
    Worked great, thanks @fsfod. No longer do i need to rip apart existing lua files.
  • MCMLXXXIVMCMLXXXIV Join Date: 2010-04-14 Member: 71400Members
    That way looks great and will work - if you're looking for an alternative here is the simplest example I can find from our new Combat/Xenoswarm codebase. I prefer a syntax which allows me to use the same syntax for the function definition as is used in the vanilla codebase

    In this example we hook into the Create function to add a new mixin to grenade launchers. We can also add new networkVars into the networkVars table at the top, and they will get added to the class:
    local networkVars = {
    }
    
    AddMixinNetworkVars(ClipSizeMixin, networkVars)
    
    local overrideOnCreate = GrenadeLauncher.OnCreate
    function GrenadeLauncher:OnCreate()
    
    	overrideOnCreate(self)
    	
    	local clipSizeParameters = { kBaseClipSize = kGrenadeLauncherClipSize,
    								 kClipSizeIncrease = 1, }
    	InitMixin(self, ClipSizeMixin, clipSizeParameters)
    	assert(HasMixin(self, "VariableClipSize"))	
    	
    end
    
    Class_Reload("GrenadeLauncher", networkVars)
    
  • IronsoulIronsoul Join Date: 2011-03-12 Member: 86048Members
    I didn't really understand that much @MCMLCXXXIV but in time I probably will. Thanks for giving me another path to explore later.
Sign In or Register to comment.