Jump to content

Recommended Posts

below code block from [Health Info Plus]

//======================================

local function _Inject(comp,fn_name,fn)

    local old = comp[fn_name]

        comp[fn_name] = function(self,...)
        local res = old(self,...)    
        return fn(res,self,...)
    end
end
 


if TheNet == nil then
    _Inject(GLOBAL.EntityScript,"GetDisplayName",function(name,self)

......
        return name
    end)

    return 
end

//==========

how does the Inject work? why this code  can repeat call EntityScript:GetDisplayName() function?

is the comp[fn_name] = function(self,...) overwrite the game lua code?

hope someone can help me ! thx!!!!!!!!!!!!

 

ps:sorry aboat my poor english!

As I understand it, the function "_Inject" takes 3 parameters: the component to modify, a function name which will be associated with and the function to associate. It then writes the function to Component[function_name]. This works because objects in Lua are in fact tables, meaning that even the functions that comprise them are simply entries in a table. In the code you posted, the _Inject function is overwriting the "GetDisplayName" function which is originally defined in scripts/entityscript.lua. If you were to replace the variables, it looks like this for that single execution:

local function _Inject(comp, fn_name, fn)
    local old = GLOBAL.EntityScript["GetDisplayName"]
    GLOBAL.EntityScript["GetDisplayName"] = function(self, ...)
        local res = old(self, ...)    
        return fn(res, self, ...)
    end
end

Where "fn" is defined in the call to the function:

_Inject(GLOBAL.EntityScript, "GetDisplayName", function(name, self) -- Specifically, this function defined right here
    ...
    return name
 end)

 

Edited by Yabumi

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