-
How would I call an async function from Bevy on WASM? I tried using the async_task example: fn test(mut commands: Commands, thread_pool: Res<AsyncComputeTaskPool>) {
commands.spawn().insert(thread_pool.spawn(async move {
// web request
}));
} but it throws
EDIT: OK I was able to get something working based on the code linked by @mvlabat. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
So first of all, it's no longer good style to add tasks directly as a component on an entity - you should instead create a wrapper component (that is because it would break if multiple libraries used the same kind of task) Additionally, it's possible that this just isn't supported yet; for example, the type being returned is Maybe you should check what bevy_assets does on wasm? |
Beta Was this translation helpful? Give feedback.
-
I wasn't able to use tasks like this in WASM as well. I had to detach For example, here's how I use The task isn't async itself, but I just needed to send a compute-heavy task to a task pool (it's a bad idea, I know, one shouldn't use the async task pool for this purpose), and I found it to be an easy hack that works for now. This compiles for WASM as well. But for web requests, I found that having a separate executor is a bit more convenient to use. It might not be in your case, but if you need more complex logic like listening to a websocket, periodic polling, it doesn't probably fit into a concept of a short-lived task. I picked My game systems also communicate with it with channels. For example, here's my UI system sending an auth request on a button click: And here it's reading another channel for responses: While serving requests is happening in a long-living future that reads the messages in an endless loop: I don't claim it's 100% a good pattern, but it's something I've found working well enough for me. If folks here have opinions about this approach, I'm also curious and will be happy to learn different methods of doing asynchronous work in Bevy. |
Beta Was this translation helpful? Give feedback.
-
Bit of an old thread, but I recently ran into a similar issue (needed to await a blocking call in non-async context). Shopped around a bit and found this new library: https://crates.io/crates/bevy-async-task Could be useful some. For me, it shrinks my code to: AsyncTask::new(async { blow_out_butt_cork().await }).blocking_recv() |
Beta Was this translation helpful? Give feedback.
I wasn't able to use tasks like this in WASM as well. I had to detach
FakeTask
, and then use channels to communicate between the task and my game systems.For example, here's how I use
AsyncComputeTaskPool
in my project:https://github.com/mvlabat/muddle-run/blob/b02374bf90f29a246c39d89ebf35ba49f53865b4/libs/shared_lib/src/game/level.rs#L161
The task isn't async itself, but I just needed to send a compute-heavy task to a task pool (it's a bad idea, I know, one shouldn't use the async task pool for this purpose), and I found it to be an easy hack that works for now.
This compiles for WASM as well.
But for web requests, I found that having a separate executor is a bit more convenient to use…