cBournhonesque / lightyear

A networking library to make multiplayer games for the Bevy game engine
https://cbournhonesque.github.io/lightyear/book
Apache License 2.0
439 stars 46 forks source link

Make the examples simple #397

Open simbleau opened 4 months ago

simbleau commented 4 months ago

Something that really hurts my understand of lightyear is just how many hoops I have to jump through with tons of layers of configurations and boilerplate for what really should be a straightforward example.

Instead of having a lobby example that uses any configuration of transports, tons of CLI args, etc. It would be beneficial to simply use WebTransport in all cases by default, since that works on all platforms.

Secondly, I'm not really sure why the standard with a settings.ron file was chosen. It would be nice if this was code by default instead, with a separate "file_settings" example if you want to show how to do that.

Most people don't want to start serializing/deserializing the settings.ron file just to try out the examples, they want to jump around the code in their IDE, Ctrl+Click from struct to struct and read documentation. Having an opaque .ron file disables someone from understanding the settings.

This will also dramatically reduce the amount of refactoring if you change 1 piece of the config in a PR - and likely stop someone from wanting to contribute to such pieces, since it requires updating that config and boilerplate 20x across all the examples.

TLDR:

cBournhonesque commented 4 months ago

There is an example that shows a simple setup without the example harness: https://github.com/cBournhonesque/lightyear/tree/main/examples/simple_setup

I think right now I'll keep the current setup, as it allows me to easily test all configurations when I make a code change (host-server, all sorts of transports, etc.). There is a common test harness for setup so the examples only showcase the actual code that changes

mmarklar commented 3 months ago

Are the examples expected to work outside of the repo? I'm very inexperienced so to dip my toes in I tried extracting the simple_box example into a separate project (including the common parts). The only way I can get it to build is giving it the path to the lightyear repo:

lightyear = { version = "0.15.1", path = "../../../lightyear/lightyear", features = [
  "steam",
  "webtransport",
  "websocket",
] }

Without path="..." I get the following errors:

error[E0422]: cannot find struct, variant or union type `ReplicationConfig` in this scope
   --> games/firstgame/src/app.rs:357:22
    |
357 |         replication: ReplicationConfig {
    |                      ^^^^^^^^^^^^^^^^^ not found in this scope

error[E0422]: cannot find struct, variant or union type `ReplicationConfig` in this scope
   --> games/firstgame/src/app.rs:392:22
    |
392 |         replication: ReplicationConfig {
    |                      ^^^^^^^^^^^^^^^^^ not found in this scope

error[E0560]: struct `lightyear::prelude::server::ServerConfig` has no field named `replication`
   --> games/firstgame/src/app.rs:357:9
    |
357 |         replication: ReplicationConfig {
    |         ^^^^^^^^^^^ `lightyear::prelude::server::ServerConfig` does not have this field
    |
    = note: available fields are: `packet`, `ping`

error[E0560]: struct `lightyear::prelude::server::ServerConfig` has no field named `replication`
   --> games/firstgame/src/app.rs:392:9
    |
392 |         replication: ReplicationConfig {
    |         ^^^^^^^^^^^ `lightyear::prelude::server::ServerConfig` does not have this field
    |
    = note: available fields are: `packet`, `ping`

error[E0609]: no field `reason` on type `&lightyear::prelude::client::DisconnectEvent`
  --> games/firstgame/src/client.rs:85:29
   |
85 |         let reason = &event.reason;
   |                             ^^^^^^ unknown field

error[E0560]: struct `SharedConfig` has no field named `server_replication_send_interval`
  --> games/firstgame/src/shared.rs:79:9
   |
79 |         server_replication_send_interval: SERVER_REPLICATION_INTERVAL,
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `SharedConfig` does not have this field
   |
   = note: available fields are: `client_send_interval`, `server_send_interval`

Some errors have detailed explanations: E0422, E0560, E0609.
For more information about an error, try `rustc --explain E0422`.
error: could not compile `firstgame` (bin "firstgame") due to 6 previous errors
cBournhonesque commented 3 months ago

It's probably because the examples use code from the main branch, so are not directly compatible with the released 0.15.1 lightyear version. You might want to copy the examples from the tag https://github.com/cBournhonesque/lightyear/tree/0.15.1 if you want code that is compatible with the released version. (however I will release a new version soon once bevy 0.14 is out)

mmarklar commented 3 months ago

Makes sense, thanks!