View Single Post
01-16-18, 06:52 PM   #65
Banknorris
A Chromatic Dragonspawn
 
Banknorris's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 153
Originally Posted by Resike View Post
Except you are not hooking a function call, but a call for it's memory pointer's reference.

You can try it yourself:

Lua Code:
  1. hooksecurefunc("SetCVar", function(name, value)
  2.     if name == "Sound_EnableMusic" then
  3.         print(name, value)
  4.     end
  5. end)
  6.  
  7. local SetCVar = SetCVar
  8.  
  9. SetCVar("Sound_EnableMusic", 1)

You can do this in the other way around, it won't change a thing:

Lua Code:
  1. local SetCVar = SetCVar
  2.  
  3. hooksecurefunc("SetCVar", function(name, value)
  4.     if name == "Sound_EnableMusic" then
  5.         print(name, value)
  6.     end
  7. end)
  8.  
  9. _G.SetCVar("Sound_EnableMusic", 1)
What would happen if in the last line of the second code you have used SetCVar("Sound_EnableMusic", 1) instead of _G.SetCVar("Sound_EnableMusic", 1)? Would it still print anything? If not then that is exactly the point: calling a local reference to a global function when the attribution of the local is made BEFORE you hook the global function will result in the hook (by that I mean the function with print(name,value)) not being called. But of course the hooked function (SetCVar) will run normally.

That was exactly what Semlar said. If in my addon I don't call _G.SetCVar but a local version of it and your addon (which wants to track SetCVar calls by hooksecurefunc'ing it) loads after mine, then you will not be able to see my SetCVar calls. Hence the advice to use _G.SetCVar (implicitally, by just not defining a local version of it) that is just a little bit slower but don't interfere with hooks.
__________________
"In this world nothing can be said to be certain, except that fractional reserve banking is a Ponzi scheme and that you won't believe it." - Mandrill

Last edited by Banknorris : 01-17-18 at 03:06 PM.
  Reply With Quote