Code:
use std::{clone, future};
use bevy::{
ecs::system::CommandQueue,
prelude::*,
tasks::{prelude::*, Task},
};
use futures_lite::{future::block_on, FutureExt};
pub struct ViewWorldPlugin;
impl Plugin for ViewWorldPlugin {
fn build(&self, app: &mut App) {
app.add_state::<LoadingState>()
.add_systems(Startup, spawn_tasks)
.add_systems(Update, handle_tasks)
.add_systems(OnExit(LoadingState::Loading), draw_map);
}
}
#[derive(States, Debug, Hash, PartialEq, Eq, Clone, Default)]
pub enum LoadingState {
#[default]
Loading,
Loaded,
}
#[derive(Resource, Debug)]
pub struct Map {
tiles: Vec<Tile>,
}
#[derive(Debug)]
pub struct Tile {
x: i32,
y: i32,
z: i32,
}
#[derive(Component)]
pub struct WorldGen(Task<String>);
fn spawn_tasks(mut commands: Commands) {
let thread_pool = AsyncComputeTaskPool::get();
let entity = commands.spawn_empty().id();
let task = thread_pool.spawn(async move {
let response = reqwest::get("https://localhost:8000/get_world")
.await
.unwrap()
.text()
.await
.unwrap();
response
});
// Spawn new entity and add our new task as a component
commands.entity(entity).insert(WorldGen(task));
}
fn handle_tasks(mut commands: Commands, mut transform_tasks: Query<&mut WorldGen>) {
for mut task in &mut transform_tasks {
if let Some(mut commands_queue) = block_on(future::poll_fn(&mut task.0)) {
println!(commands_queue);
}
}
}
fn draw_map(mut commands: Commands, world: Res<Map>) {
print!("world: {:?}", world)
}
Error:
expected a `FnMut(&mut Context<'_>)` closure, found `Task<std::string::String>`
the trait `for<'a, 'b> FnMut<(&'a mut Context<'b>,)>` is not implemented for `Task<std::string::String>`
required for `&mut Task<std::string::String>` to implement `for<'a, 'b> FnOnce<(&'a mut Context<'b>,)>`
required for `std::future::PollFn<&mut Task<std::string::String>>` to implement `Future`rustcClick for full compiler diagnostic
view_world_plugin.rs(63, 43): required by a bound introduced by this call
future.rs(56, 33): required by a bound in `futures_lite::future::block_on`