benmoran56 / esper

An ECS (Entity Component System) for Python
MIT License
544 stars 69 forks source link

Thoughts on adding a Resource type to esper? #45

Closed xFrednet closed 4 years ago

xFrednet commented 4 years ago

Hello, I just wanted to ask: What are your thoughts on adding a Resource type to esper? Before esper I've used amethyst as an ECS for Rust. That system uses so called Resource to share data that is not specific to an direct entity. For example a global score that gets accessed by multiple systems.

The formal definition of a Resource by amethyst:

A resource is any type that stores data that you might need for your game AND that is not specific to an entity. For example, the score of a pong game is global to the whole game and isn't owned by any of the entities (paddle, ball and even the ui score text).

I know that I could just distribute the value to the systems at the creation time or simply add a new member to the world class. However, I believe it might be beneficial to have a shared resource marked as one by accessing them through world.get_resource(<Type>) for example. Do you have any thoughts on this?

benmoran56 commented 4 years ago

Hi @xFrednet,

I think the idea of a simple World subclass would be a good way to go. To prevent bloat, I'm not sure if I'd want to add something to the main code, but a simple example would be nice to include in repository perhaps.

For my personal projects, I tend to arrange them by Scenes. I use one World instance per Scene, and add the Processors on instantiation. For those that need it, I just pass a scene reference into those Processors that need it. Something like:

    def __init__(self):
        self.world.add_processor(AttackProcessor(scene=self))
        self.world.add_processor(CollisionProcessor(scene=self))
        self.world.add_processor(MovementProcessor())
        self.world.add_processor(CommandProcessor(scene=self))
        .....

All Scenes share the same high level Audio playback interface, and SaveGame interface, so all of the Processors with a reference to the scene will also be able to access these. The "SaveManager" is in essence a shared resource manager. Adding a dedicated resource manager in this same vein would be pretty easy to do.

Since I use separate World instances for each Scene in my designs, it makes sense to my brain. If I was going to use a single World instance for the entire application, I would probably just add things to the World instead (as you've mentioned).

Does this make sense? Sorry if it's a bit hard to follow :sweat_smile:

xFrednet commented 4 years ago

Hey thank you very much for the response. It makes sense to split up the game into different scenes. I could also add the resources in the sub class if i really need them.

Thank you for the response 🙃