What i have is a HashMap of all of my template objects - bullets, ships, asteroids, planets, images, sounds, etc. Now, using Slick2D Images i can use the duplicate method tog et a copy of my stored template.

How can i write a similar duplicate() method for my other classes?

I've been working on this all day with no luck:Simply implementing Clonable and writing a public clone() override fails. Creating a duplicate() class that creates a new object and then sets all its feilds and properties = to the original class fails.Constructors that use an object to initialize a new object fails.

Seriously, what do i have to do to copy a class?(I may be being a tard amd making a prtoblem where there is none, please put me streight if so!)

what seems to be happening is the the class I've copied, however i do it, is only a reference to the origional class, not a new class.

so if i clone 10 particle effects from the 'explosion' stored class, what i get is 10 copies that update 10* faster - where the update is repeatedly called on the stored class. The same thing seems to happen for other classes too.

I was wondering if there is a way of cloning any arbitrary class and retaining to references to the original.

what I'm really looking for is pointers on how to go about creating a Duplicator class with something like a

/** * Returns a copy of the object, or null if the object cannot * be serialized. */publicstaticObjectcopy(Objectorig) {Objectobj = null;try {// Write the object out to a byte arrayByteArrayOutputStreambos = newByteArrayOutputStream();ObjectOutputStreamout = newObjectOutputStream(bos);out.writeObject(orig);out.flush();out.close();

// Make an input stream from the byte array and read// a copy of the object back in.ObjectInputStreamin = newObjectInputStream(newByteArrayInputStream(bos.toByteArray()));obj = in.readObject(); }catch(IOExceptione) {e.printStackTrace(); }catch(ClassNotFoundExceptioncnfe) {cnfe.printStackTrace(); }returnobj; }

You could use Kryo to round trip your objects. You go objects -> bytes -> objects and the second set of objects you get are your copies. Same as kingaschi's suggestion, but more efficient.

The clone method probably works for your simple case, but requires you to write manual deep copy code and in general sucks. You could extend a Cloneable class and not override clone in your subclass. Also, clone uses an extralinguistic mechanism to create an object without calling a constructor, which is hacky.

A copy constructor sucks when you want to create a copy and you don't know the class.

It sounds like you're using the 'prototype' pattern to define game entity types / configs, so I'd like to humbly suggest using Rebirth as this is exactly the kind of thing that it's designed to handle.

You can then specify not just the actual class/type in the data but also the parameters for any given template, and as a bonus you can make changes to your data files and see them reflected in your game without needing a restart. If you give an example of how you're defining and using your template classes/objects I could show you how it would translate.

deep copy or shallow copy? Most of the time you need some mixture. Deep copy libs etc can use a huge amount of memory really fast, because a large part of the object tree is copied "bit for bit" when perhaps a reference will do.

Generally you have to think through the ramifications of new instance vers reference for each field. I tend to have a copy constructor in my classed that need it and use that (work up the class hierarchy too). But it is very class specific as it needs to be (Its like this is all langs, some default which is rarely what you want).

I have no special talents. I am only passionately curious.--Albert Einstein

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org