User:JulienDethurens/Removing Objects: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>JulienDethurens
No edit summary
>JulienDethurens
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
In a recent update, 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.
#redirect [[User:JulienDethurens/Essays/Removing objects]]
 
==remove==
 
The [[Remove_(Method)|remove]] method was already deprecated when this update happened, because it uses camelCase, which was deprecated quite some time ago. The remove method should <strong>NEVER</strong> 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.
 
{{EmphasisBox|The [[Remove_(Method)|remove]] method should never be used. There is no good reason to use it. There is no case which prefers the use of the remove method. Just don't use it.|red|dark=yes}}
 
==Remove==
 
The [[Remove_(Method)|Remove]] method was deprecated in this update. What it did is set the [[Parent_(Property)|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.
 
{{EmphasisBox|The [[Remove_(Method)|Remove]] method should never be used. There is no good reason to use it. There is no case which prefers the use of the Remove method. Just don't use it.|red|dark=yes}}
 
==Destroy==
 
The [[Destroy_(Method)|Destroy]] method was introduced in this update. What it does is set the [[Parent_(Property)|Parent]] of the object it is called on to nil, lock the [[Parent_(Property)|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 <em>much</em> better memory management.
 
{{EmphasisBox|Some players think Destroy skips the garbage collection and immediately deallocates the object, by removing it from the memory. However, <strong>that is not true, and these players are seriously mistaken.</strong> The Destroy method <em>does</em> manage the memory in a much better way than Remove, and could even be considered as a fix to a certain memory leak, but it does not directly deallocate the object.|blue}}
 
==ClearAllChildren==
 
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_(Method)|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 don't need an object anymore and want to get rid of it (most common case), use the Destroy method on it:
 
<code lua>
object:Destroy()
</code>
 
If you want to store an object temporarily out of the game hiearchy, set its Parent to nil:
 
<code lua>
object.Parent = nil
</code>
 
If you want to remove all the descendants of an object, use the ClearAllChildren method on it:
 
<code lua>
object:ClearAllChildren()
</code>
 
{{EmphasisBox|<strong>The [[Remove_(Method)|Remove]] and the [[Remove_(Method)|remove]] methods should never be used. There is no good reason to use them. There is no case which prefers the use of them over another. Just don't use them.</strong>|red|dark=yes}}

Latest revision as of 19:39, 9 April 2012