Coder Social home page Coder Social logo

bevy-async-ecs's Introduction

๐Ÿ”„ Bevy Async ECS

License: MIT Doc Crate Bevy tracking

What is Bevy Async ECS?

Bevy Async ECS is an asynchronous interface to the standard Bevy World. It aims to be simple and intuitive to use for those familiar with Bevy's ECS.

AsyncWorld

AsyncWorld is the entrypoint for all further asynchronous manipulation of the world. It can only be created using the FromWorld trait implementation. It should be driven by an executor running parallel with the main Bevy app (this can either be one of the TaskPools or a blocking executor running on another thread).

Internally, the AsyncWorld simply wraps an MPSC channel sender. As such, it can be cheaply cloned and further sent to separate threads or tasks. This means that all operations on the AsyncWorld are processed in FIFO order. However, there are no ordering guarantees between AsyncWorlds or any derivatives sharing the same internal channel sender, or any AsyncWorlds constructed separately.

It is important to note that Bevy is still running and mutating the world while the async tasks run! Assume that the world could have been mutated between any asynchronous call. However, there are several ways to ensure that multiple commands are applied together, without mutation of the world in between:

  • Construct a vanilla Bevy CommandQueue, and send it to the Bevy World with CommandQueueSender::send_queue()
  • Use the queue builder provided by the AsyncWorld via AsyncWorld::start_queue()

Basic example

use bevy::prelude::*;
use bevy::tasks::AsyncComputeTaskPool;
use bevy_async_ecs::*;

// vanilla Bevy system
fn print_names(query: Query<(Entity, &Name)>) {
	for (id, name) in query.iter() {
		info!("entity {:?} has name '{}'", id, name);
	}
}

fn main() {
	App::new()
		.add_plugins((DefaultPlugins, AsyncEcsPlugin))
		.add_systems(Startup, |world: &mut World| {
			let async_world = AsyncWorld::from_world(world);
			let fut = async move {
				let print_names = async_world.register_system(print_names).await;

				let entity = async_world.spawn_named("Frank").await;
				print_names.run().await;
				entity.despawn().await;
			};
			AsyncComputeTaskPool::get().spawn(fut).detach();
		})
		.run();
}

Multithreaded

bevy-async-ecs does not explicitly require the multi-threaded feature (though all the tests and examples do). However, when the task executor is running on a single thread (on wasm, for example), the async world will probably deadlock. If this is a pain-point for you, please open a GitHub issue.

Most recently compatible versions

bevy bevy-async-ecs
0.13 0.5.1
0.12 0.4.1
0.11 N/A

bevy-async-ecs's People

Contributors

dlom avatar databasedav 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.