naia-lib / naia

a cross-platform (including Wasm!) networking library built in Rust. Intended to make multiplayer game development dead-simple & lightning-fast
Apache License 2.0
911 stars 58 forks source link

Bevy Adapter: Server.disconnect() should be migrated to CommandsExt #178

Open connorcarpenter opened 1 year ago

connorcarpenter commented 1 year ago

https://github.com/naia-lib/naia/blob/15bf23adda3d8c36f1464c7fa09e0c16d40d3622/server/src/server.rs#L732

Users shouldn't be expected to only be able to call this function from an exclusive system.

EDIT: Apparently even in an exclusive system, it is not possible to call this method..

Veritius commented 1 year ago

It can't be run in an exclusive system at all, as the only API surface for the server is Server, a systemparam.

Veritius commented 1 year ago

Correction, it is possible, there are two workarounds I found.

The current workaround I am using for this is

world.resource_scope(|world, mut server: Mut<NaiaServer<Entity>>| {
    for key in keys_to_disconnect {
        let world_mut = WorldMut::new(world);
        server.user_mut(&key).disconnect(world_mut);
    }
});

however, this requires naia-server to be added as a dependency (with the same version as naia-bevy-server), so NaiaServer (just Server in naia-server) is accessible.

You can do this without using naia-server as a dependency, just using SystemState, but it has to be in an unsafe block, as there are two simultaneous mutable borrows. It's probably not an issue, but I use the former solution for its safety.