benmoran56 / esper

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

Seeking Guidance on Persisting and Loading Esper from a Database #93

Closed GeorgesAlkhouri closed 11 months ago

GeorgesAlkhouri commented 11 months ago

Hi everyone,

First and foremost, thank you for this elegant and beautifully crafted piece of code. We are in the process of developing a turn-based game on Discord and are considering using Esper for our project.

I have a question regarding the persistence and loading of the game world. Are there examples or recommendations on how to accomplish this? Should we focus solely on entities and components and then reconstruct the world context from them?

Thanks in advance for your assistance!

benmoran56 commented 11 months ago

Hi Georges, Thank you for the kind words. Esper does not directly support loading or saving (to disk or otherwise).

What I tend to do in my own projects, is define a series of template functions for creating each entity type. These functions can take arguments for any initial data (or saved data from a previous save). If loading a fresh game, the arguments can be default. If loading from save, you can override with saved data. For example:

def create_slime(x, y, hp, etc):
    # create components & entity
    return entity_id   # if necessary

In a checkers game, for example, you would save all of the pieces and their positions. On load, you repopulate the world using this saved data. For a platformer, you might only care about saving the main player stats, and which level/save point you were at.

Let me know if you need clarity on anything, or have any additional questions.

GeorgesAlkhouri commented 11 months ago

Yes, that clarifies a lot, thanks. closing the issue.