• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
dechorus

Dictionary of Dictionaries based on Type

8 posts in this topic

Hey guys. I'm working in XNA.

 

I'm basically trying to have a Data/Asset Manager class that holds everything that's loaded in by the ContentManager.

 

I'm trying to have a property that's essentially something along the lines of

 

 

Dictionary<T, Dictionary<string, T>> listOfObjects;

 

 

My hope is to be able to get objects doing something like

 

 

Texture2D texture = listOfObjects<Texture2D>("nameOfTexture");

SpriteFont font = listOfObjects<SpriteFont>("nameOfFont");

// etc. 

 

 

Any help and / or suggestions? Thank you so much in advance smile.png

Edited by dechorus
0

Share this post


Link to post
Share on other sites

I'm not super familiar with .Net's Dictionary type, as its been awhile since I've used it, but while its entirely possible to have a dictionary of dictionaries, I don't think that your intention can be expressed in the way you are attempting because it would make the value-type dependent upon the index type, unless .Net generics work even more differently than C++ templates than I think they do.

 

More to the point though, why do you want to be able to determine the appropriate list of homogenous objects from the type of its value? In practice, one needs to statically know the value type anyhow, in order to store or reference it, so I don't get how this extra contortion is meant to be useful. In other words, whats wrong with listOfTextures("nameOfTexture"), and listOfFonts(""nameOfFont)?

 

There are ways to achieve exactly what your post lays out, but you sacrifice some type safety in doing so, which is generally frowned upon if reasonable solutions avoid it.

0

Share this post


Link to post
Share on other sites

My initial implementation is in fact having multiple dictionaries for each type, which is absolutely fine.

 

I suppose the reason I wanted it this way was so that the class could be generic enough to be used across projects.

 

If I required a dictionary of objects using a class that was specific to a particular game, such as a Level class, then the list would be generic enough for me to Add and Get levels, as opposed to adding a new dictionary for Levels which would be specific to a game project.

 

Does that make sense? 

 

Thank you for your response!

0

Share this post


Link to post
Share on other sites
T itself needs to be a concrete type for the case you're using it in, for example:
 
public static class AssetManager
{
  public static Dictionary<string,Texture2D> Textures;
  public static Dictionary<string,SpriteFont> Fonts;
}
 
elsewhere:
{
  var texture = AssetManager.Textures["nameOfTexture"];
}
 
(my use of static here is optional and not required if you want an instance-based AssetManager)


You can put dictionaries of different types inside one big dictionary, but it's uglier than it needs to be:
public static class AssetManager
{
  static Dictionary<Type,object> dict;

  T GetAssetByType<T>(string key) where T:class
  {
    object o;
    if (dict.TryGetValue(typeof(T), out o))
    {
      var nestedDict = o as Dictionary<string,T>;
      if (nestedDict != null)
        return nestedDict[key];
    }    

    return null;
  }
}

elsewhere:
{
  var texture = AssetManager.GetAssetByType<Texture2D>("nameOfTexture");
}
Another option is to just have the AssetManager itself be generic. Static generics get a separate static definition per generic type.
public static class AssetManager<T> where T:class // or the base class of XNA assets, if you want
{
  public static Dictionary<string,T> Assets;
}

elsewhere:
{
  var texture = AssetManager<Texture2D>.Assets["nameOfTexture"];
}
Edited by Nypyren
0

Share this post


Link to post
Share on other sites

What's your end goal exactly?

 

My hope is to be able to get objects doing something like
 
 
Texture2D texture = listOfObjects<Texture2D>("nameOfTexture");

SpriteFont font = listOfObjects<SpriteFont>("nameOfFont");

// etc. 

 

That's exactly what XNA's ContentManager does.

 

Texture2D texture = Content.Load<Texture2D>("nameOfTexture");

SpriteFont font = Content.Load<SpriteFont>("nameOfFont");

 

Why not just use that?

0

Share this post


Link to post
Share on other sites

I would propose that you are attempting to encapsulate asset management at the wrong level by putting all asset types into one container, and that encapsulating at the level of individual resource types, as in your original solution, is indeed the correct course of action. At some point, your game needs to know that its requesting a level, loading a level, getting a level in return. Your original solution is already generic enough to take any asset type (or should be), albeit in separate containers that may be game specific, so its eminently re-usable for textures, levels, or anything else.

 

Your game already needs to know that either:

  • I ask a container to tell me about another container that holds levels, then I ask that container about the level I want.

-or-

  • I have a container of textures, levels, and other things, I know which one I'm dealing with now, I can ask the right container about the level I want directly.

 

Again, there are ways to implement your alternative proposal by eschewing some type safety -- you can probably even do it in a way where all the type un-safety is internal to the asset manager class -- but you will still, at some level, be effectively instantiating a set of functionality specific to an asset type that is separate from like functionality of other asset types. This alternative implementation becomes nothing more than a logical firewall between you and the implementation you think you'll be avoiding.

 

Setting that aside, there are benefits to keeping asset managers of different types separate -- for example, your caching strategy for sounds, textures, and levels may all be very different from one another. Putting them all in one super-container makes using different strategies for different types not only more difficult, but creates an unnecessary level of decoupling (usually, decoupling is a good thing, but here it would be 1 step too far removed).

0

Share this post


Link to post
Share on other sites

What's your end goal exactly?

 

 

My hope is to be able to get objects doing something like
 
 
Texture2D texture = listOfObjects<Texture2D>("nameOfTexture");

SpriteFont font = listOfObjects<SpriteFont>("nameOfFont");

// etc. 

 

That's exactly what XNA's ContentManager does.

 

Texture2D texture = Content.Load<Texture2D>("nameOfTexture");

SpriteFont font = Content.Load<SpriteFont>("nameOfFont");

 

Why not just use that?

 

The ContentManager is used to Load objects, not get them once they've been loaded. I don't think I'd be creating new variables for each and every texture and so on. A Dictionary seems more appropriate.

 

As for the generic solution I was after, I've come up with this:

 

        Dictionary<Type, object> dict;

        public void Add<T>(string key, T value)
        {
            Type type = typeof(T);
            bool dictContainsKey = dict.ContainsKey(type);

            if (!dictContainsKey)
                dict.Add(type, new Dictionary<string, T>());

            Dictionary<string, T> nestedDict = dict[type] as Dictionary<string, T>;

            if (nestedDict.ContainsKey(key))
                nestedDict[key] = value;
            else
                nestedDict.Add(key, value);
        }

        public T Get<T>(string key)
        {
            Type type = typeof(T);
            bool dictContainsKey = dict.ContainsKey(type);

            if (!dictContainsKey)
                return default(T);

            Dictionary<string, T> nestedDict = dict[type] as Dictionary<string, T>;

            if (nestedDict.ContainsKey(key))
                return nestedDict[key];
            else
                return default(T);
        }

What are your thoughts - is this a bad / unnecessary implementation?

 

Admittedly it's a little messy, and probably unnecessary. For whatever reason I felt a single Dictionary with all assets is the way to go. My plan was to then write getters and setters and so on for accessing specific types, such as GetTexture(string key) { return // ... gets Texture2D from nested dictionary in dict }

 

Anyway, I guess having Dictionaries for each type seems to be a more suitable and cleaner solution.

 

Thanks everybody for your input.

0

Share this post


Link to post
Share on other sites

Personally i use somthing like this:

 

 

public Dictionary<string, object> _AssetDictionary;


public void LoadAssets()
{
    _AssetDictionary = new Dictionary<string, object>();
    _AssetDictionary.Add("FONT_MENUFONT", LoadSpritefont(MenuFont));
    _AssetDictionary.Add("ASSET_AWESOMETEXTURE", LoadTexture(MenuFont));
}
public T Get<T> GetAsset(string name)
{
    return (T)_AssetDictionary[name];
}

//Reference with
GetAsset<Texture2D>("ASSET_AWESOMETEXTURE");

ofc string can be replaced with anything, even object :P so you have have it stored by different enum values.

 

There are many ways to expand on this, but strings suffice for most things.

Edited by Andy474
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0