473,320 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

EARNEST
128 100+
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 :

Expand|Select|Wrap|Line Numbers
  1.         #region constants for Explosion
  2.  
  3.         const string _path_Expl_0 = _path_Explosion + "0/";
  4.         const string _path_Expl_1 = _path_Explosion + "1/";
  5.         const string _path_Expl_2 = _path_Explosion + "2/";
  6.  
  7.         const string _baseName_Expl_0 = _path_Expl_0 + "DES_";
  8.         const string _baseName_Expl_1 = _path_Expl_1 + "ExplC_";
  9.         const string _baseName_Expl_2 = _path_Expl_2 + "Bomb_";
  10.  
  11.         #endregion        private void LoadExplosion()
  12.         {
  13.             //asteroid explosion
  14.             for (int i = 0; i < _numOfAsteroids; i++)
  15.             {
  16.                 _gameObject.Space.Asteroid.Multiple[i].Explosion = new GameEntity(LoadTexture2D(_path_Expl_1, _baseName_Expl_1, "00", "0"));
  17.             }
  18.  
  19.             //main ship explosion
  20.             _gameObject.Ship.Single.Explosion = new GameEntity(LoadTexture2D(_path_Expl_2, _baseName_Expl_2, "00", "0"));
  21.  
  22.             //enemy ship explosion
  23.             for (int i = 0; i < _numOfEnemies; i++)
  24.             {
  25.                 _gameObject.Enemy.Multiple[i].Explosion = new GameEntity(LoadTexture2D(_path_Expl_0, _baseName_Expl_0, "00", "0"));
  26.             }
  27.         }
  28.  
  29.  
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

Expand|Select|Wrap|Line Numbers
  1. strStarfieldAssets = config.GetStarfieldAssetName(level1);
  2.  
  3. loader.LoadSheet(strStarfieldAssets);
  4.  
  5. StarfieldAssets
  6. {
  7.  
  8. Level=1;
  9. Count=10;
  10. Filename=starfield_*.png;
  11. }
  12.  
  13.  
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.
Feb 8 '11 #1

✓ answered by GaryTexmo

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..

Expand|Select|Wrap|Line Numbers
  1. <game_definitions>
  2.   <assets>
  3.     <Texture2D name="ship" src="assets\images\ship.png" />
  4.     <Sound name="explosion1" src="assets\sounds\explosion1.wav" />
  5.     ...
  6.   </assets>
  7.  
  8.   <game_objects>
  9.     <PlayerShip sprite="ship" explosionSound="explosion1" ... />
  10.     ...
  11.   </game_objects>
  12. </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?

8 1559
GaryTexmo
1,501 Expert 1GB
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...

Expand|Select|Wrap|Line Numbers
  1. Hashtable m_spriteBank = new Hashtable();
  2.  
  3. ...
  4.  
  5. m_spriteBank["ship"] = Content.Load<Texture2D>("ship");
  6.  
  7. ...
  8.  
  9. Ship ship = new Ship(m_spriteBank["ship"], ...);
  10.  
  11. ...
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.
Feb 8 '11 #2
EARNEST
128 100+
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
Feb 8 '11 #3
EARNEST
128 100+
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?
Feb 10 '11 #4
GaryTexmo
1,501 Expert 1GB
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..

Expand|Select|Wrap|Line Numbers
  1. <game_definitions>
  2.   <assets>
  3.     <Texture2D name="ship" src="assets\images\ship.png" />
  4.     <Sound name="explosion1" src="assets\sounds\explosion1.wav" />
  5.     ...
  6.   </assets>
  7.  
  8.   <game_objects>
  9.     <PlayerShip sprite="ship" explosionSound="explosion1" ... />
  10.     ...
  11.   </game_objects>
  12. </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?
Feb 10 '11 #5
EARNEST
128 100+
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.
Expand|Select|Wrap|Line Numbers
  1. #   <game_objects>
  2. #     <PlayerShip sprite="ship" explosionSound="explosion1" ... />
  3. #     ...
  4. #   </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 :)
Feb 10 '11 #6
GaryTexmo
1,501 Expert 1GB
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.
Feb 10 '11 #7
EARNEST
128 100+
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
Expand|Select|Wrap|Line Numbers
  1. 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
Feb 10 '11 #8
GaryTexmo
1,501 Expert 1GB
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.
Feb 11 '11 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Phil Jenson | last post by:
I am try to evaluate the most efficient method of handling thousands of simultaneous TCP connects each of which remain connected to the server for hours and pass a small amount of data usually once...
5
by: Mantorok | last post by:
Hi all I have a DataTable containing around 25000 rows. For each row I want to query the entire DT to find out if potentially duplicate items exist, problem is - this is reeeeeeaaaaal slow. ...
2
by: GetTheMonkey | last post by:
After a fresh install I cannot create a new project. I also installed XNA Game Studio Express (beta) right after installing C#. The following error message appears: The filename, directory name,...
1
by: =?Utf-8?B?UmV1YmVu?= | last post by:
Hi, Sorry if I'm in the wrong newsgroup, but I couldn't find anything that dealt solely with XNA. I was wondering if there is any way to compile content into XNA binary files without using the...
4
by: COHENMARVIN | last post by:
Are there any good sources on video game programming in vb.net? Is "DirectX" a set of libraries for video game programmers? The reason I ask is that I'd like to convert a board game into a...
5
by: colin | last post by:
Hi, Ive got a 3d model editor wich im developing with XNA c# development environment, using the game window to display the wireframe mesh in 3d. however I need to use some other windows too,...
9
by: igor.tatarinov | last post by:
Hi, I am pretty new to Python and trying to use it for a relatively simple problem of loading a 5 million line text file and converting it into a few binary files. The text file has a fixed format...
1
by: Smokey Grindel | last post by:
Anyone know when XNA 2 will be made available on VS2008? I guess all they have to do is fix the project templates?
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.