views:

78

answers:

2

Hey guys,

I am currently writing a resource manager for my game. This is basically a class that will handle all kinds of other objects of different types, and each object is referred to by name (a System.String). Now this is my current implementation, but since I am using a dictionary of objects I will still need to cast every object. Is there a way to use Generics in this case? I'm not very strong on those, I tried reading up on them and it just ended up confusing me more.

public static class ResourceManager
{
    public static Dictionary<string, object> Resources { get; private set; }

    public static void LoadResources()
    {
        Resources = new Dictionary<string, object>();

        //Sample resource loading code
        Resources.Add("number", 3);
        Resources.Add("string", "bleh");

        Console.Log("Loaded " + Resources.Count + " resources.");
    }
}
+2  A: 

I imagine those objects all have at least a few things in common. Find that common ground and turn it into an interface that your objects implement. Then use a Dictionary<string, IMyInterface>.

Joel Coehoorn
Actually, those have absolutely nothing in common. The objects are fonts, images and sounds. I would still need to cast them using an interface because I have external methods that will only use those objects.
Xeon06
In that case you want to build something that uses overloaded functions that will work based on the type resolution for the function calls.
Joel Coehoorn
But won't that still require unboxing since I will still store them as objects?
Xeon06
Only value types. In that case, I doubt most of these are primitives and you really should only rarely build your own structs - C# does better with classes.
Joel Coehoorn
+2  A: 

Is there a need to only have one set of resources, or can you have one for each type of resource?

public class ResourceMap<T> extends Dictionary<string, T> { }

public static class ResourceManager
{
    public static ResourceMap<Font> Fonts { get; private set; }
    public static ResourceMap<Image> Images { get; private set; }
    public static ResourceMap<Sound> Sounds { get; private set; }

    public static void LoadResources()
    {
        Fonts = new ResourceMap<Font>();
        Images = new ResourceMap<Image>();
        Sounds = new ResourceMap<Sound>();

        Fonts.Add("Helvetica", new Font("Helvetica", 12, FontStyle.Regular));
        Images.Add("Example", Image.FromFile("example.png"));
    }
}
Samir Talwar
Well in that case I don't really need generics. The idea was to have only one Resource list and using generics to avoid type casting, but I think I got stuff confused here.
Xeon06
Well, you could avoid the `ResourceMap` class altogether and just use `Dictionary`—perhaps that would be more readable after all. The question is, do you need to be able to grab resources without knowing which type they are? I can't think of such a scenario, personally.
Samir Talwar
No, what I want to do is be able to access the resources using only one property/method, even though those resources can be of different types.
Xeon06
In that case, you'll need to cast at some point. Methods can only return values of a specific type: if you want to return different types of objects, you'll need to use their common superclass, which means you'll need to cast back down when you want to use the object for its intended purpose.
Samir Talwar