View Single Post
08-13-17, 08:07 PM   #4
kurapica.igas
A Chromatic Dragonspawn
Join Date: Aug 2011
Posts: 152
If you using the environment control, you don't need to change the _G back at the end, and you won't write anything to the _G since your job is done in the private environment.

http://www.wowinterface.com/forums/s...5&postcount=11

With the environment control, we'll have more power with our codes, here is an example:

Lua Code:
  1. if val = ture then print("val is true") end

The "ture" is a spell error for 'true', and it'd be treated as nil, the code would run and we won't know the bug. With the environment control :

Lua Code:
  1. -- Modify the environment, using private table as environment
  2. setfenv(1, setmetatable( select(2, ...), { __index = function(self, key)
  3.     local val = _G[key]
  4.     if val ~= nil then rawset(self, key, val) else error(("Global variable %q can't be found"):format(key), 2) end
  5.     return val
  6. end } ))
  7.  
  8. local a = true
  9. if a == ture then print("Okay") end -- error Global variable "ture" can't be found

I also development plenty features based on it : http://www.wowinterface.com/forums/s...ad.php?t=55057
  Reply With Quote