|
| 12 Dec 2014 08:32 AM |
| Is there a hacky thing I could put in my script to do this? |
|
|
| Report Abuse |
|
|
Fedorakid
|
  |
| Joined: 17 Jul 2010 |
| Total Posts: 7079 |
|
|
| 12 Dec 2014 08:56 AM |
You mean like
variable = 9
variable changed something
variable = 3 --something happens since you changed it
??? |
|
|
| Report Abuse |
|
|
|
| 12 Dec 2014 09:01 AM |
| like Variable.Changed:connect(function() |
|
|
| Report Abuse |
|
|
SLY3
|
  |
| Joined: 10 Jul 2008 |
| Total Posts: 1700 |
|
|
| 12 Dec 2014 09:16 AM |
| I don't think there's a way you can do it to script variables, only physical values. |
|
|
| Report Abuse |
|
|
Fedorakid
|
  |
| Joined: 17 Jul 2010 |
| Total Posts: 7079 |
|
|
| 12 Dec 2014 09:28 AM |
| Well, if I were to do this. I'd have the variables in a table then use the __newindex metamethod. |
|
|
| Report Abuse |
|
|
|
| 12 Dec 2014 09:33 AM |
| ya i still have to learn that |
|
|
| Report Abuse |
|
|
breuning
|
  |
| Joined: 30 Oct 2008 |
| Total Posts: 4268 |
|
|
| 12 Dec 2014 09:33 AM |
function changed(val,functiontorun) coroutine.wrap(function() val2 = getfenv(0)[val]
while game:GetService("RunService").RenderStepped:Wait() do if val2 ~= getfenv(0)[val] then functiontorun() val2 = getfenv(0)[val] end end end) end
not sure if it works
|
|
|
| Report Abuse |
|
|
Fedorakid
|
  |
| Joined: 17 Jul 2010 |
| Total Posts: 7079 |
|
|
| 12 Dec 2014 09:41 AM |
This is what i'd do, just because I don't get getfenv and setfenv. I get it but I don't get how I'd use it usefully because I'm so unsecure on it.
vars = { var1 = "hello", var2 = "hey", var3 = "hmmm" }
setmetatable(vars, {__newindex = function(t,i,v)
--What do you want to happen when something changes?
end})
vars is a table where you add variables, add what is supposed to happen when a variable change.
atleast this is how i'd do it |
|
|
| Report Abuse |
|
|
|
| 12 Dec 2014 09:44 AM |
| use a table for your variables, then use metamethods |
|
|
| Report Abuse |
|
|
|
| 12 Dec 2014 10:01 AM |
So __newindex fires when setting an index that has already been indexed as well?
--ThatChristianGuy |
|
|
| Report Abuse |
|
|
|
| 12 Dec 2014 11:00 AM |
| No. __newindex only fires when a nil value is set to something else. |
|
|
| Report Abuse |
|
|
|
| 12 Dec 2014 11:28 AM |
Yes, you can. But only if the variable is global, not local. Put this at the start of your script. Call addWatch with a variable name to add it to the watch. Call addConnection with the variable name to be notified when it changes, and the function to call. Enjoy!
do
local env = getfenv() local watchVariables = {} local watchValues = {}
function __newindex (environment,key, value) if watchVariables[key] then watchValues[key] = value for func, TRUE in next, watchConnections[key] do local success, msg = pcall(func, key, value) if not success then error("Watch caught an error wile " .. key .. " was changing: " .. msg, 2) end end else return rawset(environment, key, value) end end
function __index(environment, key) if watchVariables[key] then return watchValues[key] else return rawget(environment, key) end end
function addWatch(varName) watchVariables[varName] = true watchValues[varName] = rawget(env, varName) rawset(env, varNAme, nil) watchConnections[varName] = {} end
function addConnection(varName, func) watchConnections[varName][func] = true end
function removeConnection(varName, func) watchConnections[varName][func] = nil end
function resetConnections(varName) watchConnections[varName] = {} end
function removeWatch(varName) watchVariables[varName] = nil watchValues[varName] = nil watchConnections[varName] = nil end
setmetatable(env, env)
end
|
|
|
| Report Abuse |
|
|
|
| 13 Dec 2014 05:54 PM |
| Most of my variables are local. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 13 Dec 2014 05:56 PM |
Just use newproxy and __newindex and a "hidden" table?
This is one without newproxy that I made a while back: http://pastebin.com/p9QvnuPU but it could be made better |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 13 Dec 2014 05:59 PM |
| Arbiter, what you said wouldn't work on Roblox since they locked the environment. You would have to create a new table and just set the __index to the default env. |
|
|
| Report Abuse |
|
|
| |
|
|
| 13 Dec 2014 06:16 PM |
Either way, you cannot access local variables without access to the Lua debug library. You will have to use global variables.
The function environment works just fine. Here is an example:
value = "Locked" print(tostring(value)) --> "Locked" getfenv().value = nil print(tostring(value)) --> "nil" |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 13 Dec 2014 06:18 PM |
I'm talking about this part:
local env = getfenv() ... setmetatable(env, env)
And also, just create a function to do event stuff instead of making all new global tables doing it? |
|
|
| Report Abuse |
|
|
|
| 13 Dec 2014 06:35 PM |
| ^ I see. That isn't too hard to fix. The purpose of the above script is to enable the event connections without changing existing code. |
|
|
| Report Abuse |
|
|