From 31a433b69ee2abe4b7877b5fbd1419e715ee4cf2 Mon Sep 17 00:00:00 2001 From: easynam Date: Mon, 9 Nov 2020 21:04:27 +0000 Subject: [PATCH] add basic example of a custom update loop (#799) --- Cargo.toml | 4 ++++ examples/app/custom_loop.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 examples/app/custom_loop.rs diff --git a/Cargo.toml b/Cargo.toml index 5ca648ab451f7..16ee7690147c6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -148,6 +148,10 @@ path = "examples/3d/texture.rs" name = "z_sort_debug" path = "examples/3d/z_sort_debug.rs" +[[example]] +name = "custom_loop" +path = "examples/app/custom_loop.rs" + [[example]] name = "empty_defaults" path = "examples/app/empty_defaults.rs" diff --git a/examples/app/custom_loop.rs b/examples/app/custom_loop.rs new file mode 100644 index 0000000000000..4ca46dee35665 --- /dev/null +++ b/examples/app/custom_loop.rs @@ -0,0 +1,31 @@ +use bevy::prelude::*; +use std::{io, io::BufRead}; + +struct Input(String); + +/// This example demonstrates you can create a custom runner (to update an app manually). It reads +/// lines from stdin and prints them from within the ecs. +fn my_runner(mut app: App) { + app.initialize(); + + println!("Type stuff into the console"); + for line in io::stdin().lock().lines() { + { + let mut input = app.resources.get_mut::().unwrap(); + input.0 = line.unwrap(); + } + app.update(); + } +} + +fn print_system(input: Res) { + println!("You typed: {}", input.0); +} + +fn main() { + App::build() + .add_resource(Input(String::new())) + .set_runner(my_runner) + .add_system(print_system.system()) + .run(); +}