Bevy 0.12 (#9)

* Update to bevy 0.12
This commit is contained in:
Jarosław Konik 2024-04-10 18:36:11 +02:00 committed by GitHub
parent 928f5437fc
commit 60984220a0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 35 additions and 27 deletions

View file

@ -13,9 +13,10 @@ keywords = ["bevy", "rhai", "scripting", "game", "gamedev"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
bevy = { default-features = false, version = "0.11.0", features = [ bevy = { default-features = false, version = "0.12.0", features = [
"bevy_asset", "bevy_asset",
] } ] }
serde = "1.0.162" serde = "1.0.162"
rhai = { version = "1.14.0", features = ["sync", "internals", "unchecked"] } rhai = { version = "1.14.0", features = ["sync", "internals", "unchecked"] }
thiserror = "1.0.40" thiserror = "1.0.40"
anyhow = "1.0.82"

View file

@ -144,6 +144,7 @@ The examples live in `examples` directory and their corresponding scripts live i
| bevy version | bevy_scriptum version | | bevy version | bevy_scriptum version |
|--------------|----------------------| |--------------|----------------------|
| 0.12 | 0.3 |
| 0.11 | 0.2 | | 0.11 | 0.2 |
| 0.10 | 0.1 | | 0.10 | 0.1 |

View file

@ -10,7 +10,7 @@ fn main() {
loop { loop {
if let Some(app_exit_events) = app.world.get_resource_mut::<Events<AppExit>>() { if let Some(app_exit_events) = app.world.get_resource_mut::<Events<AppExit>>() {
if app_exit_event_reader if app_exit_event_reader
.iter(&app_exit_events) .read(&app_exit_events)
.last() .last()
.is_some() .is_some()
{ {
@ -21,7 +21,7 @@ fn main() {
} }
}) })
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_plugins(ScriptingPlugin::default()) .add_plugins(ScriptingPlugin)
.add_systems(Startup, startup) .add_systems(Startup, startup)
.add_systems(Update, call_rhai_on_update_from_rust) .add_systems(Update, call_rhai_on_update_from_rust)
.add_script_function(String::from("quit"), |mut exit: EventWriter<AppExit>| { .add_script_function(String::from("quit"), |mut exit: EventWriter<AppExit>| {

View file

@ -4,7 +4,7 @@ use bevy_scriptum::{prelude::*, Script};
fn main() { fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_plugins(ScriptingPlugin::default()) .add_plugins(ScriptingPlugin)
.add_script_function( .add_script_function(
String::from("get_name"), String::from("get_name"),
|In((entity,)): In<(Entity,)>, names: Query<&Name>| { |In((entity,)): In<(Entity,)>, names: Query<&Name>| {

View file

@ -4,7 +4,7 @@ use bevy_scriptum::{prelude::*, Script, ScriptingRuntime};
fn main() { fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_plugins(ScriptingPlugin::default()) .add_plugins(ScriptingPlugin)
.add_script_function(String::from("hello_bevy"), || { .add_script_function(String::from("hello_bevy"), || {
println!("hello bevy, called from script"); println!("hello bevy, called from script");
}) })

View file

@ -7,7 +7,7 @@ struct Player;
fn main() { fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_plugins(ScriptingPlugin::default()) .add_plugins(ScriptingPlugin)
.add_script_function( .add_script_function(
String::from("print_player_names"), String::from("print_player_names"),
|players: Query<&Name, With<Player>>| { |players: Query<&Name, With<Player>>| {

View file

@ -4,7 +4,7 @@ use bevy_scriptum::{prelude::*, Script};
fn main() { fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_plugins(ScriptingPlugin::default()) .add_plugins(ScriptingPlugin)
.add_systems(Startup, startup) .add_systems(Startup, startup)
.run(); .run();
} }

View file

@ -5,7 +5,7 @@ use rhai::ImmutableString;
fn main() { fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_plugins(ScriptingPlugin::default()) .add_plugins(ScriptingPlugin)
.add_script_function(String::from("fun_without_params"), || { .add_script_function(String::from("fun_without_params"), || {
println!("called without params"); println!("called without params");
}) })

View file

@ -4,7 +4,7 @@ use bevy_scriptum::{prelude::*, Script};
fn main() { fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_plugins(ScriptingPlugin::default()) .add_plugins(ScriptingPlugin)
.add_script_function(String::from("hello_bevy"), || { .add_script_function(String::from("hello_bevy"), || {
println!("hello bevy, called from script"); println!("hello bevy, called from script");
}) })

View file

@ -4,7 +4,7 @@ use bevy_scriptum::{prelude::*, Script};
fn main() { fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_plugins(ScriptingPlugin::default()) .add_plugins(ScriptingPlugin)
.add_script_function(String::from("hello_bevy"), hello_bevy_callback_system) .add_script_function(String::from("hello_bevy"), hello_bevy_callback_system)
.add_systems(Startup, startup) .add_systems(Startup, startup)
.run(); .run();

View file

@ -7,7 +7,7 @@ struct Player;
fn main() { fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_plugins(ScriptingPlugin::default()) .add_plugins(ScriptingPlugin)
.add_script_function( .add_script_function(
String::from("get_player_name"), String::from("get_player_name"),
|player_names: Query<&Name, With<Player>>| player_names.single().to_string(), |player_names: Query<&Name, With<Player>>| player_names.single().to_string(),

View file

@ -13,7 +13,7 @@ fn main() {
loop { loop {
if let Some(app_exit_events) = app.world.get_resource_mut::<Events<AppExit>>() { if let Some(app_exit_events) = app.world.get_resource_mut::<Events<AppExit>>() {
if app_exit_event_reader if app_exit_event_reader
.iter(&app_exit_events) .read(&app_exit_events)
.last() .last()
.is_some() .is_some()
{ {
@ -24,7 +24,7 @@ fn main() {
} }
}) })
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_plugins(ScriptingPlugin::default()) .add_plugins(ScriptingPlugin)
.add_systems(Startup, startup) .add_systems(Startup, startup)
.add_systems(Update, print_entity_names_and_quit) .add_systems(Update, print_entity_names_and_quit)
.add_script_function(String::from("spawn_entity"), spawn_entity) .add_script_function(String::from("spawn_entity"), spawn_entity)

View file

@ -1,12 +1,12 @@
use bevy::{ use bevy::{
asset::{AssetLoader, LoadContext, LoadedAsset}, asset::{io::Reader, Asset, AssetLoader, AsyncReadExt as _, LoadContext},
reflect::{TypePath, TypeUuid}, reflect::{TypePath, TypeUuid},
utils::BoxedFuture, utils::BoxedFuture,
}; };
use serde::Deserialize; use serde::Deserialize;
/// A script that can be loaded by the [crate::ScriptingPlugin]. /// A script that can be loaded by the [crate::ScriptingPlugin].
#[derive(Debug, Deserialize, TypeUuid, TypePath)] #[derive(Asset, Debug, Deserialize, TypeUuid, TypePath)]
#[uuid = "3ed4b68b-4f5d-4d82-96f6-5194e358921a"] #[uuid = "3ed4b68b-4f5d-4d82-96f6-5194e358921a"]
pub struct RhaiScript(pub String); pub struct RhaiScript(pub String);
@ -15,15 +15,22 @@ pub struct RhaiScript(pub String);
pub struct RhaiScriptLoader; pub struct RhaiScriptLoader;
impl AssetLoader for RhaiScriptLoader { impl AssetLoader for RhaiScriptLoader {
type Asset = RhaiScript;
type Settings = ();
type Error = anyhow::Error;
fn load<'a>( fn load<'a>(
&'a self, &'a self,
bytes: &'a [u8], reader: &'a mut Reader,
load_context: &'a mut LoadContext, _settings: &'a Self::Settings,
) -> BoxedFuture<'a, Result<(), bevy::asset::Error>> { _load_context: &'a mut LoadContext,
) -> BoxedFuture<'a, anyhow::Result<RhaiScript, anyhow::Error>> {
Box::pin(async move { Box::pin(async move {
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await?;
let rhai_script = RhaiScript(String::from_utf8(bytes.to_vec())?); let rhai_script = RhaiScript(String::from_utf8(bytes.to_vec())?);
load_context.set_default_asset(LoadedAsset::new(rhai_script)); Ok(rhai_script)
Ok(())
}) })
} }

View file

@ -1,5 +1,3 @@
//! ⚠️ **Pre-release, alpha version**: API is bound to change, bugs are to be expected.
//!
//! bevy_scriptum is a a plugin for [Bevy](https://bevyengine.org/) that allows you to write some of your game logic in a scripting language. //! bevy_scriptum is a a plugin for [Bevy](https://bevyengine.org/) that allows you to write some of your game logic in a scripting language.
//! Currently, only [Rhai](https://rhai.rs/) is supported, but more languages may be added in the future. //! Currently, only [Rhai](https://rhai.rs/) is supported, but more languages may be added in the future.
//! //!
@ -144,6 +142,7 @@
//! //!
//! | bevy version | bevy_scriptum version | //! | bevy version | bevy_scriptum version |
//! |--------------|----------------------| //! |--------------|----------------------|
//! | 0.12 | 0.3 |
//! | 0.11 | 0.2 | //! | 0.11 | 0.2 |
//! | 0.10 | 0.1 | //! | 0.10 | 0.1 |
//! //!
@ -218,8 +217,8 @@ pub struct ScriptingPlugin;
impl Plugin for ScriptingPlugin { impl Plugin for ScriptingPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_asset::<RhaiScript>() app.register_asset_loader(RhaiScriptLoader)
.init_asset_loader::<RhaiScriptLoader>() .init_asset::<RhaiScript>()
.init_resource::<Callbacks>() .init_resource::<Callbacks>()
.insert_resource(ScriptingRuntime::default()) .insert_resource(ScriptingRuntime::default())
.add_systems(Startup, init_engine.pipe(log_errors)) .add_systems(Startup, init_engine.pipe(log_errors))

View file

@ -48,10 +48,10 @@ pub(crate) fn reload_scripts(
mut ev_asset: EventReader<AssetEvent<RhaiScript>>, mut ev_asset: EventReader<AssetEvent<RhaiScript>>,
mut scripts: Query<(Entity, &mut Script)>, mut scripts: Query<(Entity, &mut Script)>,
) { ) {
for ev in ev_asset.iter() { for ev in ev_asset.read() {
if let AssetEvent::Modified { handle } = ev { if let AssetEvent::Modified { id } = ev {
for (entity, script) in &mut scripts { for (entity, script) in &mut scripts {
if script.script == *handle { if script.script.id() == *id {
commands.entity(entity).remove::<ScriptData>(); commands.entity(entity).remove::<ScriptData>();
} }
} }