Jump to content

[help]how can i understand this code!?


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!

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...