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: Using ChildAdded with Tables

Previous Thread :: Next Thread 
epicfail22 is not online. epicfail22
Joined: 25 Sep 2009
Total Posts: 3739
19 Jan 2012 05:23 PM
How exactly would I use a ChildAdded-like event within a table (t={}). I know it has something to do with metatables, but what metamethod exactly would I use and how would I use it? Thank you.
Report Abuse
oxcool1 is not online. oxcool1
Joined: 05 Nov 2009
Total Posts: 15444
19 Jan 2012 05:34 PM
[ Content Deleted ]
Report Abuse
epicfail22 is not online. epicfail22
Joined: 25 Sep 2009
Total Posts: 3739
19 Jan 2012 07:26 PM
Will that work with table.insert()?
Report Abuse
oxcool1 is not online. oxcool1
Joined: 05 Nov 2009
Total Posts: 15444
19 Jan 2012 07:35 PM
[ Content Deleted ]
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
19 Jan 2012 09:06 PM
Let me find my script. It's more user-friendly than setting __newindex to what you want to do.
Report Abuse
Cyrok is not online. Cyrok
Joined: 11 Jan 2012
Total Posts: 630
19 Jan 2012 09:10 PM
inb4AFFpostswithmindbogglingcode

{ I have an OCD when it comes to non-camelCase variables/custom functions. }
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
19 Jan 2012 09:13 PM
function getTableWithCA(TableQ)
    local tab = type(TableQ)=="table" and TableQ or {}
    local Calling = false
    local Connections = {}
    local Events = {
        ChildAdded = {
            connect = function(self, Func)
                local connection = newproxy(true)
                local mt = getmetatable(connection)
                local Index = {
                    Name = "ChildAdded",
                    Fire = Func,
                    Active = true,
                    disconnect = function(self)
                        self.Active = false
                        self.Fire = function() end
                        for _, v in pairs(Connections) do
                            if v == connection then
                                table.remove(Connections, _)
                                break
                            end
                        end
                    end
                }
                mt.__index = function(t,k)
                    if type(Index[k])=="function" then
                        return function(a,...)
                            if a~=nil then
                                if not Calling then
                                    return Index[k](Index, ...)
                                else
                                    return Index[k](a, ...)
                                end
                            else
                                return Index[k]
                            end
                        end
                    else
                        return Index[k]
                    end
                end
                mt.__newindex = function() print"No" end
                mt.__metatable = {}
                table.insert(Connections, connection)
                return connection
            end
        }
    }
    return setmetatable({}, {
        __index = function(t, k)
            if Events[k] then
                return Events[k]
            else
                return tab[k]
            end
        end,
        __newindex = function(t, k, v)
            tab[k]=v
            for _, V in pairs(Connections) do
                if V.Name == "ChildAdded" then
                    if V.Active then
                        Calling = true
                        tab[k] = v
                        coroutine.wrap(V.Fire)(k, v)
                        Calling = false
                    end
                end
            end
        end,
        __metatable = {},
    })
end


local myTable = getTableWithCA()
myTable.One = "one"

local TheConnection = myTable.ChildAdded:connect(function(Key, Value)
    print(Key, Value)
end)

myTable.One = "two"
myTable.Noob = 1337

TheConnection:disconnect()

myTable.NewIndex = "New index!"

--> One two
--> Noob 1337
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
19 Jan 2012 09:15 PM
You can also use the ChildAdded event with existing tables:


local myTable = { One = 1, Two = 2, "Lol", "epicsause" }

myTable = getTableWithCA(myTable)
myTable.One = "one"

local TheConnection = myTable.ChildAdded:connect(function(Key, Value)
    print(Key, Value)
end)

myTable.One = "two"
myTable.Noob = 1337

TheConnection:disconnect()

myTable.NewIndex = "New index!"

--> One two
--> Noob 1337



NOTE: This ChildAdded "event" also acts as a Changed event.
Report Abuse
Rensus is not online. Rensus
Joined: 19 Jun 2008
Total Posts: 454
19 Jan 2012 10:27 PM
You scared him off, AgentFireFox! :O

-Rensus
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
19 Jan 2012 10:28 PM
Look at AFF
making scripts for everything. . .
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
19 Jan 2012 10:30 PM
Making code like this takes off a lot of stress.
Report Abuse
blockoo is not online. blockoo
Joined: 08 Nov 2007
Total Posts: 17202
19 Jan 2012 10:36 PM
And spreads it to everyone else when they read it o_o

~Would you like some butter with that latetoast?
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
19 Jan 2012 10:37 PM
"Making code like this takes off a lot of stress."

You like to code as a stress releiver ._.? I like to script but unless its one of my 'scripting days' I can never stay concentrated long enough to actually get anything done :/. I mostly listen to music and draw :3
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
19 Jan 2012 10:38 PM
*reliever
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
19 Jan 2012 10:38 PM
Writing code, playing COD, or writing my story. That's what I do to relieve stress.
Report Abuse
JulienDethurens is not online. JulienDethurens
Joined: 11 Jun 2009
Total Posts: 11046
20 Jan 2012 12:46 AM
@AgentFirefox

If I was you, I'd rename it 'FieldAdded'. Why would you assume that everything that's in the table is a child, an object? What if I add '2' in the table, ChildAdded wouldn't make much sense, would it?
Report Abuse
Cyrok is not online. Cyrok
Joined: 11 Jan 2012
Total Posts: 630
20 Jan 2012 03:30 PM
But then the number two becomes a child of that table, child being a general term (not only for objects).

{ I have an OCD when it comes to non-camelCase variables/custom functions. }
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
20 Jan 2012 03:51 PM
I only named it ChildAdded because of the thread title. XD
The name may not be technical, but it still works, and it is VERY similar to a ChildAdded/Changed event with in-game objects. Having the name FieldAdded may confuse the beginners that were to try this script out.

But yes, I agree, FieldAdded is more appropriate, and should have a different firing than a FieldChanged "event."
Report Abuse
epicfail22 is not online. epicfail22
Joined: 25 Sep 2009
Total Posts: 3739
20 Jan 2012 04:29 PM
Wait, so I can just flat-out say

Table = {}
Table.ChildAdded:connect(function(c) print(c.Name) end)
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
20 Jan 2012 04:30 PM
local myTable = { One = 1, Two = 2, "Lol", "epicsause" }

myTable = getTableWithCA(myTable)
myTable.One = "one"

local TheConnection = myTable.ChildAdded:connect(function(Key, Value)
    print(Key, Value)
end)
-- Notice the event passes the Key AND the Value to the function (in that order)

myTable.One = "two"
myTable.Noob = 1337

TheConnection:disconnect()

myTable.NewIndex = "New index!"

--> One two
--> Noob 1337
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
20 Jan 2012 04:31 PM
The getTableWithCA function is what 'creates' this ChildAdded 'method' in your table. All it is is just a clever use of metatables.
Report Abuse
epicfail22 is not online. epicfail22
Joined: 25 Sep 2009
Total Posts: 3739
20 Jan 2012 04:34 PM
But you haven't defined 'getTableWithCA(myTable)' in the script...
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
20 Jan 2012 04:35 PM
Read my longest post. It's up about half a page.
Report Abuse
ElectricAxel is not online. ElectricAxel
Joined: 15 May 2009
Total Posts: 16239
20 Jan 2012 04:38 PM
Why not:

a = getTableWithCA {1, 2, 3} --? It would work too. ;P
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
20 Jan 2012 04:39 PM
Yes, I know. I just don't want to cause any more confusion than I already have.
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