Skip to main content
broken link fixed, cf. https://meta.stackoverflow.com/a/406565/4751173
Source Link

update

After a bit of a chat with Kikaimaru, I realized the deep copy wont work as seamlessly as I thought. Too bad, I thought it was very elegant. The methods below will still work though.


If you are still set on doing it, try something like this. Just overload the method telling it to cache or not. I pretty much snagged this from this blogfrom this blog, and he goes into more detail there. You might find it useful should you decide to go this way.

public override T Load<T>(string assetName)
{
    return Load<T>(assetName, true);
}

public T Load<T>(strng assetName, bool cache)
{
    if (loadedAssets.ContainsKey(assetName))
        return (T)loadedAssets[assetName];

    T asset = ReadAsset<T>(assetName, null);

    if(cache)
        loadedAssets.Add(assetName, asset);

    return asset;
}

I had another thought. What you could do it something along the lines of creating an interface to make all your non-cacheable object implement. Have the load manager look for that, and don't cache if it is found. You could accomplish the same thing with an attribute if you wanted.

public interface INoCache{} //no member here, just empty...Maybe a bool "ShouldCache" if you want...

public class Destructible : INoCache

//In your ContentManager
public T Load<T>(string assetName)
{
    if (loadedAssets.ContainsKey(assetName))
        return (T)loadedAssets[assetName];

    T asset = ReadAsset<T>(assetName, null);

    //this logic seems a bit backwards, but 
    //it makes for a little cleaner code IMHO
    if(!(asset is INoCache))
        loadedAssets.Add(assetName, asset);

    return asset;
}

update

After a bit of a chat with Kikaimaru, I realized the deep copy wont work as seamlessly as I thought. Too bad, I thought it was very elegant. The methods below will still work though.


If you are still set on doing it, try something like this. Just overload the method telling it to cache or not. I pretty much snagged this from this blog, and he goes into more detail there. You might find it useful should you decide to go this way.

public override T Load<T>(string assetName)
{
    return Load<T>(assetName, true);
}

public T Load<T>(strng assetName, bool cache)
{
    if (loadedAssets.ContainsKey(assetName))
        return (T)loadedAssets[assetName];

    T asset = ReadAsset<T>(assetName, null);

    if(cache)
        loadedAssets.Add(assetName, asset);

    return asset;
}

I had another thought. What you could do it something along the lines of creating an interface to make all your non-cacheable object implement. Have the load manager look for that, and don't cache if it is found. You could accomplish the same thing with an attribute if you wanted.

public interface INoCache{} //no member here, just empty...Maybe a bool "ShouldCache" if you want...

public class Destructible : INoCache

//In your ContentManager
public T Load<T>(string assetName)
{
    if (loadedAssets.ContainsKey(assetName))
        return (T)loadedAssets[assetName];

    T asset = ReadAsset<T>(assetName, null);

    //this logic seems a bit backwards, but 
    //it makes for a little cleaner code IMHO
    if(!(asset is INoCache))
        loadedAssets.Add(assetName, asset);

    return asset;
}

update

After a bit of a chat with Kikaimaru, I realized the deep copy wont work as seamlessly as I thought. Too bad, I thought it was very elegant. The methods below will still work though.


If you are still set on doing it, try something like this. Just overload the method telling it to cache or not. I pretty much snagged this from this blog, and he goes into more detail there. You might find it useful should you decide to go this way.

public override T Load<T>(string assetName)
{
    return Load<T>(assetName, true);
}

public T Load<T>(strng assetName, bool cache)
{
    if (loadedAssets.ContainsKey(assetName))
        return (T)loadedAssets[assetName];

    T asset = ReadAsset<T>(assetName, null);

    if(cache)
        loadedAssets.Add(assetName, asset);

    return asset;
}

I had another thought. What you could do it something along the lines of creating an interface to make all your non-cacheable object implement. Have the load manager look for that, and don't cache if it is found. You could accomplish the same thing with an attribute if you wanted.

public interface INoCache{} //no member here, just empty...Maybe a bool "ShouldCache" if you want...

public class Destructible : INoCache

//In your ContentManager
public T Load<T>(string assetName)
{
    if (loadedAssets.ContainsKey(assetName))
        return (T)loadedAssets[assetName];

    T asset = ReadAsset<T>(assetName, null);

    //this logic seems a bit backwards, but 
    //it makes for a little cleaner code IMHO
    if(!(asset is INoCache))
        loadedAssets.Add(assetName, asset);

    return asset;
}
Commonmark migration
Source Link

###update###

update

After a bit of a chat with Kikaimaru, I realized the deep copy wont work as seamlessly as I thought. Too bad, I thought it was very elegant. The methods below will still work though.


If you are still set on doing it, try something like this. Just overload the method telling it to cache or not. I pretty much snagged this from this blog, and he goes into more detail there. You might find it useful should you decide to go this way.

public override T Load<T>(string assetName)
{
    return Load<T>(assetName, true);
}

public T Load<T>(strng assetName, bool cache)
{
    if (loadedAssets.ContainsKey(assetName))
        return (T)loadedAssets[assetName];

    T asset = ReadAsset<T>(assetName, null);

    if(cache)
        loadedAssets.Add(assetName, asset);

    return asset;
}

I had another thought. What you could do it something along the lines of creating an interface to make all your non-cacheable object implement. Have the load manager look for that, and don't cache if it is found. You could accomplish the same thing with an attribute if you wanted.

public interface INoCache{} //no member here, just empty...Maybe a bool "ShouldCache" if you want...

public class Destructible : INoCache

//In your ContentManager
public T Load<T>(string assetName)
{
    if (loadedAssets.ContainsKey(assetName))
        return (T)loadedAssets[assetName];

    T asset = ReadAsset<T>(assetName, null);

    //this logic seems a bit backwards, but 
    //it makes for a little cleaner code IMHO
    if(!(asset is INoCache))
        loadedAssets.Add(assetName, asset);

    return asset;
}

###update###

After a bit of a chat with Kikaimaru, I realized the deep copy wont work as seamlessly as I thought. Too bad, I thought it was very elegant. The methods below will still work though.


If you are still set on doing it, try something like this. Just overload the method telling it to cache or not. I pretty much snagged this from this blog, and he goes into more detail there. You might find it useful should you decide to go this way.

public override T Load<T>(string assetName)
{
    return Load<T>(assetName, true);
}

public T Load<T>(strng assetName, bool cache)
{
    if (loadedAssets.ContainsKey(assetName))
        return (T)loadedAssets[assetName];

    T asset = ReadAsset<T>(assetName, null);

    if(cache)
        loadedAssets.Add(assetName, asset);

    return asset;
}

I had another thought. What you could do it something along the lines of creating an interface to make all your non-cacheable object implement. Have the load manager look for that, and don't cache if it is found. You could accomplish the same thing with an attribute if you wanted.

public interface INoCache{} //no member here, just empty...Maybe a bool "ShouldCache" if you want...

public class Destructible : INoCache

//In your ContentManager
public T Load<T>(string assetName)
{
    if (loadedAssets.ContainsKey(assetName))
        return (T)loadedAssets[assetName];

    T asset = ReadAsset<T>(assetName, null);

    //this logic seems a bit backwards, but 
    //it makes for a little cleaner code IMHO
    if(!(asset is INoCache))
        loadedAssets.Add(assetName, asset);

    return asset;
}

update

After a bit of a chat with Kikaimaru, I realized the deep copy wont work as seamlessly as I thought. Too bad, I thought it was very elegant. The methods below will still work though.


If you are still set on doing it, try something like this. Just overload the method telling it to cache or not. I pretty much snagged this from this blog, and he goes into more detail there. You might find it useful should you decide to go this way.

public override T Load<T>(string assetName)
{
    return Load<T>(assetName, true);
}

public T Load<T>(strng assetName, bool cache)
{
    if (loadedAssets.ContainsKey(assetName))
        return (T)loadedAssets[assetName];

    T asset = ReadAsset<T>(assetName, null);

    if(cache)
        loadedAssets.Add(assetName, asset);

    return asset;
}

I had another thought. What you could do it something along the lines of creating an interface to make all your non-cacheable object implement. Have the load manager look for that, and don't cache if it is found. You could accomplish the same thing with an attribute if you wanted.

public interface INoCache{} //no member here, just empty...Maybe a bool "ShouldCache" if you want...

public class Destructible : INoCache

//In your ContentManager
public T Load<T>(string assetName)
{
    if (loadedAssets.ContainsKey(assetName))
        return (T)loadedAssets[assetName];

    T asset = ReadAsset<T>(assetName, null);

    //this logic seems a bit backwards, but 
    //it makes for a little cleaner code IMHO
    if(!(asset is INoCache))
        loadedAssets.Add(assetName, asset);

    return asset;
}
replaced http://gamedev.stackexchange.com/ with https://gamedev.stackexchange.com/
Source Link

###update###

After a bit of a chat with KikaimaruKikaimaru, I realized the deep copy wont work as seamlessly as I thought. Too bad, I thought it was very elegant. The methods below will still work though.


If you are still set on doing it, try something like this. Just overload the method telling it to cache or not. I pretty much snagged this from this blog, and he goes into more detail there. You might find it useful should you decide to go this way.

public override T Load<T>(string assetName)
{
    return Load<T>(assetName, true);
}

public T Load<T>(strng assetName, bool cache)
{
    if (loadedAssets.ContainsKey(assetName))
        return (T)loadedAssets[assetName];

    T asset = ReadAsset<T>(assetName, null);

    if(cache)
        loadedAssets.Add(assetName, asset);

    return asset;
}

I had another thought. What you could do it something along the lines of creating an interface to make all your non-cacheable object implement. Have the load manager look for that, and don't cache if it is found. You could accomplish the same thing with an attribute if you wanted.

public interface INoCache{} //no member here, just empty...Maybe a bool "ShouldCache" if you want...

public class Destructible : INoCache

//In your ContentManager
public T Load<T>(string assetName)
{
    if (loadedAssets.ContainsKey(assetName))
        return (T)loadedAssets[assetName];

    T asset = ReadAsset<T>(assetName, null);

    //this logic seems a bit backwards, but 
    //it makes for a little cleaner code IMHO
    if(!(asset is INoCache))
        loadedAssets.Add(assetName, asset);

    return asset;
}

###update###

After a bit of a chat with Kikaimaru, I realized the deep copy wont work as seamlessly as I thought. Too bad, I thought it was very elegant. The methods below will still work though.


If you are still set on doing it, try something like this. Just overload the method telling it to cache or not. I pretty much snagged this from this blog, and he goes into more detail there. You might find it useful should you decide to go this way.

public override T Load<T>(string assetName)
{
    return Load<T>(assetName, true);
}

public T Load<T>(strng assetName, bool cache)
{
    if (loadedAssets.ContainsKey(assetName))
        return (T)loadedAssets[assetName];

    T asset = ReadAsset<T>(assetName, null);

    if(cache)
        loadedAssets.Add(assetName, asset);

    return asset;
}

I had another thought. What you could do it something along the lines of creating an interface to make all your non-cacheable object implement. Have the load manager look for that, and don't cache if it is found. You could accomplish the same thing with an attribute if you wanted.

public interface INoCache{} //no member here, just empty...Maybe a bool "ShouldCache" if you want...

public class Destructible : INoCache

//In your ContentManager
public T Load<T>(string assetName)
{
    if (loadedAssets.ContainsKey(assetName))
        return (T)loadedAssets[assetName];

    T asset = ReadAsset<T>(assetName, null);

    //this logic seems a bit backwards, but 
    //it makes for a little cleaner code IMHO
    if(!(asset is INoCache))
        loadedAssets.Add(assetName, asset);

    return asset;
}

###update###

After a bit of a chat with Kikaimaru, I realized the deep copy wont work as seamlessly as I thought. Too bad, I thought it was very elegant. The methods below will still work though.


If you are still set on doing it, try something like this. Just overload the method telling it to cache or not. I pretty much snagged this from this blog, and he goes into more detail there. You might find it useful should you decide to go this way.

public override T Load<T>(string assetName)
{
    return Load<T>(assetName, true);
}

public T Load<T>(strng assetName, bool cache)
{
    if (loadedAssets.ContainsKey(assetName))
        return (T)loadedAssets[assetName];

    T asset = ReadAsset<T>(assetName, null);

    if(cache)
        loadedAssets.Add(assetName, asset);

    return asset;
}

I had another thought. What you could do it something along the lines of creating an interface to make all your non-cacheable object implement. Have the load manager look for that, and don't cache if it is found. You could accomplish the same thing with an attribute if you wanted.

public interface INoCache{} //no member here, just empty...Maybe a bool "ShouldCache" if you want...

public class Destructible : INoCache

//In your ContentManager
public T Load<T>(string assetName)
{
    if (loadedAssets.ContainsKey(assetName))
        return (T)loadedAssets[assetName];

    T asset = ReadAsset<T>(assetName, null);

    //this logic seems a bit backwards, but 
    //it makes for a little cleaner code IMHO
    if(!(asset is INoCache))
        loadedAssets.Add(assetName, asset);

    return asset;
}
deleted 920 characters in body
Source Link
user159
user159
Loading
added 802 characters in body
Source Link
user159
user159
Loading
added 576 characters in body
Source Link
user159
user159
Loading
Source Link
user159
user159
Loading