Crate bevy_entity_uuid

Crate bevy_entity_uuid 

Source
Expand description

Support uuids to identify entities in bevy.

Bevy does not easily expose control over manually creating an entity with an arbritrary value due to its internal requirements. This will allow to keep track of entities with some manually specified id instead.

This plugin adds a new component that can mark an entity with a uuid. The plugin will be able to do fast lookups uuid<>entity and keep track of entity creation or destroy.

This can be a useful plugin when things need to be synchronized across the network clients where entity ids are different but they need somehow to be unified under one id representation.

The normal method to create an entity in bevy is to do so from a Command. This module will add an extra method to create entities from a Uuid. It will still call the normal entity creation underneath, but also take care to add relevant component and tracking resources to bind it to the wanted uuid on creation.

To attach a uuid to an entity instead, just add the EntityUuid component to the wanted entity.

Two methods are added to Commands with the trait CommandsEntityUuid:

  • spawn_uuid(uuid, …): equivalent of cmd.spawn(…) but with added uuid
  • spawn_empty_uuid(uuid): equivalent of cmd.spawn_empty() but with added uuid

To setup this plugin, add the plugin EntityUuidPlugin to bevy:

use bevy_entity_uuid::EntityUuidPlugin;
use bevy_ecs::prelude::*;
use bevy_app::prelude::*;

let mut app = App::new();
app.add_plugins(EntityUuidPlugin);

Then when using Commands, spawn with uuid variants:

use bevy_entity_uuid::CommandsEntityUuid;
use bevy::prelude::*;
use uuid::Uuid;

fn system_with_empty(mut cmd: Commands) {
    let uuid = Uuid::new_v4();
    cmd.spawn_empty_uuid(uuid);
}

fn system_with_bundle(mut cmd: Commands) {
    let uuid = Uuid::new_v4();
    cmd.spawn_uuid(uuid, Transform::default());
}

Structs§

EntityLookup
This resource allows for fast lookups uuid<>entity.
EntityUuid
Component used to keep track of which uuid is in this entity.
EntityUuidDeleted
Event triggered when a EventUuid is deleted. This can be useful on entity despawn situations because otherwise it’s not possible to obtain the original deleted uuid: the map resourece is already updated and the uuid won’t be found in it.
EntityUuidPlugin
Plugin to setup EntityUuid functonality.

Traits§

CommandsEntityUuid
Added methods to spawn entities with a uuid.
WorldEntityUuid
This trait is for solving the eventual consistency of Commands: using world the same uuid won’t be spawned twice, but return the original first entity.