Display Objects / custom component question

other than have the class create a sprite instance (Game Edition) rather than a newImage for a performance reasons, i don’t think you can do it any better way [import]uid: 6645 topic_id: 3515 reply_id: 12708[/import]

My display objects are indeed sprites, but having them cloned by making new sprite instances inside the new() function of the “class” is actually calling an internal function of the SDK (as with display.newImage). I want to avoid calling this creator function dozens of times, and instead have the original objects copied by value.

I have searched the net for many hours but can’t find a way to do this. I can’t believe that Lua has no facilities for copying by value instead of reference (even with an indirect way)!

[import]uid: 7356 topic_id: 3515 reply_id: 12709[/import]

but you can’t create a sprite without using the sprite.newSprite command. i’m guessing that’s stacking up a coordinate ready for the GL Draw command (which should blit a number of sprites at once based on a list of coordinates)… it’s not going to get more optimized than that. [import]uid: 6645 topic_id: 3515 reply_id: 12712[/import]

I want the creation to happen only once jmp909. After that, I want to have “photocopies” like these ones that are described bellow for php4:

"What are the differences between the Object Oriented model in PHP5 and PHP4?

Firstly (and the reason the release number jumped) is that assignments copy references in PHP5 (which is regarded as correct OO behaviour) but they duplicate or clone the object in PHP4. This may sound a bit obtuse, but:
* if you duplicate an object and then change one of the copies, you do NOT effect the data refered to by the other name (PHP4 behaviour)
* if you copy by reference (PHP5 behaviour, in effect giving the object a second name) and then change one of the “copies”, you effect both as there one and the same.
In effect, it’s the difference between letting someone share a piece of paper with you and both writing on it (PHP5), and taking a photocopy to give to someone then each writing on your own sheet (PHP4)."

Now, is there any “photocopier” workaround for Lua5? [import]uid: 7356 topic_id: 3515 reply_id: 12714[/import]

a new instance of a class *is* a photocopy (of the base object). if you change one it doesnt change the other.

but if you want to create a new copy of an instance (ie something that’s changes since it was instantiated, look up “deep copy” routines i guess

j

[import]uid: 6645 topic_id: 3515 reply_id: 12716[/import]

Actually, oop methods discussed here are not photocopies of the same page. They are ways to *re-write* the same content of a book-page to a second page, by hand, as you did for the original page.

The difference is that here you call the class constructor again, while i ask for a way to construct the object once and then replicate it many times without calling the constructor again.

Imagine a table-object that holds a sprite, a custom physics shape, a textfield, a functions table, a sound set and some touch handlers. It takes many cpu-cycles to bring this info together and construct the parent object! After creating the object once, I only want to have several copies of this exact object visually placed in different coordinates.

There must be a way to tell Lua “take this exact piece of memory and make a copy by value somewhere else, then change this bit of info only on the second place without altering the original memory piece”.

[import]uid: 7356 topic_id: 3515 reply_id: 12717[/import]

http://lua-users.org/wiki/CopyTable
? [import]uid: 6645 topic_id: 3515 reply_id: 12732[/import]

Nope! It copies the original object by reference.

Thanks, nevertheless :slight_smile: [import]uid: 7356 topic_id: 3515 reply_id: 12750[/import]

post #18 with the following inline URL no longer exists.

http://defunside.blogspot.com/2006/10/oop-in-lua.html

just an FYI, if anyone wants to find a suitable “EXAMPLE”

[import]uid: 11094 topic_id: 3515 reply_id: 23720[/import]

yes, I can create objects !

but,how can I delete or remove the objects ?
“nil” seems not work.

thanks alot. [import]uid: 21680 topic_id: 3515 reply_id: 37870[/import]

use removeSelf() [import]uid: 59054 topic_id: 3515 reply_id: 37872[/import]

no !
removeSelf() just only for corona objects,like movieClip,
but not work on custom objects. [import]uid: 21680 topic_id: 3515 reply_id: 37874[/import]

Sorry to raise this old topic, but I’m looking for a way to clone a display object.

Magenda, did you ever find out how to clone a display object? [import]uid: 43714 topic_id: 3515 reply_id: 50225[/import]