|
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. |
|
|
MrNilJoin Date: 2014-04-26 Post Count: 352 |
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. |
|
|
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 |
|
|
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 |
|
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
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]) |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
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 |
|
|
Glad to see you again cnt.
So I did test;
setmetatable(_G, {__newindex=function() error("."); end });
x = 5; --no output
It errors with "." |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
Weird, I tested it on the Lua site and have me no output. Anyway try using the ENV table instead of the G table
|
|
|
I kinda like _G though XD |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
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]) |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
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); |
|
|
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? |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
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 |
|
|