Last active
August 18, 2020 01:45
-
-
Save mfrancis107/03cf057680d78eb452d0d6643fb6bdfb to your computer and use it in GitHub Desktop.
Example server scheduler for Bevy Engine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// The ScheduleRunnerPlugin in bevy uses sleep. So it's going to sleep for t duration. | |
/// If my understanding is correct this example should take execution time into consideration when scheduling. | |
use tokio::time; | |
use tokio::process::Command; | |
use std::time::Duration; | |
use legion::prelude::*; | |
use tokio::net::TcpListener; | |
use bevy:: { | |
prelude::*, | |
type_registry::TypeRegistryPlugin, | |
core::CorePlugin, | |
transform::TransformPlugin, | |
scene::ScenePlugin, | |
diagnostic::DiagnosticsPlugin, | |
window::WindowPlugin, | |
app::{AppExit} | |
}; | |
pub struct NetRuntime(tokio::runtime::Handle); | |
pub fn server_runner(mut app: App) { | |
let mut app_exit_event_reader = EventReader::<AppExit>::default(); | |
let mut runtime = tokio::runtime::Runtime::new().unwrap(); | |
let handle = runtime.handle(); | |
let mut interval = handle.block_on(async { | |
time::interval(time::Duration::from_micros(33_333)) | |
}); | |
let netRuntime = NetRuntime(handle.clone()); | |
app.resources.insert(netRuntime); | |
loop { | |
if let Some(app_exit_events) = app.resources.get_mut::<Events<AppExit>>() { | |
if app_exit_event_reader.latest(&app_exit_events).is_some() { | |
break; | |
} | |
} | |
handle.block_on(async { | |
interval.tick().await; | |
}); | |
app.update(); | |
} | |
} | |
fn tick_printer(){ | |
println!("Tick"); | |
} | |
fn main() { | |
App::build() | |
.add_plugin(bevy::type_registry::TypeRegistryPlugin::default()) | |
.add_plugin(bevy::core::CorePlugin::default()) | |
.set_runner(server_runner) | |
.add_system(tick_printer.system()) | |
.run(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment