#bevy #reqwest #mod

bevy_mod_reqwest

use reqwest with the bevy runtime in the same way on both web and native

11 releases

0.12.0 Nov 13, 2023
0.11.2 Oct 9, 2023
0.11.1 Aug 13, 2023
0.11.0 Jul 12, 2023
0.10.5 Apr 27, 2023

#435 in Game dev

Download history 81/week @ 2023-08-13 61/week @ 2023-08-20 48/week @ 2023-08-27 28/week @ 2023-09-03 16/week @ 2023-09-10 35/week @ 2023-09-17 22/week @ 2023-09-24 19/week @ 2023-10-01 61/week @ 2023-10-08 18/week @ 2023-10-15 13/week @ 2023-10-22 41/week @ 2023-10-29 16/week @ 2023-11-05 39/week @ 2023-11-12 25/week @ 2023-11-19 76/week @ 2023-11-26

156 downloads per month

MIT license

22KB
153 lines

bevy_mod_reqwest

This crate helps when trying to use reqwest with bevy, without having to deal with async stuff, and it works on both web and and native ( only tested on x86_64 and wasm for now)

Example

use bevy::{log::LogPlugin, prelude::*};
use bevy_mod_reqwest::*;

fn main() {
    App::new()
        .add_plugins((MinimalPlugins, LogPlugin::default(), ReqwestPlugin))
        .add_systems(Update, (send_requests, handle_responses,))
        .insert_resource(ReqTimer(Timer::new(
            std::time::Duration::from_secs(1),
            TimerMode::Repeating,
        )))
        .run();
}

#[derive(Resource)]
struct ReqTimer(pub Timer);

fn send_requests(mut commands: Commands, time: Res<Time>, mut timer: ResMut<ReqTimer>) {
    timer.0.tick(time.delta());

    if timer.0.just_finished() {
        if let Ok(url) = "https://www.boredapi.com/api/activity".try_into() {
            let req = reqwest::Request::new(reqwest::Method::GET, url);
            let req = ReqwestRequest::new(req);
            commands.spawn(req);
        }
    }
}

fn handle_responses(mut commands: Commands, results: Query<(Entity, &ReqwestBytesResult)>) {
    for (e, res) in results.iter() {
        let string = res.as_str().unwrap();
        bevy::log::info!("{string}");

        // Done with this entity
        commands.entity(e).despawn_recursive();
    }
}

Dependencies

~23–65MB
~1M SLoC