Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6721233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:18:19+00:00 2026-05-26T09:18:19+00:00

For my game, I’m making a resource class which manager resources likes textures, sounds,

  • 0

For my game, I’m making a resource class which manager resources likes textures, sounds, fonts, etc. I made this ImageList class, which automatically loads textures during runtime as they’re needed.

class TextureList
{
    private Dictionary<string, Texture> _list;

    public Texture this[string name]
    {
        get
        {
            Texture tex;
            bool success = List.TryGetValue(name, out tex);

            if (!success)
            {
                tex = Resources.LoadImage(name);
                List[name] = tex;
            }

            return tex;
        }
    }

    protected Dictionary<String, Texture> List
    {
        get
        {
            return _list ?? new Dictionary<String, Texture>();
        }
    }
}

Now I want to make to make this class generic, so that I can make a FontList or SoundList class without the hassle of changing every Texture to Sound or Font (and writing lots of duplicate code). Anyone willing to help me out on making a generic version of this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:18:19+00:00Added an answer on May 26, 2026 at 9:18 am
    1. Make your class generic
    2. In the constructor, you must pass a Func where T is your generic argument.
    3. The logic goes: When you do not find the item, the passed in Func is called and the result stored in the internal list.

    so, your class becomes

    var x = new Cachinglist<Texture>(s => Resources.LoadImage(s));
    

    or

    class CachingList<T>
    {
        private readonly Dictionary<string, T> _list = new Blabla..();
        private readonly Func<string,T> _itemsFactory;
    
        public CachingList(Func<string,T> itemsFactory) {
          _itemsFactory = itemsFactory;
        }
    
        public T this[string name]
        {
            get
            {
                T t;
    
                if (!_list.TryGetValue(name, out t))
                {
                    t = _itemsFactory(name);
                    _list[name] = t;
                }
    
                return t;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My game normally uses GL_TEXTURE_RECTANGLE_EXT to draw 2D textures. However, since this isn't supported
i am making a game using andengine in android, in this game various sprites(eggs)
Well I made this game in java and when you shoot a bullet it
Game.prototype.run = function() { window.setInterval(function() { var thisLoop = new Date().getTime(); this.update(); this.render(); lastLoop
I have a game which displays an array of colored blocks. The user can
Making game of life I need to a have a grid that is 30x20
Project : Game in Flex builder 4 (Actionscript Project) Language : Actionscript 3 This
I made a game for symbian^3 with QML . and I stumbled upon one
I have a game, stripped down for the problem, a GameHandler class, a Game
I'm writing a game in C using the Gtk libraries, in which the player

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.