generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Answer

Previous Thread :: Next Thread 
skate1011 is not online. 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
gametastic100 is not online. gametastic100
Joined: 15 Jun 2011
Total Posts: 1286
22 Dec 2012 06:07 PM
If statement, or repeat until if you are waiting for a certain thing to change.
Report Abuse
skate1011 is not online. 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
gametastic100 is not online. gametastic100
Joined: 15 Jun 2011
Total Posts: 1286
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 is not online. 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
gametastic100 is not online. gametastic100
Joined: 15 Jun 2011
Total Posts: 1286
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 is not online. 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
gametastic100 is not online. gametastic100
Joined: 15 Jun 2011
Total Posts: 1286
22 Dec 2012 06:28 PM
Yup, forgot a wait.
Report Abuse
gametastic100 is not online. gametastic100
Joined: 15 Jun 2011
Total Posts: 1286
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
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
22 Dec 2012 07:49 PM
This is advanced scripting. If you cannot help, please get out of the thread.
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
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 is not online. 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
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
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
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image