Texture2D in SaveGame

Hi there,
I am building an Save/Load functionality in my game using the XMLSerializer as described here https://msdn.microsoft.com/en-us/library/bb203924.aspx

The thing is, I (of course) also want to save the players inventory. The inventory is a collection of items where each item has some attributes like name, value and an icon that is beeing displayed in the inventory. This icon is saved as a Texture2D

So my example SaveDate might look like this:

public struct SaveGameData
    public Vector2 PlayerPosition;
    public List<Item> Inventory;

The XMLSerializer is beeing called like this:
XmlSerializer serializer = new XmlSerializer(typeof(SaveGameData), new Type[] { typeof(Item) });
(I use Type[ ] here becauce there are more types to come)

Now the save crashes because Texture2D can not be serialized due to a missing default constructor.

How do I deal with that? Is there a workaround or am I n the wrong way using this kind of saving?

you cannot save a texture2d since it’s stored on the GPU instead of the RAM.

But you wouldn’t want to do that anyways, since that is not “new information” so to say, it’s not something that has to be saved (rather you just want to save the reference)

What I would rather do is

have a class that stores all texture2ds of the icons.

So for example

public class IconManager
public Texture2D IconStar;


and load the textures from there.

Then in IconManager you also make an enumerable like this
public Enum IconType { Star, Stripe, etc. }

In your inventory item class you only store this icontype for the specific inventory item.

So for example

public IconManager.IconType Icon

Then, you need a function inside your Iconmanager that returns a texture2d based on the input of IconType

So you basically just make a switch function and return the correct icon texture.

When you draw your icon you can use this function to draw the correct icon for each inventory item.

I hope I helped, but it’s just one of hundreds of possible solutions.

1 Like

Yeah, already thought that I need to do some kind of mapping instead. Should not be a big deal, since those icons already have an unique key corresponding to the actual image-file.

Thanks :slight_smile:

Actually you can pretty easily save Texture2D by using the GetData and SetData methods and just saving/loading the color array, tho I would recommend you only use it in case you made modifications to the texture during runtime, otherwise you are just wasting performance.

Why don’t you just save the id of the item to your save file. Should be better if you have a list of items which you are loading before.

Saving textures is possible too, but not cool in your case.
I’m saving textures in my map files, i like it more to have 1 map file instead of the map file+lots of textures.

To just not save the texture just tell the xmlserialize to ignore it.

XmlIgnore Instructs the Serialize method of the XmlSerializer not to serialize the public field or public read/write property value

NonSerialized Indicates that a field of a serializable class should not be serialized.

As for textures in the case crazy states that might be a bit tougher. In the opposite case what stealth said sounds reasonable and you could serialize that id.

Alternatively if required say you had a paint program or so, i suppose you could save it to disk separately and load it from stream. Im not sure if this still works or if it works on every platform.

were t is the texture.

FileStream fs = new FileStream(fullFilePath, FileMode.Create);
t.SaveAsPng(fs, t.Width, t.Height);

Thank you for your answers.
I solved - like most of you suggested - with not storing the texture but its ID (which good enough).
The Texture2D property in the item class is marked with [XmlIgnore].