What is an efficient and refined way of loading and handling assets?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • EARNEST
    New Member
    • Feb 2010
    • 128

    What is an efficient and refined way of loading and handling assets?

    Hello everyone,

    Currently I'm developing a small project, a game and it is almost done, but now, I would like to restructure it, develop an appropriate style of programming in game dev. industry.
    I'd like to improve my knowledge in concurrency, multi-threading and networking, so these are the plans for the [near] future. As of now, I would like to ask for your help and advise.
    How should I structure and handle my assets?
    Here is my current code that loads certain elements :

    Code:
            #region constants for Explosion
    
            const string _path_Expl_0 = _path_Explosion + "0/";
            const string _path_Expl_1 = _path_Explosion + "1/";
            const string _path_Expl_2 = _path_Explosion + "2/";
    
            const string _baseName_Expl_0 = _path_Expl_0 + "DES_";
            const string _baseName_Expl_1 = _path_Expl_1 + "ExplC_";
            const string _baseName_Expl_2 = _path_Expl_2 + "Bomb_";
    
            #endregion        private void LoadExplosion()
            {
                //asteroid explosion
                for (int i = 0; i < _numOfAsteroids; i++)
                {
                    _gameObject.Space.Asteroid.Multiple[i].Explosion = new GameEntity(LoadTexture2D(_path_Expl_1, _baseName_Expl_1, "00", "0"));
                }
    
                //main ship explosion
                _gameObject.Ship.Single.Explosion = new GameEntity(LoadTexture2D(_path_Expl_2, _baseName_Expl_2, "00", "0"));
    
                //enemy ship explosion
                for (int i = 0; i < _numOfEnemies; i++)
                {
                    _gameObject.Enemy.Multiple[i].Explosion = new GameEntity(LoadTexture2D(_path_Expl_0, _baseName_Expl_0, "00", "0"));
                }
            }
    My next idea was to scan appropriate folders and automatically load assets in respective order.
    I was given an advice to use content pipe-lining, regular expressions, something like this

    Code:
    strStarfieldAssets = config.GetStarfieldAssetName(level1);
    
    loader.LoadSheet(strStarfieldAssets);
    
    StarfieldAssets
    {
    
    Level=1;
    Count=10;
    Filename=starfield_*.png;
    }
    and encode in the config file. I didn't quite get it. Any one can explain this a bit more details, pls?
    Also, I found some related material online, the XML structure had been mentioned, but I am a bit lost about that.
    Thanks in advance.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    Have you tried using the built in XNA content manager? You just add files directly to your project and then you can use the Content class (I think) to load them in. The downside is they build with your project, so if you ever wanted to support something like modding it wouldn't work (in which case loading at the start out of a folder might be more appropriate).

    I haven't really done too much with it, but my general approach is to use the content manager and then use hash tables to access them.

    I might do something like this...

    Code:
    Hashtable m_spriteBank = new Hashtable();
    
    ...
    
    m_spriteBank["ship"] = Content.Load<Texture2D>("ship");
    
    ...
    
    Ship ship = new Ship(m_spriteBank["ship"], ...);
    
    ...
    I load everything at the start, then pass the reference of the asset to the object when I create it.

    I don't know i this helps you, just sharing my experiences.

    Comment

    • EARNEST
      New Member
      • Feb 2010
      • 128

      #3
      that is what i kinda did. my goal is to automate the process of loading assets. let's say you will have 100 possible different entities in a game with all different textures.
      one way, the way i did, is to have 100 names for each (not very efficient). another way, is to use regex, then xml. and i'm a bit confused about this way. cant understand it

      Comment

      • EARNEST
        New Member
        • Feb 2010
        • 128

        #4
        Should I just serialize my main game entity class and save is as an XML file, and then when I "generate the world", I will scan those files and populate it accordingly to the files?

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          I suppose you could do that, though that's definitely an interesting approach. You're suggesting that your main class be able to load via an XML file, right? Would you be serializing the assets themselves directly in the XML file or will you serialize it with a file name?

          I'm not sure what you're asking here... if you're asking if you can do it, I'm sure you can. You can do a lot of things... the problem you've defined here has many solutions and I'm sure many of us do it in a variety of ways :D

          Honestly, as I said, my personal approach is to use the XNA ContentManager to load everything in. If things get large I might use XML files to define my objects, but it's generally not a serialization of my class because that's a bit more information than I need.

          When I use XML files, it's generally something simpler like..

          Code:
          <game_definitions>
            <assets>
              <Texture2D name="ship" src="assets\images\ship.png" />
              <Sound name="explosion1" src="assets\sounds\explosion1.wav" />
              ...
            </assets>
          
            <game_objects>
              <PlayerShip sprite="ship" explosionSound="explosion1" ... />
              ...
            </game_objects>
          </game_definitions>
          I suppose you could make your XML deserializer work from that though. Anyway, like I said, many approaches, you have to decide what works for you. I do think it's worth pointing out here that if you load the bulk of your assets at the beginning, so it doesn't matter terribly much if you do a bit of work at the start.

          Additionally, while it may seem tedious, maintaining a list like I mentioned above provides an easy means of making changes. If you want to change your ship's graphic you can change a line in an XML file and boom, done. You don't have to go digging through code for it. Of course, if you've written your code in such a way that you've got these definitions somewhere nice and clean, that might not be so bad anyway.

          I hope that helps, I'm actually not sure what you're asking here so I'm not sure how much help I'm being. You're asking for an efficient and refined way of loading/handling assets, and that's the only way I really know how. I'm sure there's lots of other ways, but I can only provide my own methods ;)

          Good luck though! What kind of game are you making?

          Comment

          • EARNEST
            New Member
            • Feb 2010
            • 128

            #6
            thanks for your informative answer. the code you provided is something that I am planning to do. also, i was wondering about these large commercial game dev teams and the way they solve this issue. i've never seen an xml file with level/world description, do they use some customized data structure and serialization or what?
            You're suggesting that your main class be able to load via an XML file, right? Would you be serializing the assets themselves directly in the XML file or will you serialize it with a file name?
            I am not sure if I understood you, so, please, correct me if i'm wrong. i will have a "scanner"; let's say there would be 1 xml file, called settings.xml, and 3 other xml files. the settings file would contain information regarding those 3 files (not sure, if it is a good idea, but we'll start from there). and those 3 files would have information [something like your code] of, let's say, one for the ship, one for possible weapons and finally one for planets.
            Code:
            #   <game_objects>
            #     <PlayerShip sprite="ship" explosionSound="explosion1" ... />
            #     ...
            #   </game_objects>
            can you pls explain this part? why would i need it? can't i scan the assets nodes, and populate the world using that? am i missing something? thanks.

            p.s.
            as of now, i am trying to refine my space arcade style game, later on, i will change it so it can reflect certain characteristics of the RPG genre games, and then, the final challenge, make it playable online using the P2P architecture. hopefully, i will manage to finish it, and then, in summer, when I'll have more time to myself, after I find a good job, i would like to start working on my 2.5D game engine with some friends and other people who are interested :)

            Comment

            • GaryTexmo
              Recognized Expert Top Contributor
              • Jul 2009
              • 1501

              #7
              I'm thinking of assets here as sounds, images, and perhaps music for the most part. However in that file I also included a section for game objects, which might be things my game will create at some point.

              So that line for ship, that defines how a ship gets created... I've defined an object definition for how a player ship looks, so in my game, when I create a new player ship, it would know to create it with those settings. That way if I wanted to change the image for the ship (or whatever) I can change it in the XML file. That was more a general idea, I may not do that and just define it in code as well using the asset names. It's just away of defining how objects look/behave via XML instead of doing it in code.

              Regarding the scanner, I'm not sure what you mean now, actually. From what you wrote before I thought you were going to serialize your instantiated game classes to save them out, then load them back in later (which is something you can do certainly). I think we're both confused at this point, but I don't know your game and you do :)

              I'd say start with what you want to accomplish... once you have a clear idea of that, work towards it. From your original post, it sounds like you want a way to manage your images/sounds for your game. Start there and create a resource manager.

              It's worth noting that you can use the Content class to load from a file... I did it once at the very start so you can certainly avoid using the content pipeline if you want. Of course, this also means people can just go change your settings :D

              As for what big game companies use, I think they do a similar thing but it's a custom format. I think Blizzard puts all their assets into a single, huge file and loads that in. I can't remember the name of it, but yea they use that general idea. Valve games too have something similar... there's tools you can get to explore them and see the contents. Me, I like things a little more open... I'd be more inclined to leave all my resources available as is in a folder, but if you're concerned with people modifying them or using your assets, you'd want to protect them a bit.

              Comment

              • EARNEST
                New Member
                • Feb 2010
                • 128

                #8
                So that line for ship, that defines how a ship gets created... I've defined an object definition for how a player ship looks, so in my game, when I create a new player ship, it would know to create it with those settings. That way if I wanted to change the image for the ship (or whatever) I can change it in the XML file. That was more a general idea, I may not do that and just define it in code as well using the asset names. It's just away of defining how objects look/behave via XML instead of doing it in code.
                and why won't you do something like this
                Code:
                GameShip ship = Content.Load<GameShip>("ship.xml");
                ? I'm a bit confused. In assets you define "everything " (every possible content: sound fx, textures etc) and in the game object, you are being more specific or what? if that's the case, i think it is similar to my "idea" of using the settings.xml file (ur game_object's PlayerShip element) that would point to an actual XML file for the ship with textures, vectors etc....am I right?

                Of course, this also means people can just go change your settings :D
                i'm "afraid" of this also :)) but also, i'd like to make it as flexible and adjustable as possible, without compromising the consistency and efficiency.
                thanks again, you've been very helpful :) nice to find another "brother in coding" :P

                Comment

                • GaryTexmo
                  Recognized Expert Top Contributor
                  • Jul 2009
                  • 1501

                  #9
                  Can you load it like that? I honestly don't know... If so, that's really coo actually.

                  Otherwise yes, you're right. That's what I was thinking... have the settings xml file define what comprises a game object.

                  Meh, I figure if the user screws up their game it's their own fault... I like to keep it flexible as well, but that's more for my own purposes. It can be an issue when you have multiplayer though, since people could just go and make your images super sized or otherwise easier to see, etc... Gives them an advantage.

                  No problem at all, there's not enough XNA on these boards haha. Glad to see some folks who use it wandering by.

                  Comment

                  Working...