Clean architecture for an asset loader in a component based framework?

Started by
5 comments, last by TheAsterite 11 years, 12 months ago
I've been doing a lot of reading about how a loader should be handled on the forums, and I feel like I'm on information overload. I've read that it's better to have separate loaders for different assets such as having a texture, mesh, shader, etc, loaders all separate, but I've also seen some examples where the different components are grouped together so that a class loads the file, then the different components figure out what to do with file when it's loaded in. I also need a way to store the information depending on how it's going to be used. I saw an example I'd like to implement where there are 3 memory blocks, one for data needed every frame, one for data needed every block, and one for constant data.

My basic design goals for the framework are as follows:
I'm only worried about having it work on windows
I want to stream in world blocks to have a straight continuous level with branches
I want to have flexibility to experiment with shaders

So my first issue to tackle is how to handle the loading of assets and storing them into the different blocks of memory. Some insight into the pros and cons of different methods would be greatly appreciated. Software design and architecture is really hard for me, and I'd like all the information I can get.
Advertisement
At its core, this should boil down to a very simple API:

void LoadAsset(some_identifier, std::vector<char>& memorybuffer);

(Assuming C++; translate to the appropriate language of your choice.)

Your high level code asks for some data and provides a blob of memory to store it in, and the low-level asset loader just fetches the data and blindly stuffs it into the buffer.

Of course you can fancy this up all kinds of ways, but that's the basic principle. It's so simple that it doesn't even require a class (again assuming a language that supports free functions) and provides convenient decoupling of the asset-loading infrastructure from the logic that actually consumes assets.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]


At its core, this should boil down to a very simple API:

void LoadAsset(some_identifier, std::vector<char>& memorybuffer);

(Assuming C++; translate to the appropriate language of your choice.)

Your high level code asks for some data and provides a blob of memory to store it in, and the low-level asset loader just fetches the data and blindly stuffs it into the buffer.

Of course you can fancy this up all kinds of ways, but that's the basic principle. It's so simple that it doesn't even require a class (again assuming a language that supports free functions) and provides convenient decoupling of the asset-loading infrastructure from the logic that actually consumes assets.


Ok, that makes sense, so the texture or whatever loader will call the function that provides a file handle then determine what to do with the blob that's loaded in? Now in a component based engine, what's a good way to couple what's being stored and how it's being used? I get that a component based system will have lists that you store different properties into, but how do you group them together logically so that it's relatively easy to determine which properties belong to say the main character? I was thinking loading them into the lists first based on the world information with a tag, then a high level object will then search for the property that matches it's tag during the level initialization. Am I going in the right direction?

Edit: Actually, I'm having trouble seeing how a single function will work. For example, with a file that has vertex info, you need to parse it to get the coordinates out of it. But with something like a texture, I know D3D has it's own function for opening the file and storing the data in it. How do I make the single function work for both instances?
Generally the way component systems manage groups of related components is with systems. A system is just a collection of containers for components that belong to a related group.

That sounds really vague now that I read it to myself, so here's an example:

class HealthComponent { /* blah */ };
class WeaponComponent { /* blah */ };
class AnimatedComponent { /* blah */ };

class CharacterSystem
{
std::vector<std::shared_ptr<HealthComponent>> HealthComponents;
std::vector<std::shared_ptr<WeaponComponent>> WeaponComponents;
std::vector<std::shared_ptr<AnimatedComponent>> AnimatedComponents;
};



You define a system for groups of related components, in this case, characters. A character in our little example consists of a health component, a weapon component, and a component that manages the animations of the character (and presumably does rendering/etc.).

There are two kinds of data loading to consider. One is loading "static" data - levels, maps, models, etc. The other is loading "dynamic" data, i.e. save games, replays, etc.

Static data usually looks like this:

std::vector<char> buffer;
LoadAsset("asset_foo", buffer);
TheSystem.AddComponent(FooComponent::ParseData(buffer));


Where the buffer contains the raw asset, and the FooComponent knows how to parse that into the appropriate stuff.

Dynamic data is basically the same, but instead you tag each buffer with what kind of component it is, so you can hand off to the right factory:


std::vector<char> buffer;
LoadAsset("savegame", buffer);
std::string tag = ReadTag(buffer);
if(tag == "foo")
TheSystem.AddComponent(FooComponent::ParseData(buffer));

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Oh, ok I get it now. Thanks so much for your help. Do you have any links on this kind of stuff where I can do a little research on my own?
Check out this presentation from the 2011 GDC AI Summit. It's got a pretty good breakdown of how to do component systems; it focuses on AI but it's applicable to general architecture across the board.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Actually I just have 1 more question, what's the best way to handle loading assets that are normally imported with a library? Like loading a .dds file with directx? It doesn't fit loading the contents of the file into a buffer to be parsed later right?

Edit: Never mind, I see directx has a createshaderresourcefrommemory function. I'm all set, thanks again.

Edit2: Well, now that I think about it, my point still stands. It seems like things like textures and levels/models are read in completely different ways. Can you really load static data all the same way? It seems like levels/models are parsed and stored as they're being read, but things like textures are either read by a library or directly copied into the buffer. Am I just forced to make separate loaders in this instance? The only real experience I've had to deal with loading is either using an already provided function to do so, or just parsing a text file and converting the strings into data.

This topic is closed to new replies.

Advertisement