GithubHelp home page GithubHelp logo

mrgvsv / bevy-tokio-tasks Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ekardnt/bevy-tokio-tasks

0.0 0.0 0.0 95 KB

Simple integration of a Tokio runtime into a Bevy app for background processing.

License: Creative Commons Zero v1.0 Universal

Rust 100.00%

bevy-tokio-tasks's Introduction

bevy-tokio-tasks

A simple Bevy plugin which integrates a Tokio runtime into a Bevy app.

crates.io docs.rs

How To

How to initialize this plugin

To initialize the plugin, simply install the TokioTasksPlugin when initializing your Bevy app. The simplest way to do this is to use the TokioTasksPlugin::default() method, which sets up a Tokio Runtime with default settings (the plugin calls Runtime::enable_all to enable Tokio's IO and timing functionality).

fn main() {
    bevy::App::new()
        .add_plugins(bevy_tokio_tasks::TokioTasksPlugin::default())
}

If you want to customize the Tokio Runtime setup, you may do so by specifying a make_runtime callback on the TokioTasksPlugin.

fn main() {
    bevy::App::new()
        .add_plugins(bevy_tokio_tasks::TokioTasksPlugin {
            make_runtime: Box::new(|| {
                let mut runtime = tokio::runtime::Builder::new_multi_thread();
                runtime.enable_all();
                runtime.build().unwrap()
            }),
            ..bevy_tokio_tasks::TokioTasksPlugin::default()
        })
}

How to spawn a background task

To spawn a background task from a Bevy system function, add a TokioTasksRuntime as a resource parameter and call the spawn_background_task function.

fn example_system(runtime: ResMut<TokioTasksRuntime>) {
    runtime.spawn_background_task(|_ctx| async move {
        println!("This task is running on a background thread");
    });
}

How to synchronize with the main thread

Often times, background tasks will need to synchronize with the main Bevy app at certain points. You may do this by calling the run_on_main_thread function on the TaskContext that is passed to each background task.

fn example_system(runtime: ResMut<TokioTasksRuntime>) {
    runtime.spawn_background_task(|mut ctx| async move {
        println!("This print executes from a background Tokio runtime thread");
        ctx.run_on_main_thread(move |ctx| {
            // The inner context gives access to a mutable Bevy World reference.
            let _world: &mut World = ctx.world;
        }).await;
    });
}

Examples

  • change_clear_color - This example spawns a background task which runs forever. Every second, the background task updates the app's background clear color. This demonstrates how background tasks can synchronize with the main thread to update game state.
  • current_thread_runtime - This example demonstrates how you can customize the Tokio Runtime. It configures a current_thread Runtime instead of a multi-threading Runtime.
  • async_fn - Does the same thing as the change_clear_color example, except that it shows how you can pass an async fn to spawn_background_task.
  • shutdown_after_sleep - This example spawns a background task which sleeps for 120 Bevy game updates, then shuts down the Bevy app.

Version Compatibility

This crate's major and minor version numbers will match Bevy's. To allow this crate to publish updates between Bevy updates, the patch version is allowed to increment independent of Bevy's release cycle.

bevy-tokio-tasks version bevy version tokio version
0.11.0 0.11.0 1
0.10.2 0.10.1 1
0.10.1 0.10.0 1
0.10.0 0.10.0 1
0.9.5 0.9.1 1
0.9.4 0.9.1 1
0.9.3 0.9.1 1
0.9.2 0.9.1 1
0.9.1 0.9.1 1
0.9.0 0.9.1 1

bevy-tokio-tasks's People

Contributors

ekardnt avatar joehowarth avatar keithsharp avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.