566895_1452788811 Posted July 24, 2017 Share Posted July 24, 2017 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 More sharing options...
566895_1452788811 Posted July 24, 2017 Author Share Posted July 24, 2017 this is the repeat call. if my cursor on the bird or other moster, the GetDisplayName start repeat call. Link to comment Share on other sites More sharing options...
Yabumi Posted July 29, 2017 Share Posted July 29, 2017 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 More sharing options...
Recommended Posts
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.