bevywasm & bevywasm_sys

Mod your Bevy games with WebAssembly!

See examples/cubes for a comprehensive example of how to use this.

Protocol

Our protocol crate defines the two message types for communicating between the game and mods.

```rust use serde::{Deserialize, Serialize};

/// A message to be sent Mod -> Game.

[derive(Clone, Serialize, Deserialize, Debug)]

pub enum ModMessage { Hello, }

/// A message to be sent Game -> Mod.

[derive(Clone, Serialize, Deserialize, Debug)]

pub enum GameMessage { HiThere, } ```

Game

Our game will import WasmPlugin from bevy_wasm, and use it to automatically send and receive messages with the mods.

```rust use bevy::prelude::*; use bevywasm::WasmPlugin; use mygames_protocol::{GameMessage, ModMessage};

fn main() { let startupmods = vec![ includebytes!("somemod.wasm"), includebytes!("someothermod.wasm"), ];

App::build()
    .add_plugins(DefaultPlugins)
    .add_plugin(WasmPlugin::<GameMessage, ModMessage>::new(startup_mods))
    .add_system(listen_for_mod_messages)
    .add_system(send_messages_to_mods)
    .run();

}

fn listenformod_messages(mut events: EventReader) { for event in events.iter() { match event { ModMessage::Hello => { println!("The mod said hello!"); } } } }

fn sendmessagesto_mods(mut events: EventWriter) { events.send(GameMessage::HiThere); } ```

Mod

Our mod will import FFIPlugin from bevy_wasm_sys, and use it to automatically send and receive messages with the game.

```rust use bevywasmsys::prelude::*; use mygamesprotocol::{GameMessage, ModMessage};

[no_mangle]

pub unsafe extern "C" fn buildapp() { App::new() .addplugin(FFIPlugin::::default()) .addsystem(listenforgamemessages) .addsystem(sendmessagestogame) .run(); }

fn listenforgame_messages(mut events: EventReader) { for event in events.iter() { match event { GameMessage::HiThere => { println!("The game said hi there!"); } } } }

fn sendmessagesto_game(mut events: EventWriter) { events.send(ModMessage::Hello); } ```