Zephos wrote:Let Y be the main problem here now. My X should be irrelevant for you as long as I know exactly what I want, anyway. Just please give me the answer to my problem and let me be happy, especially if the solution is exactly as easy as I imagined it to be:
Code: Select all
function object_destroy(objstr)
loadstring(objstr .. " = 'whateverthefuckiwant'")()
end
...
object_destroy("obj")
Now, is there anything to complain about this? In the end, that's all I wanted to know.
You can not do what you want. Let me try to explain this.
When you assign a variable to a value, for example
local a = {}, you create a reference to it.
When you pass a variable to a function (for example by calling
foo(a)), you are creating a
second reference to the same value. While foo executes, your program has two references to the table. When foo finishes executing the second reference is eliminated, unless you return it or save it somewhere else.
This means that it doesn't matter what you do to the variable inside foo; the variable "outside" foo still exists - you are just modifying a second copy. In other words, your code is doing something equivalent to this:
Code: Select all
local a = {}
local b = a -- create a second reference
b = nil -- b is nil, but a still points to the table
Since your program still has one reference to the value, the garbage collector will not collect it (yet). It might collect the value later on, when
all the references to it have been eliminated.
The simplest way to do what you want is using a table to contain the references. You can set the values of the table to nil very easily:
Code: Select all
local objects = {}
function object_destroy(objstr)
objects[objstr] = "whateverthefuckiwant" -- no need to use loadString here
end
...
object_destroy("obj")