bevyengine / bevy

A refreshingly simple data-driven game engine built in Rust
https://bevyengine.org
Apache License 2.0
35.75k stars 3.54k forks source link

Setting ScheduleRunnerSettings doesn't seem to get loop to run with MinimalPlugins #8870

Closed codeacula closed 11 months ago

codeacula commented 1 year ago

I am new to Rust and bevy. Please don't hesitate to correct coding issues. I'm aware of a bug in my handling of TcpListener. That's been resolved locally.

Bevy version

0.10.1

Relevant system information

cargo 1.70.0 (ec8a8a0ca 2023-04-25) Windows 11 Version 10.0.22621 Build 22621

What you did

I tried to use the example to create a headless server, and it won't build.

    // main.rs
    App::new()
        .insert_resource(ScheduleRunnerSettings::run_loop(Duration::from_secs_f64(
            1.0 / 60.0,
        )))
        .add_plugins(MinimalPlugins)
        .add_plugin(ServerPlugin)
        .run();
// server lib.rs
pub struct ServerPlugin;

use std::{
    io::Read,
    net::{TcpListener, TcpStream},
};

use bevy::{ecs::system::Commands, prelude::*};

#[derive(Component)]
struct Connection {
    stream: TcpStream,
}

#[derive(Resource)]
struct Server {
    listener: TcpListener,
}

#[derive(Component)]
struct Person;

#[derive(Component)]
struct Name(String);

fn start_listening(mut commands: Commands) {
    let listener = match TcpListener::bind("0.0.0.0:23") {
        Ok(listener) => listener,
        Err(e) => panic!("{:?}", e),
    };

    let server = Server { listener };
    commands.insert_resource(server);
}

fn check_for_new_connections(mut commands: Commands, server: Res<Server>) {
    for conn in server.listener.incoming() {
        match conn {
            Ok(stream) => {
                commands.spawn((Connection { stream },));
                commands.spawn((Person, Name("Elaina Proctor".to_string())));
                println!("New connection!");
            }
            Err(err) => println!("Um, err? {}", err),
        }
    }
}

fn check_for_waiting_input(all_connections: Query<&Connection>) {
    for conn in all_connections.iter() {
        let mut buf = String::new();
        let mut stream_copy = match conn.stream.try_clone() {
            Ok(stream) => stream,
            Err(e) => panic!("{:?}", e),
        };

        match stream_copy.read_to_string(&mut buf) {
            Err(err) => {
                panic!("{:?}", err);
            }
            _ => (),
        }

        if buf.len() > 0 {
            println!("Received: {}", buf);
        }
    }
}

fn greet_people(query: Query<&Name, With<Person>>) {
    for name in &query {
        println!("hello {}!", name.0);
    }
}

impl Plugin for ServerPlugin {
    fn build(&self, app: &mut App) {
        app.add_startup_system(start_listening)
            .add_system(check_for_new_connections)
            .add_system(check_for_waiting_input)
            .add_system(greet_people);
    }
}

What went wrong

Following the headless directions in the example, I get:

no function or associated item named `run_loop` found for struct `ScheduleRunnerPlugin` in the current scope function or associated item not found in `ScheduleRunnerPlugin`

Additional information

Other information that can be used to further reproduce or isolate the problem. This commonly includes:

This PR looks like it updated the example. I can't tell if anything else might've changed it.

This seems to work:

    app.add_plugin(LogPlugin {
        level: Level::DEBUG,
        filter: "bevy_ecs=trace".to_string(),
    })
    .insert_resource(ScheduleRunnerSettings::run_loop(Duration::from_secs_f64(
        1.0 / 60.0,
    )))
    .add_plugins(MinimalPlugins)
    .add_system(hello_us)
    .add_plugin(ServerPlugin);
    return app;

https://github.com/bevyengine/bevy/commit/86aaad743b6842152f92f910f09a85e7c2f8b689#

codeacula commented 1 year ago

Updated the comment to focus solely on the documentation

codeacula commented 11 months ago

Closing because I don't think it is an issue any longer with the new systems.