User:JulienDethurens/Removing Objects
Recently, two methods were introduced, and an older method, Remove, was deprecated. In this article, I am going to explain everything and remove all the confusion about these.
remove
The remove method was already deprecated when this update happened, because it uses camelCase, which was deprecated quite some time ago. The remove method should NEVER be used, there is just no reason at all to use it. This method does the same thing as the Remove method, but its name is different.
Remove
The Remove method was deprecated in this update. What it did is set the Parent of every children of the object it is called on to nil, and call itself on all of them, resulting in all the descendants of the object being parented to nil.
Destroy
The Destroy method was introduced in this update. What it does is set the Parent of the object it is called on to nil, lock the Parent property of the object and disconnect all the connections attached to events of the object.
Using the Destroy method instead of the Remove method makes you benefit from the fact it disconnects all the connections related to the object, connections which you don't need. What this means is much more elegant code on the C side and much better memory management.
ClearAllChildre
Because some persons incorrectly used the Remove method to clear an object from all its descendants, the admins also decided to make a method that does exactly that: ClearAllChildren. This method simply sets the Parent of all the descendants of an object to nil, by setting the Parent of all the children of the object to nil and then calling itself on all of them.
Parent Property
Some persons incorrectly used the Remove method to store an objet in nil temporarily. Not only is that an incorrect way to do it, but there was already a correct way to do it: setting its Parent property to nil.
If you want to store an object out of the game hierarchy temporarily, set its Parent property to nil.
Conclusion (AKA: tl;dr)
If you want to store an object temporarily out of the game hiearchy, set its Parent to nil:
object.Parent = nil
If you don't need an object anymore and want to get rid of it:
object:Destroy()
If you want to remove all the descendants of an object:
object:ClearAllChildren()