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

How do I remove references to a table?

Asked by 8 years ago

This seems a bit strange. I've got a hard logic problem based on the concepts of encapsulation here: Basically when I was trying to make a universal "discard" method for my tables, I ran into the problem where I could not remove the references other objects had to an object being discarded. Thus, I could only copy around an object, but never remove it without access to the references needed to be removed.

Let me make that more simple. Roblox has a "Destroy()" Method for all parts in the game, which => Sets the Parent property to nil, locks the Parent property, disconnects all connections and calls Destroy() on all children. So basically, when you call "Destroy" on any given instance, the object now will be nil, and any references to it will also be nil. If you set x = Instance.new("Part") and then x:Destroy(), when you try to print(x), you will get nil

In my case, I want to design an object which has a "discard" function. Like Destroy, when the discard method is fired, other references to the part will become nil, but the part itself will move to a "discarded" table to be kept for later.

Here's what I have so far

discard = {}

Part = {}

toolbox = {
    hammerTool = Part,
    sillyTool = nil
}

function Part.discard()
    table.insert(discard, Part)
    --How do I make it remove the reference toolbox.hammerTool holds?
end

So when I try to fire discard on the Part object, it will move a reference of the table to the discard table, but not remove it from it's original table:

--main--
toolbox.hammerTool.discard();
print("--toolbox--");
for _, v in pairs(toolbox) do
    print(v)
end
print("--discard--");
for _, v in pairs(discard) do
    print(v)
end

--OUTPUT
----toolbox--
--table: 22738DB0
----discard--
--table: 22738DB0

So, does anyone know a nifty way I could make it so that the discard method would remove the reference of toolbox.hammerToolwithout passing the reference as an argument?

0
Holding a reference to an object you call "Destroy" on (creating a variable for it), will not become nil when destroyed. In your first example, "x" would still represent the object. Just something I thought I should throw in there. ScriptGuider 5640 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago

Overview

I'm confident in understanding what you're trying to say, though I'm still a bit confused on it. Firstly, I think you missunderstood the example using "x" to create a part, then destroying it. Once you create the variable that represents that part object, it's not gonna change until you set it to nil. So saying this:

local x = Instance.new("Part")
x:Destroy()
print(x)

Is still going to print whatever the part's name returns (indicating that x still holds a reference to the object). If you want to create a method for destroying the object you create (be it a ROBLOX object, or just a normal table), you could simply just set it to nil inside the method. For example:

local part = {}

function part:destroy()
    -- Obviously if it was a ROBLOX object, you'd set the parent to nil if your goal was to physically remove it.
    part = nil
end

HOWEVER, I'd strongly advise against doing this. For what it sounds like you're trying to do, is remove the reference completely, which would cause more problems than it would solve. For example, trying to index "part" after calling that method would just error, since it's nil.

Solution?

Instead what you should do, is create a variable within the object that represents it's parent, or simply an indication that it's being used, and modify that. This is why all objects in ROBLOX have a Parent property to indicate if it exists, rather than using itself, and/or setting the reference to nil. Here's an example:

local part = {
    existing = true
}

function part:destroy()
    self.existing = false
end

And then your code would just be based around whether "part.existing" is true or not. Now as far as removing a reference from anything all together, just setting it to nil will do the job. This IS all of course assuming that I correctly understood what you were saying, so correct me if this is off topic.

0
I think I tend to overestimate ROBLOX's engine a lot more than I should :P. Should have been obvious once I actually started looking at the wording of Instance:Destroy() randomsmileyface 375 — 8y
Ad

Answer this question