0
\$\begingroup\$

I am working on a small multiplayer game with rpg elements using java and "Artemis ODB". Most of the logic is already done but one important thing is missing. The persistence.

So i am searching for different ways to persist my game. One thing is important, the world is huge and i cant load in the whole world at once. I already stumbled upon two different ways on my own. Using a relational database or a nosql database. But one thing scares me... entity references.

In my game i have an player entity and item entities. The player entity owns a inventory component which stores a reference to some item entities. In my ecs, each entity reference is an simple integer. So this looks like this...

/**
 * Represents the Inventory of our game, contains references to entities acting as items.
 */
public class Inventory extends HibernateComponent {
    public Set<Integer> itemEntities = new LinkedHashSet<>();
}

Why does this scare me ? Well... Entity references are Integers in my case. They are no real ID and i cant set or change this "ID" of an entity because my framework permits it. This brings us straight to the problem. When i serialize this component or the whole player entity it would look like this.

Inventory{
   itemEntities: [40,2,5,50]
}

So when we save this and load it later on we have no entity with the id 40... and even if we recreate that entity we cant set it to an id of 40, because the frameworks permits it. So how the heck do we save references between entities ? ^^

So all in all... what are common ways to save, load entities from an ecs ? How do you do it ? And what would you recommend ?

\$\endgroup\$
5
  • 1
    \$\begingroup\$ The ID in the save data does not need to be the ID you use at runtime. You can invent a new ID when you need to serialize data, then map those serialized IDs to your own new one-off IDs when you deserialize them. \$\endgroup\$
    – DMGregory
    Commented Feb 18, 2021 at 20:51
  • \$\begingroup\$ @DMGregory Thanks ! Well that makes sense, so each entity/component owns an "database id". But how should i proceed when i load an entity/component that references other ones ? The problem i see here is that when we load it... we are forced to load the referenced items too. Because otherwhise we cant map our serialized database ids to our ingame ids... because those items do not exist ingame yet. \$\endgroup\$
    – genaray
    Commented Feb 18, 2021 at 20:57
  • 1
    \$\begingroup\$ In what situation would it be meaningful to load a player's inventory while NOT loading the items inside that inventory? \$\endgroup\$
    – DMGregory
    Commented Feb 18, 2021 at 21:02
  • \$\begingroup\$ @DMGregory Right... \$\endgroup\$
    – genaray
    Commented Feb 18, 2021 at 21:05
  • \$\begingroup\$ When your ECS framework was designed without thinking about how one would save and reload the game state, then that seems like a serious design flaw. Have you looked at how other users of the same framework work around that problem? \$\endgroup\$
    – Philipp
    Commented Feb 19, 2021 at 14:06

0

You must log in to answer this question.

Browse other questions tagged .