Jump to content

Java - clone() method


Ronan

Recommended Posts

Hey there,

 

Well - getting forward with my game, but I'm finding I'm in need of 'cloning' objects, as such; reusing all the data from one object in another one, in a similar fashion to the 'ByVal' implementations in other languages.

 

However, I've never looked into this in Java before. With a bit of searching around, it seems that implementing your own clone method is the most viable solution, but I'm wondering if anyone else has more experience in this area and some potential solutions. :)

Link to comment
Share on other sites

I would say that using your own clone() method is probably a good idea. Just make so clone() makes so that that the correct properties are made unique or reset so that you can differentiate between objects.

C2b6gs7.png

Link to comment
Share on other sites

Hmm, I'm not sure exactly what you're trying to do (never used byVal) but couldn't you just do something like this?

 

Object a (property a, property b, property c); //object you want to clone

 

to clone:

 

Object b(a.getProperty(a), a.getProperty(b), a.getProperty(c));

 

Unless I'm not understanding what you want to do that seems like an easy way to do it.... If you wanted a unique object ID or something you could do

 

a(1, b, c);

 

b(1+a.getProperty(id);

polvCwJ.gif
"It's not a rest for me, it's a rest for the weights." - Dom Mazzetti

Link to comment
Share on other sites

That seems like a pretty roundabout way of doing it IMO... clone methods are easy to write and make for more readable code.

Wouldn't a clone method basically just contain the same thing?

polvCwJ.gif
"It's not a rest for me, it's a rest for the weights." - Dom Mazzetti

Link to comment
Share on other sites

Quite true dear sir.

 

I shall now attempt to write such a method being extremely bored at work..

 

EDIT: Never mind, I won't, I forgot I don't have Java :(

polvCwJ.gif
"It's not a rest for me, it's a rest for the weights." - Dom Mazzetti

Link to comment
Share on other sites

Thanks for the replies! :) I did indeed end up just writing my own clone methods, they're surprisingly easy with most classes actually, certainly more so than I had imagined.

 

I was rather surprised that there was no 'built-in' way to easily get another 'clone' of an object in java without writing your own method actually, but then I suppose that it's rarely needed, so writing a few methods yourself is hardly any trouble.

 

If interested, currently the area that it's being utilised in is within levels of my game. Once all the levels have been completed, large modifications have been made to the objects that the level is comprised of so resetting the level involves changing all the objects back to their original state. Doing so via cloning is a relatively easy solution. Whenever a new level is created, a clone is placed in an ArrayList which contains all levels in their 'initial' state, allowing me to replace a modified level with a clone of their initial-state counterpart whenever it should be played again.

 

Another option was to define a structure which would contain all the objects at their initial state of the level and create a constructor for a level based on the structure - again, would involve cloning too.

Link to comment
Share on other sites

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.