of     1   

warspyking
#140624771Thursday, July 17, 2014 1:36 PM GMT

I might not get many responses since this is on Lua 5.2 not Rblx.Lua, but I'm coming here anyway. Here's my script; Env = {} setmetatable(_G, { __newindex = function(tab, var, val) rawset(Env, var, nil) rawset(_G, var, nil) end, __index = function(tab, var) return rawget(Env, var) end }) x = 5 print(x) For those of you, who would be able to help if this was Rblx.Lua, the only difference here is how _G works; "Lua keeps all its global variables in a regular table, called the environment. (To be more precise, Lua keeps its "global" variables in several environments, but we will ignore this multiplicity for a while.) One advantage of this structure is that it simplifies the internal implementation of Lua, because there is no need for a different data structure for global variables. The other (actually the main) advantage is that we can manipulate this table as any other table. To facilitate such manipulations, Lua stores the environment itself in a global variable _G. (Yes, _G._G is equal to _G.) For instance, the following code prints the names of all global variables defined in the current environment: for n in pairs(_G) do print(n) end In this chapter, we will see several useful techniques to manipulate the environment." _G is basically a dictionary full of every global. Anyway, when I run the code, it'd print nil. I want it to print 5.
warspyking
#140626093Thursday, July 17, 2014 2:02 PM GMT

Nobody can help..?
MrNil
#140626143Thursday, July 17, 2014 2:03 PM GMT

wen you are telling the computer "rawset" your telling it to make the variable change. not to instanciate it. How i do this... like... lets say: table={x=1,y=2,z=3} instead of saying: rawset(table,w,4) --Instancing 'w' into the table we have to change an EXISTING value. essentially whats going on with your thing is when you declare any variables, you are essentially invoking rawset to give you nil every time because your not changing existing 'indexes' in the _G environment. saying this instead: rawset(table,x,0) would be the correct use.
warspyking
#140626579Thursday, July 17, 2014 2:12 PM GMT

Never mind, I found the problem; rawset(Env, var, nil) --Not val, oops rawset(_G, var, nil) --Copied I copy pasted the rawset on _G and replaced _G with Env, forgetting to change nil to val Thanks anyway
warspyking
#140691541Friday, July 18, 2014 1:55 AM GMT

New problem; History = { } local function Detect(T) return setmetatable({}, { __newindex = function(t, var, val) rawset(History, #History + 1, "Global variable " .. var .. " was set to " .. val) rawset(T, var, val) end, __index = function(t, var) rawset(History, #History + 1, "Global variable " .. var .. " was indexed.") return rawget(T, var) end }) end Old_G = _G _G = Detect(Old_G) x = 5 print(x) print(#History) print(History[1]) print(History[2]) Output; 5 0 nil nil
warspyking
#140719196Friday, July 18, 2014 7:16 AM GMT

Anybody?
cntkillme
#140728240Friday, July 18, 2014 11:19 AM GMT

The output is correct. #table doesn't include non-numerical keys and History[1] and History[2] are nil, but History["x"] should print out what x equals.\ print(x) print(#History) print(History[1]) print(History[2])
cntkillme
#140728439Friday, July 18, 2014 11:24 AM GMT

Never mind didn't see the History variable. The problem is that the __newindex is not being invoked. I'm not entirely sure on the details of how _G works but when you create a global variable the __newindex metamethod is not invoked. Like test this: _G = setmetatable({}, {__newindex=function() error("."); end }); x = 5; --no output _G.x = 1 --output
warspyking
#140730951Friday, July 18, 2014 12:27 PM GMT

Glad to see you again cnt. So I did test; setmetatable(_G, {__newindex=function() error("."); end }); x = 5; --no output It errors with "."
cntkillme
#140731009Friday, July 18, 2014 12:29 PM GMT

Weird, I tested it on the Lua site and have me no output. Anyway try using the ENV table instead of the G table
warspyking
#140733326Friday, July 18, 2014 1:19 PM GMT

I kinda like _G though XD
cntkillme
#140736118Friday, July 18, 2014 2:07 PM GMT

Just realized something, you are completely resetting _G at that time, instead of resetting _G, try something like: History = {} local function Detect(T) return { __newindex = function(t, var, val) rawset(History, #History + 1, "Global variable " .. var .. " was set to " .. val) rawset(T, var, val) end, __index = function(t, var) rawset(History, #History + 1, "Global variable " .. var .. " was indexed.") return rawget(T, var) end } end Old_G = {} setmetatable(_G, Detect(Old_G)); //as opposed to resetting it x = 5 print(x) print(#History) print(History[1]) print(History[2])
cntkillme
#140736418Friday, July 18, 2014 2:11 PM GMT

So this works: setmetatable(_G, {__newindex=function() error("."); end;}); x=1;print(x); This doesn't: _G = setmetatable({}, {__newindex=function() error("."); end;}); x=1;print(x);
warspyking
#140737282Friday, July 18, 2014 2:23 PM GMT

I was returning a proxy table so that I could use __index and __newindex even if the value is not nil. Will that still happen with yours?
cntkillme
#140737474Friday, July 18, 2014 2:26 PM GMT

Yeah, Old_G will hold any newvalues (if you want, you can fill it up with the values in _G) The reason yours wasn't working was probably something to do with removing all values in the _G table which most functions are stored in regular Lua
warspyking
#140737833Friday, July 18, 2014 2:31 PM GMT

Thanks then :D

    of     1