Jump to content
  • Advertisement
Sign in to follow this  
lipsryme

Good way to design a content system (add & remove data)

This topic is 1085 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

Working on my private project's like for example my hobby engine has always brought me to the same issue with the content system.

What I do right now is have a content class that contains a vector where the data is added to. And then have other objects store a ptr or index to an element of that data as reference. However the problem always start when there's need to be able to remove it. Because pushing back objects is easy but when you have objects throughout your application that point or reference via index to a certain element in your data storage and suddenly need to remove it, issues start to appear. Because if you stored an index somewhere in your application to that element that needs to be removed, you have to make sure to change the index for all other parts of the code that reference it, which is pretty bad. Storing a ptr works better however that also has issues because storing a ptr to an element inside the vector makes a copy of that ptr and when you delete and set the ptr inside the vector to null, the copy that is somewhere still has it's reference to it and is not null...

 

So I was wondering what a good design is for something like that and how you guys would go on about it.

Share this post


Link to post
Share on other sites
Advertisement
that strongly depends why you'd want to remove it. why is the content removed although objects are still referencing it? what should happen if you use an object that references content that has been removed?

removing content is easy ;)
making everything else still work is the challenge and it starts by defining what behavior you! want.

Share this post


Link to post
Share on other sites

Use a hash map of integer to resource (i.e. in C++, a std::unordered_map<int, Resource>), and generate arbitrary integer handles (you can increment each time, or generate random unique ids, or what have you).

 

Your handles are still plain old integers, they don't change when you insert/remove, and lookup and insertion are still O(1), with the benefit of O(1) removal, and a slightly higher constant factor.

Share this post


Link to post
Share on other sites

@krypt0n: Well let's say you have an entity object that needs to have a reference to its material object and for some reason you need to delete that material. You then have to make sure to also set all references to it throughout the program to null (or to something else).

 

@swiftcoder: So would you say the extra cost vs an std::vector for storing e.g. all the texture objects that the engine has currently loaded or all the entities in your scene is worth it ?

Share this post


Link to post
Share on other sites

I see various possibilities here:

 

  1. Each content gets a unique ID, every other Classe/Subsystem/whatever who wants to use it hast to check bevore usage if the content is still available.
  2. If someone wants to use the content resource, it hast to reqister bevor using and unregister when finished
    Here you again have two possibilities: delete an object only if no one is registerd for using it (can be done by simple reference counting) or inform every system that is using the object that this object is gona dying
  3. You can define a sepcific point (e.g the end of your game loop) where all objects will be deleted which are marked for deletion during the loop. Befor the next run begins everyone has to check if there resources are still available

Personally i would go with option 2, depending on implementation constraints smart pointers would be a good advice here

Share this post


Link to post
Share on other sites


@swiftcoder: So would you say the extra cost vs an std::vector for storing e.g. all the texture objects that the engine has currently loaded or all the entities in your scene is worth it?

If you want to be able to delete items, then you need a level of indirection in the way you access items. Pretty much any other method of pointer indirection is going to incur just as much overhead.

 

A better alternative might be to get rid of the concept of centralised resource management entirely, and just have a resource cache that client objects access resources through. Then you resource lifetimes are implicitly specified by when they are accessed, and you can control this deterministically through something like Dice's scope stacks.

Share this post


Link to post
Share on other sites

You can use smart pointers.

If you reference to the object through an std::weak_ptr (and in the container you use an std::shared_ptr) when the object is deleted, the weak_ptr::lock() will give you a shared_ptr pointing to nullptr.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!