Jump to content
  • Advertisement
Sign in to follow this  

Unity Resource cache for game engine

This topic is 1189 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have xml files to define the game object, but reading them everytime to create a game object is not a good idea, So I am trying to create something like prefabs in unity for my engine. To do that currently I have different structure for each component.

struct TransformA
glm::vec3 position;
glm::vec3 rotation;
glm::vec3 scale;


struct SpriteA
std::string ModelName;
std::string ShaderName;
struct ComponentsA
std::vector<std::string> components;

So when I read an object from XML file the first time and if the objects need to create a prefab, what I do is create an object of each of that structure and store it in a map. I have 1 map for each structure I have mentioned. Now when I have to create an object during runtime, I search for the object of that component in map and use it to create the gameobject instead of reading the file.

std::map<std::string, ComponentsA*> componentArchetypesMap;
std::map<std::string, TransformA*> TransformArchetypesMap;
std::map<std::string, SpriteA*> SpriteArchetypesMap;
std::map<std::string, ScriptA*> ScriptArchetypesMap;

Getter functions for the components

ComponentsA* GetComponents(std::string gameobject_name);
TransformA* GetTransform(std::string gameobject_name);
SpriteA* GetSprite(std::string gameobject_name);

And I use this functions to add them to map when I read the file

void AddComponents(ComponentsA* vecOfComponent, std::string gameobject_name);
void AddTransform(TransformA* transform, std::string gameobject_name);
void AddSprite(SpriteA* sprite,std::string gameobject_name);

So is this method any good, as I have just mentioned few of the components but I have more components in my engine ?

Can someone help me with doing this in a better way, if this is not a good way to do it.

Share this post

Link to post
Share on other sites

I use XML data definitions for a lot of the content in my project.


In effect, my data didn't specifically represent game object instances, but rather Meta classes to represent the definition of the object (keys to determine spritesheet or sfx, initial values for different member variables and the like). At the start I load in all my data, which is pretty light. I store these meta objects in a manager class which can, on request, take the data and give me back an instance of the specific object.


The actual instance objects are much lighter and keep only the mutable data and any logic needed (Position, Color, HP, Rotation, that sort of thing). They may also reference the content object that it was sourced from to reduce data duplication.


It appears you're going a similar path but without the separation between instance object and data representation. Don't know if my way is The Best™ or even really better or good, but it seems to work pretty well.

Share this post

Link to post
Share on other sites

I use a very similar methed to what you are using.


I store what I like to call ObjectDescriptions inside of a map, with a string as the object type name and the key. I read them from the XML file on startup, and just create them and store them in the map.


So far, I have not had any problems with this.


For adding specific values to your game objects that you create, you may wish to export those values from your level editor separately (or from a script of some kind, like what Amnesia does), and assign the values to the already created object. I store my level data in a binary file, which is way faster to read from, with contiguous data chunks.

Edited by Solid_Spy

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!