skate1011
|
  |
| Joined: 13 Mar 2010 |
| Total Posts: 688 |
|
|
| 22 Dec 2012 06:04 PM |
What would be a good way to simulate the .Changed event on a in-script variable without using a while loop?
wiki.roblox.com |
|
|
| Report Abuse |
|
|
|
| 22 Dec 2012 06:07 PM |
| If statement, or repeat until if you are waiting for a certain thing to change. |
|
|
| Report Abuse |
|
|
skate1011
|
  |
| Joined: 13 Mar 2010 |
| Total Posts: 688 |
|
|
| 22 Dec 2012 06:09 PM |
That's not what I'm asking for. What I'm asking for is how would I simulate a .Changed event inside a script on a in-script variable.
wiki.roblox.com |
|
|
| Report Abuse |
|
|
|
| 22 Dec 2012 06:13 PM |
Oh. My bad. Misinterpreted that. I would do something like this:
a = script.Parent if a.Changed then --stoof here end |
|
|
| Report Abuse |
|
|
skate1011
|
  |
| Joined: 13 Mar 2010 |
| Total Posts: 688 |
|
|
| 22 Dec 2012 06:13 PM |
I was wondering if I could use tables or custom events for this, not a repeat until or if statement.
wiki.roblox.com |
|
|
| Report Abuse |
|
|
|
| 22 Dec 2012 06:16 PM |
Here's the while loop version if that didn't work (I know you don't want this).
function check() if script.Parent.Name ~= script.Parent.Name then break --stoof here else --other stoof end
--
while true do check() end
Ineffeciency, I know. |
|
|
| Report Abuse |
|
|
skate1011
|
  |
| Joined: 13 Mar 2010 |
| Total Posts: 688 |
|
|
| 22 Dec 2012 06:25 PM |
If you haven't read, I was trying to simulate a .Changed event on a in-script variable(a bool value) without using repeat until or if statement. Oh, and that check function would crash, because it would overload.
wiki.roblox.com
|
|
|
| Report Abuse |
|
|
| |
|
|
| 22 Dec 2012 06:29 PM |
| And you never said anything about a repeat until statement. I did read if you looked inside the parenthesis. |
|
|
| Report Abuse |
|
|
|
| 22 Dec 2012 07:49 PM |
| This is advanced scripting. If you cannot help, please get out of the thread. |
|
|
| Report Abuse |
|
|
|
| 22 Dec 2012 07:52 PM |
local env = getfenv() local function Changed(key, newValue) -- stuff print("Changed!", key, newValue) end local newenv = setmetatable({}, {__index = env, __newindex = function(t, k, v) Changed[k, v] env[k] = v end}) setfenv(0, newenv)
a = "o" --> Changed! a o
I can make it so you do 'a.Changed:connect(function() end)' if you wish. |
|
|
| Report Abuse |
|
|
skate1011
|
  |
| Joined: 13 Mar 2010 |
| Total Posts: 688 |
|
|
| 22 Dec 2012 08:45 PM |
Please do. By the way, thanks for the help, Agent.
wiki.roblox.com |
|
|
| Report Abuse |
|
|
|
| 22 Dec 2012 09:06 PM |
Actually, don't do that. Do this:
do local env = getfenv() local id = 1 local Connections = {} local function Disconnect(Connection) for _, v in pairs(Connections) do if v.ID == Connection.ID then table.remove(Connections, _) return end end end local ConnectFunction = function(dummy, Callback) local ID = id id = id + 1 local con = {ID = ID, Fire = Callback} table.insert(Connections, con) return setmetatable({}, {__index = function(t, k) if k == "disconnect" then return function() Disconnect(con) end end end}) end local Changed = setmetatable({}, {__index = function(t, k) if k == "connect" then return ConnectFunction end end}) local newenv = setmetatable({}, {__index = function(t, k) if k == "Changed" then return Changed else return env[k] end end, __newindex = function(t, k, v) for _, v in pairs(Connections) do v.Fire(k, v) end end}) setfenv(0, newenv)
end
a = "o" getfenv().Changed:connect(function(VarName, Value) print(VarName, Value) end) a = 12 --> a 12 |
|
|
| Report Abuse |
|
|
|
| 22 Dec 2012 09:11 PM |
Here's a tested, working version:
do local env = getfenv() local id = 1 local Connections = {} local function Disconnect(Connection) for _, v in pairs(Connections) do if v.ID == Connection.ID then table.remove(Connections, _) return end end end local ConnectFunction = function(dummy, Callback) local ID = id id = id + 1 local con = {ID = ID, Fire = Callback} table.insert(Connections, con) return setmetatable({}, {__index = function(t, k) if k == "disconnect" then return function() Disconnect(con) end end end}) end local Changed = setmetatable({}, {__index = function(t, k) if k == "connect" then return ConnectFunction end end}) local newenv = setmetatable({}, {__index = function(t, k) if k == "Changed" then return Changed else return env[k] end end, __newindex = function(t, k, v) for _, con in pairs(Connections) do con.Fire(k, v) end env[k] = v end}) setfenv(1, newenv) end
a = "o" getfenv().Changed:connect(function(VarName, Value) print(VarName, Value) end) a = 12 --> a 12 |
|
|
| Report Abuse |
|
|