Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I get the parent of a table?

Asked by 4 years ago

lets say we have a table

Table = {TableInsideOfTable = {}}

if I only had TableInsideOfTable how would I get the parent

0
can we have ur whole script cuz im not sure if its possible HappyTimIsHim 652 — 4y
0
imma try to find an alternative instead HappyTimIsHim 652 — 4y
0
So let’s say you have a Table called TableExample = {variable1 = 123, variable2 = “hello, variable3 = { variable1 = “hello”, variable2 = 13554}} . So how tables work is they assign an address to a certain block of memory. So like if you gave me an address to a storage unit, it’s kind of like that. So when you make a table like the one I just made, you can’t access the inside values without first g SethHeinzman 284 — 4y

3 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

what your actually looking for is called a linked list. linked lists work by one object(or table as lua addresses it) pointing to another object (or a set of objects as an array), and that internal object points to another object, etc..

in fact, the Roblox Hierarchy system works using lists, except they are written in C++. each object has a property called Parent which points to the object that holds reference to it.

you can create lists like this:

function create_object(value, parent)
    local instance = {
        Parent = parent,
        Value = value,
        Children = {}, --the nodes(or internal objects) it will point to.
    }

    function instance:push(node)
        node.Parent = self
        self.Children[#(self.Children) + 1] = node;
    end

    if parent then
        parent:push(instance)
    end
    return instance
end

function create_hierarchy(length)
    local head = create_object(0);
    local current_node = head

    for i =1, math.max(1, length), 1 do
        local node  = create_object(i, current_node);
        current_node = node;
    end
    return head;
end

function iterate_hierarchy(hierarchy)
    for _, node in ipairs(hierarchy.Children) do
        print("hey i am number #",node.Value)
        iterate_hierarchy(node)
    end
end

local hierarchy = create_hierarchy(100);
iterate_hierarchy(hierarchy)

of course this is just an example of data an object(or node) can store.. Roblox nodes store properties and methods like IsA(), FindFirstChild(), Parent, Name IsArchivable

but these nodes only store Value, Push(), and Children[]

1
Love linked lists. Good point SethHeinzman 284 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

You have to keep track of such information separately, such as performing TableInsideOfTable.Parent = Table, or tableParents[TableInsideOfTable] = Table (assuming TableInsideOfTable is a variable referencing the table).

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

So let’s say you have a Table called

TableExample = {variable1 = 123, variable2 = “hello, variable3 = { variable1 = “hello”, variable2 = 13554}}

So how tables work is they assign an address to a certain block of memory.

So like if you gave me an address to a storage unit, it’s kind of like that. So when you make a table like the one I just made, you can’t access the inside values without first going to the address in memory for where those values are stored.

So for example imagine the TableExample is the storage unit, and the boxes inside the storage unit are the variabes.

So when you say how can I find the parent of a table, there isn't a way because there can be multiple tables of the same name inside of different tables. So what I mean is this:

Table= {variable1 = 123, variable2 = “hello, variable3 = { variable1 = “hello”, variable2 = 13554}}

Table2 = {variable1 = 123, variable2 = “hello, variable3 = { variable1 = “hello”, variable2 = 13554}}

Table3 = {variable1 = 123, variable2 = “hello, variable3 = { variable1 = “hello”, variable2 = 13554}}

Tables don't have parents, because they are not instances in a game. So what I mean is you would access something in the game by saying script.Parent or game.ServerStorage or game.Lighting.

Maybe in a module script but that's different....

However there is a way to make this easier for you. If you aren't going to have the same variables in different tables you can keep track of what variables are in what table by storing those in something seperate.

So like if I had this Table that I knew had variables named differently from other Tables like so:

Table= {DifferentTable = {}, }

Table2 = {TableNamedSomethingBlah = {}, }

Instead of:

Table= {DifferentTable = {}, }

Table2 = {DifferentTable= {}, }

Now to answer your question:

If you know the name DifferentTable but want to know the name Table in the example above you could create something to store that information on the side. So maybe another table like this:

TableTracker = { DifferentTable = "Table", TableNamedSomethingBlah = "Table2" }

Or you can create folders or something.

Answer this question