naia-bevy-server 0.25.0

Library to faciliate naia_server & Bevy interop
docs.rs failed to build naia-bevy-server-0.25.0
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Visit the last successful build: naia-bevy-server-0.24.0

Bevy adapter for the naia server.

Adds naia's replication and messaging into a Bevy application. Entities and components that carry the Replicate marker are automatically tracked and replicated to connected clients; no manual diff loop is required.

Setup

Add the plugin and call [listen_on_app] (or call [Server::listen] in a startup system):

# use bevy_app::App;
# use naia_bevy_server::Plugin;
fn main() {
    App::new()
        // .add_plugins(DefaultPlugins)
        .add_plugins(Plugin::new(server_config(), protocol()))
        // .add_systems(Startup, init)
        .run();
}
# fn server_config() -> naia_bevy_server::ServerConfig { todo!() }
# fn protocol() -> naia_bevy_shared::Protocol { todo!() }

Interact with the server via the [Server] Bevy resource, or use [CommandsExt] / [ServerCommandsExt] on Commands to spawn entities and configure replication.

Quick start

use bevy_app::{App, Startup, Update};
use bevy_ecs::prelude::*;
use naia_bevy_server::{
    events::ConnectEvent,
    transport::webrtc,
    Plugin, Server, ServerConfig, UserKey,
};
use naia_bevy_shared::Protocol;

fn main() {
    App::new()
        .add_plugins(Plugin::new(ServerConfig::default(), Protocol::builder().build()))
        .add_systems(Startup, init)
        .add_systems(Update, on_connect)
        .run();
}

fn init(mut server: Server) {
    server.listen(webrtc::Socket::new(&server_addrs(), None));
}

fn on_connect(mut server: Server, mut connect_events: EventReader<ConnectEvent>) {
    for ConnectEvent(user_key) in connect_events.read() {
        server.accept_connection(user_key);
        // server.user_mut(user_key).enter_room(&room_key);
    }
}
# fn server_addrs() -> naia_bevy_server::transport::webrtc::ServerAddrs { todo!() }

Key types

Type Purpose
[Plugin] Registers systems and the [Server] resource
[Server] Bevy-wrapped server resource
[CommandsExt] Extension methods on Commands for replication setup
[ServerCommandsExt] Server-only extension methods on Commands
[events] Bevy events mirroring naia world events