Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6986255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:47:10+00:00 2026-05-27T18:47:10+00:00

I’ve hit a bit of a stumbling block in my quest to implement object

  • 0

I’ve hit a bit of a stumbling block in my quest to implement object cloning in my game engine.
My goal is to have a cloning system that I don’t have to maintain on a class by class basis, unless the class needs special treatment.

My game engine’s setup revolves around a base class Object2D, which contains some image data in the form of a Texture2D. Well, the long story is that it contains a DisplayObject, which contains a Sprite, which contains a Texture2D. Naturally, other classes e.g. “Player”, “Enemy”, “Projectile”, etc. all derive from the basic Object2D class.

Unfortunately, I found that XNA’s Texture2D class isn’t serializable. Which makes sense, as we wouldn’t want to be duplicating texture data in memory all willy-nilly.

This creates a dilemma for me. I was using deep cloning methods to clone objects, but since it’s not serializable I can no longer do that. I tried just marking the Texture2D [NonSerializable], but this causes problems when I try to draw, as the clone’s texture is null. I wasn’t able to find any hidden trick that allows me to assign it after cloning (like some sort of an “onClone()” method).

So I figured I’d do this. For objects that can’t be deep cloned generically, I implement a “SpecialClone” interface that allows me to specify a clone() method.

However, because the class that can’t be deep cloned generically is the base class, I’m right back where I started: writing a clone method on a class by class basis.

    public static T clone<T>(T obj) {
        if (obj == null) return default(T);

        if (!typeof(T).IsSerializable) {
            if (obj is SpecialClone) {
                object obj2 = ((SpecialClone)obj).clone();
                return (T)obj2;
            } else {
                throw new ArgumentException("Object type is not serializable.", "obj type: " + Type.GetTypeHandle(obj));
            }
        } else {
            return deepClone(obj);
        }
    }

    public static T deepClone<T>(T obj) {
        if (obj == null) return default(T);
        if (typeof(T).IsSerializable) {
            try {
                BinaryFormatter bf = new BinaryFormatter();
                MemoryStream ms = new MemoryStream();
                bf.Serialize(ms, obj);
                ms.Seek(0, SeekOrigin.Begin);
                T obj2 = (T)bf.Deserialize(ms);
                ms.Dispose();
                return obj2;
            }
            catch (Exception e) {
                Console.WriteLine(e);
                return default(T);
            }
        } else {
            Console.WriteLine(typeof(T) + " is not marked as serializable!");
            return default(T);
        }
    }

I’m not the best with C# yet, so I’m not sure if I’m missing some trick that I can use or if this is really how I’ll have to deal with this problem. The main purpose of this cloning is for Object2D types, so it’d be a real pain to have to write clone methods for each new subclass I make.

Is there any way to avoid this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:47:10+00:00Added an answer on May 27, 2026 at 6:47 pm

    you do not want to clone the Texture2d.

    You should consider Textures (and models, wave files, etc…) as a “shared asset”, it could be many megabytes in size. Cloning that would leave you in a world of out-of-memory hurt real quick (in addition to the time it takes to clone)

    your ideal solution: instead, you want to clone the reference.

    if each of your Object2D instances are not meant to be serialized, then you can just reference-assign the same Texture2D to the various Object2D’s. If you want to serialize, then pass the Texture2D reference, as mentioned, plus copy a string textureFilePath, which is what gets serialized.

    Also note that this is what XNA’s ContentManager does for you automatically: if you request “myPic.xnb” 10 times, it will automagically load it once and return that reference to you 10 times.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.