Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Best way to perform a deep copy of an object? — Gideros Forum

Best way to perform a deep copy of an object?

DudDud Member
edited May 2012 in General questions
Apologies - this is another lua question really rather than Gideros but hopefully someone can suggest a good solution.

I have an array of Buttons and I would like to move a button from one array position to another.

I was making the second array position equal to the first and then setting the first array position to nil - but of course I soon realised that I need to actually perform a proper deep copy of the Button otherwise the second array position is only pointing at the Button in the first array position.


Given that my button class has all sorts of properties - do I have to write a custom method to perform a deep copy of a class instance or is there something built into Lua that can create a proper copy?

Many thanks

Comments

  • I believe it's standard practice to add a :clone() or :copy() function to the button - you should be able to do this to the Button class directly so all your buttons will have this functionality.

    The function will then create a new instance of the current button (self) and then initialise it to have the same values as the current button, you then return this new button as the result of the clone / copy function.
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • ar2rsawseenar2rsawseen Maintainer
    edited May 2012
    Hmm, that's interesting. From what you described, it should be sufficient to use reference to button instance. If button is a class, then all references used inside this class should point to instance aka self variable, thus it doesn't matter from where are you pointing to this instance.

    But if you need to copy, then it would depend on structure of your button, there is no standard function to do it.

    Here is an example for tables, which I think should also work for classes:
    http://stackoverflow.com/questions/640642/how-do-you-copy-a-lua-table-by-value
  • DudDud Member
    Thanks for the replies and link - I shall implement a clone function as suggested
Sign In or Register to comment.