Seranok
|
  |
| Joined: 12 Dec 2009 |
| Total Posts: 11083 |
|
|
| 22 Jul 2011 04:27 PM |
local Variable = 5
Is there any way to connect to that variable changed?
Or should I use this?
metaTable.Var = 5 |
|
|
| Report Abuse |
|
|
|
| 22 Jul 2011 04:32 PM |
local Var = setmetatable({}, {Value = {}, __index = function(a, b) return getmetatable(a).Value[b] end, __newindex = function(a, b, c) getmetatable(a).Value = c getfenv(2)["Var"..b.."Changed"](c) end})
Var[1] = 5 function Var1Changed(Val) print("Var[1] was changed to", Val) end
:D |
|
|
| Report Abuse |
|
|
Seranok
|
  |
| Joined: 12 Dec 2009 |
| Total Posts: 11083 |
|
| |
|
|
| 22 Jul 2011 04:35 PM |
Well, the way I did is probably the only way to do it :U because there's no Changed event of variables. If we could use the debug library, then someone would be able to script the Changed event into numbers, strings, and booleans, but since we can't, you have to use metatables and stuff to replicate a Changed event.
By the way, I just made that one up off the top of my head, so it may not work.
why am i blue |
|
|
| Report Abuse |
|
|
|
| 22 Jul 2011 04:37 PM |
OWAIT
There actually might be a way to do it using the function environment... :O
yus
local Var = setmetatable({}, {Value = {}, __index = function(a, b) return getmetatable(a).Value[b] end, __newindex = function(a, b, c) getmetatable(a).Value = c getfenv(2)["Var"..b.."Changed"](c) end, __metatable = "The metatable is locked"}) setfenv(1, Var)
a = "hi" function aChanged(new) print("a was changed to "..new) end a = 4
maybe... :D |
|
|
| Report Abuse |
|
|
|
| 22 Jul 2011 04:38 PM |
wait forgot to change something
local Var = setmetatable({}, {Value = {}, __index = function(a, b) return getmetatable(a).Value[b] end, __newindex = function(a, b, c) getmetatable(a).Value = c getfenv(2)[tostring(b).."Changed"](c) end, __metatable = "The metatable is locked"}) setfenv(1, Var)
a = "hi" function aChanged(new) print("a was changed to "..new) end a = 4
:D
why am i blue |
|
|
| Report Abuse |
|
|
|
| 22 Jul 2011 04:43 PM |
Can't do that, taterz. :P
local Var = (function() local M = { Value = 0 } return setmetatable({}, {__index = M, __newindex = function(t, k, v) if k == "Value" then print("Value changed") M[k] = v return v end end, __metatable = "The metatable is locked"}) end)()
Var.Value = 10 print( Var.Value ) Var.Value = "HI" print( Var.Value )
~~~ AFF ~~~ |
|
|
| Report Abuse |
|
|
|
| 22 Jul 2011 04:45 PM |
Oh, and you used to be able to set a metatable to the script's global environment... But they locked it back in 09 I believe...
~~~ AFF ~~~ |
|
|
| Report Abuse |
|
|
|
| 22 Jul 2011 04:51 PM |
@Agent: Why not? You can put anything in a metatable, right? It doesn't have to be metamethods only D:
Although, I think my last example might need you to remove the __metatable metamethod for it to work, since it indexes the metatable...
Or is it just because I set it to a local variable? :I |
|
|
| Report Abuse |
|
|
|
| 22 Jul 2011 04:53 PM |
Your script just doesn't make sense. At all. X_X
~~~ AFF ~~~ |
|
|
| Report Abuse |
|
|
|
| 22 Jul 2011 04:55 PM |
I typed it in like 2 minutes, and I made a few mistakes D:
Var = setmetatable({}, {Value = {}, __index = function(a, b) return getmetatable(a).Value[b] end, __newindex = function(a, b, c) getmetatable(a).Value[b] = c getfenv(2)[tostring(b).."Changed"](c) end}) setfenv(1, Var)
a = "hi" function aChanged(new) print("a was changed to "..new) end a = 4
But why doesn't it make any sense at all D: I don't see any mistakes besides the ones fixed there |
|
|
| Report Abuse |
|
|
|
| 22 Jul 2011 04:58 PM |
You aren't even using Var[INDEX], which means the __newindex metamethod wouldn't even fire.
~~~ AFF ~~~ |
|
|
| Report Abuse |
|
|
|
| 22 Jul 2011 05:00 PM |
Saying Var[INDEX] fires the __index metamethod, and that would just be when you want to access the value, which I had no need to do :U
And the __newindex metamethod of the function environment should fire when adding a variable, right? |
|
|
| Report Abuse |
|
|
|
| 22 Jul 2011 05:03 PM |
"And the __newindex metamethod of the function environment should fire when adding a variable, right?"
Yes, but please do tell me where you access the function environment? You're setting the metatable to an empty table, and the empty table is held as Var. You're not doing ANYTHING with the Var table.
Also, you can't set a metatable to a function environment, which means you have to use something else.
~~~ AFF ~~~ |
|
|
| Report Abuse |
|
|
|
| 22 Jul 2011 05:08 PM |
"Also, you can't set a metatable to a function environment, which means you have to use something else."
> setfenv(1, setmetatable({unpack(getfenv())}, {__index = function(a, b) return getfenv(2)[b] or"pie" end})) print(lol) pie
?
"You're setting the metatable to an empty table, and the empty table is held as Var."
Yeah, I suppose that's not what I should be doing, but again, I made it up in like 2 minutes D:
"but please do tell me where you access the function environment?"
setfenv(stuff) getfenv(number) a = 4
All of those are accessing the function environment D: |
|
|
| Report Abuse |
|
|
|
| 22 Jul 2011 05:11 PM |
But since you're doing nothing with the Var table, you are not accessing the environment (apart from a=4, which does nothing for the purpose of making the script).
> setfenv(1, setmetatable({unpack(getfenv())}, {__index = function(a, b) return getfenv(2)[b] or"pie" end})) print(lol) pie
Interesting. Though unpack doesn't get non-numeric indices, does it? It's been a very long time since I've used it last.
~~~ AFF ~~~ |
|
|
| Report Abuse |
|
|
iTerm
|
  |
| Joined: 12 Oct 2010 |
| Total Posts: 210 |
|
|
| 22 Jul 2011 05:13 PM |
OMG!!! Easy way....
var = 1 var.Changed:connect(function() var + 1 end) |
|
|
| Report Abuse |
|
|
|
| 22 Jul 2011 05:14 PM |
Posted By: iterm on 07-22-2011 05:13 PM Subject: Re: Connect to Variable Changed? Message: OMG!!! Easy way....
var = 1 var.Changed:connect(function() var + 1 end) __________________________
lolno Doesn't work.
~~~ AFF ~~~ |
|
|
| Report Abuse |
|
|
iTerm
|
  |
| Joined: 12 Oct 2010 |
| Total Posts: 210 |
|
|
| 23 Jul 2011 09:50 AM |
| If was something with a event Changed.... Lol. |
|
|
| Report Abuse |
|
|