Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Roof Top Pew Wee

Clone () is for creating a new object, but what if the object is already created?

This topic is 5334 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Still a little new to C#, so forgive me if this is a dumb question. I have an object Camera which I would like to copy to another camera. I''m just wondering if there is a specific way to do a by value copy from one camera to another, like camera1 = camera2; would do in C++, rather than a reference copy. Looked into the Clone() method, but my understanding is that is used when creating a new object. Would the best method be to write a method like: public void SetValuesTo(Camera cameraToSet) { // set all of the variables of cameraToSet to this.variables } Just curious if there is a standard way of doing this in C#. --Vic-- The future of 2D game development: Flat Red Ball

Share this post


Link to post
Share on other sites
Advertisement
camera2 = camera1.Clone();

Maybe I am just groggy but it seems like that is what you want. The ''old'' values of camera2 are discarded like they would be in a C++ deep-copy.

Share this post


Link to post
Share on other sites
That''s just the thing. In your example, antareus, camera2 would be sent to garbage collection and that basically sets camera2 to a new one.

For exmaple, say I have the following:


Camera camera1, camera2, camera3;

camera1 = new Camera();
camera2 = new Camera();
camera3 = camera1; // so changes in camera 1 are reflected in camera 3


camera1.x = 1;
camera2.x = 2;
// camera3.x should be 1


camera1 = camera2.Clone();
// at this point, if I understand things correctly, camera1.x will be 2, but camera3.x will still be 1 instead of 2, like it should be.




So you can see, I don''t want to trash the old camera. Just override the values without getting rid of that memory, so that my references aren''t broken.


--Vic--

The future of 2D game development:
Flat Red Ball

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!