[SOLVED]Custom ContentTypeReader: existingInstance

Can someone explain usage of parameter ‘exisitng instance’ of this method:

public abstract class ContentTypeReader<T> : ContentTypeReader {
    protected internal abstract T Read(ContentReader input, T existingInstance);
}

I’ve several custom content readers in my project but I never use this parameter and it’s always null. I can’t find any info online(MSDN page for this method returns 404).

It’s used to reload resources after a Device lost.
If existingInstance is not null, you must reload the asset into existingInstance instead if creating a new one.

So for example I have simple class:

public class TileMap {
    private Dictionary<int, TileBrush> _tileBrushes;
    
    public Texture2D SpriteSheet { get; }
    public ReadOnlyDictionary<int, TileBrush> TileBrushes { get;  } // TileBrush is custom content
  
 
    public TileMap(Texture2D spriteSheet, IEnumerable<TileBrush> tileBrushes) {
        this._tileBrushes = tileBrushes.ToDictionary(x => x.Id);
        this.TileBrushes = new ReadOnlyDictionary<int, TileBrush>(this._tileBrushes);
    }
} 

So in case you described(Device lost) I have to load SpriteSheet and TileBrushes again? This will force public setters for all properties and collection type change.

AFAIK, device lost is not implemented in most platforms and existingInstance is ignored by monogame core resources (Texture/VertexBuffer/Model).

Therefore you can throw an exception for now and figure it out whenever it becomes relevant.

1 Like

Thank you. This solves the problem :slight_smile: