0
\$\begingroup\$

I'm new to ECS. And I'm trying to create my own game in ECS. I want to ask what's the common way to do some resource management. For example, it's very common to load and unload images from disk in any games.

There is my resource management so far:

class Resource { public: Resource() = default; virtual ~Resource() = default; }; class Image : public Resource { const unsigned char *data; int width; int height; public: Image() = default; ~Image() { // free the memory } void load(const std::filesystem::path &path) { // load the image } }; class ResourceManager { std::unordered_map<std::string, std::unique_ptr<Resource *>> resource_map; public: void add_resource(const std::string &key, std::unique_ptr<Resource> resource) { resource_map[key] = std::move(resource); } template <typename T> T *get(const std::string &key) { // Check its type here auto iter = resource_map.find(key); if (resource_map == resource_map.end()) { return nullptr; } else { return static_cast<T *>(iter->second.get()); } } void unload(const std::string &key) { resource_map.erase(key); } }; 

Then I can get the resource pointer by the key string:

Image *image = resource_manager.get_resource<Image>("image.test_img"); 

In my opinion, it's more OO than ECS. Because it uses a global object. So, is there a better way to do that?

\$\endgroup\$
7
  • 1
    \$\begingroup\$ In what way do you imagine this would need to be done differently for an ECS? How have you tried incorporating resource loading into your game so far, and where have you gotten stuck in making it work with your particular ECS approach? These specifics can help us target our advice to more accurately address the parts you need help with. \$\endgroup\$ Commented Sep 8 at 10:06
  • \$\begingroup\$ @DMGregory Thank you for your reply. I have added what I have tried so far. \$\endgroup\$ Commented Sep 8 at 14:32
  • 3
    \$\begingroup\$ You are conflating subjects. Asset loading and ECS have nothing to do with each other, it's kind of like asking "how to parallel park wearing a yellow tie?". Also "it's more OO than ECS" is a poor mindset; programming patterns exist to make programming easier, not to be followed without exception. If part of your program is better fitted to be object oriented instead of data driven (ECS) then make it object oriented even if it "contradicts" ECS design. \$\endgroup\$ Commented Sep 8 at 17:07
  • 2
    \$\begingroup\$ What do you find unsatisfactory about using a global object? This isn't necessarily a bad idea — it can be an efficient solution to ensuring multiple requests / releases of the same asset get coordinated correctly. If the contract for interacting with it is clear, this won't necessarily be a problematic form of mutable global state, since the problem you're solving is by its nature global and mutable. \$\endgroup\$ Commented Sep 8 at 18:06
  • 2
    \$\begingroup\$ Hint: When a call to resource_manager.get_resource fails to find the asset with that key, then I would really recommend to log a warning with the key-string and return a placeholder asset instead of just silently returning null. You will be glad about that later. \$\endgroup\$ Commented Sep 9 at 8:25

1 Answer 1

-1
\$\begingroup\$

I use tiny-ecs for my games and I unload my images in my ECS Systems. Example from my Collectibles System:

SCollectibles = Core.class() function SCollectibles:init(xtiny, xbump, xplayer1) -- tiny function end function SCollectibles:filter(ent) -- tiny function return ent.iscollectible end function SCollectibles:onAdd(ent) -- tiny function end function SCollectibles:onRemove(ent) -- tiny function self.bworld:remove(ent) end function SCollectibles:process(ent, dt) -- tiny function if ent.isdirty then -- collected self.tiny.tworld:removeEntity(ent) end end 

Hope this can help!?

\$\endgroup\$
2
  • \$\begingroup\$ Thank you for your answer. Sorry for my unclear question. I have updated it to make it clearer. Could you check the new version? \$\endgroup\$ Commented Sep 8 at 14:39
  • 1
    \$\begingroup\$ I am not familiar with tiny-ecs, but I can't see any loading or unloading of image files happening in that code snippet. \$\endgroup\$ Commented Sep 9 at 8:31

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.