question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Identifying Entities Across Worlds (Networking)

See original GitHub issue

Background

Creating multiplayer games is tough with artemis-odb. It’s hard to synchronize two systems. I’ve read an issue #103 of @DaanVanYperen who discussed creating a co-op multiplayer game example, but wasn’t able to find said example.

In that thread, @junkdog mentioned ‘slave-worlds’ and ‘nested-worlds’ but I couldn’t find any documentation with those keywords and didn’t really know what he meant.

Global Entity Identification

I hope to identify entities globally (across worlds) using the following simple structure:

Server -> “master world” assigns entityIDs Client -> “slave world” uses IdEntityManager (similar to UuidEntityManager) This way, entities are identified using the master world’s entityID assignment.

Can anyone foresee any complications in this setup? Would you agree that this is simpler than having a custom EntityManager & World?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Wulfcommented, Jan 3, 2016

Okay so this was what I went with, and it turned out just fine:

On the server, there was a World object, which created entities by itself. On the client, there was a World object, which created entities by itself and also assigned the entity another ID - a “server id” using an IdEntityManager.

Here’s the IdEntityManager that I used on the client which helped me attach another id to an entity. It’s just a manager (which are now systems), so add it and use it as you would any other System!

import java.util.HashMap;
import java.util.Map;

import com.artemis.Entity;
import com.artemis.Manager;
import com.artemis.utils.Bag;

/**
 * Helps manage entities in a slave-world (i.e. a client-side world).
 */
public class IdEntityManager extends Manager {
    private final Map<Integer, Entity> idToEntity;
    private final Bag<Integer> entityToId;

    public IdEntityManager() {
        this.idToEntity = new HashMap<Integer, Entity>();
        this.entityToId = new Bag<Integer>();
    }

    @Override
    public void deleted(Entity e) {
        Integer id = entityToId.safeGet(e.getId());
        if (id == null) {
            return;
        }

        Entity oldEntity = idToEntity.get(id);
        if(oldEntity != null && oldEntity.equals(e)) {
            idToEntity.remove(id);
        }

        entityToId.set(e.getId(), null);
    }

    public Entity getEntity(int id) {
        return idToEntity.get(id);
    }

    /**
     * @param e Entity to get ID for
     * @return MAY RETURN NULL
     */
    public int getId(Entity e) {
        int id = entityToId.safeGet(e.getId());

        return id;
    }

    public void setId(Entity e, int newId) {
        Integer oldId = entityToId.safeGet(e.getId());
        if (oldId != null)
            idToEntity.remove(oldId);

        idToEntity.put(newId, e);
        entityToId.set(e.getId(), newId);
    }
}

Hope this helps.

0reactions
Wulfcommented, Jan 3, 2016

My reasoning for not using UuidEntityManager was that it was too expensive (in terms of bandwidth over a network) to send a whole Java UUID (something like d5628473-e293-4288-8db4-80fc1e5a349e).

This may be feasible for your situation, but my application required sending many packets per second, so I was very bitter about ever bit 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Discovering entity connections insights using Network ...
Network Analytics is best leveraged to model interactions between entities and get insights focusing on the interactions, rather than on ...
Read more >
Networking Entities - Valve Developer Community
Logical and physical objects in the game world are called 'entities' and are represented in the source code as classes derived from a...
Read more >
Naming Entities on Your Network (System Administration Guide
The TCP/IP protocols locate a machine on a network by using its IP address. However, if you use a recognizable name, then you...
Read more >
is it possible to reference a ghost entity across the network?
Question is it possible to reference a ghost entity across the network? ... GhostId as a unique reference in the Server/Clients worlds.
Read more >
OSI Model: The 7 Layers of Network Architecture
The upper most layer of the OSI model identifies networking entities to facilitate networking requests by end-user requests, determines ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found