Skip to content

Commit ca05cce

Browse files
committed
start cleaning up examples, use ScriptValue as argument for all things
1 parent 9adbe3f commit ca05cce

File tree

15 files changed

+108
-463
lines changed

15 files changed

+108
-463
lines changed

Cargo.toml

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,6 @@ name = "console_integration_rhai"
123123
path = "examples/rhai/console_integration.rs"
124124
required-features = ["rhai", "bevy/file_watcher", "bevy/multi_threaded"]
125125

126-
[[example]]
127-
name = "complex_game_loop_lua"
128-
path = "examples/lua/complex_game_loop.rs"
129-
required-features = ["lua54"]
130126

131127
[[example]]
132128
name = "dynamic_queries_lua"
@@ -148,15 +144,6 @@ required-features = ["rhai", "bevy/file_watcher", "bevy/multi_threaded"]
148144
name = "game_of_life_rhai"
149145
path = "examples/rhai/game_of_life.rs"
150146

151-
[[example]]
152-
name = "event_recipients_lua"
153-
path = "examples/lua/event_recipients.rs"
154-
required-features = ["lua54"]
155-
156-
[[example]]
157-
name = "coroutines_lua"
158-
path = "examples/lua/coroutines.rs"
159-
required-features = ["lua54"]
160147

161148
[[example]]
162149
name = "bevy_api_lua"
@@ -168,10 +155,6 @@ name = "bevy_api_rhai"
168155
path = "examples/rhai/bevy_api.rs"
169156
required-features = ["rhai"]
170157

171-
[[example]]
172-
name = "wrappers"
173-
path = "examples/wrappers.rs"
174-
required-features = ["lua54"]
175158

176159
[[example]]
177160
name = "minimal_rune"

crates/bevy_mod_scripting_core/src/event.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use bevy::{ecs::entity::Entity, prelude::Event};
22

3-
use crate::{error::ScriptError, handler::Args, script::ScriptId};
3+
use crate::{error::ScriptError, handler::Args, prelude::ScriptValue, script::ScriptId};
44

55
/// An error coming from a script
66
#[derive(Debug, Event)]
@@ -50,6 +50,23 @@ impl CallbackLabel {
5050
}
5151
}
5252

53+
#[macro_export]
54+
macro_rules! callback_labels {
55+
($($name:ident => $label:expr),*) => {
56+
pub enum CallbackLabels {
57+
$($name),*
58+
}
59+
60+
impl IntoCallbackLabel for CallbackLabels {
61+
fn into_callback_label() -> CallbackLabel {
62+
match self {
63+
$(CallbackLabels::$name => $label.into()),*
64+
}
65+
}
66+
}
67+
};
68+
}
69+
5370
pub trait IntoCallbackLabel {
5471
fn into_callback_label() -> CallbackLabel;
5572
}
@@ -90,22 +107,26 @@ pub enum Recipients {
90107

91108
/// A callback event meant to trigger a callback in a subset/set of scripts in the world with the given arguments
92109
#[derive(Clone, Event, Debug)]
93-
pub struct ScriptCallbackEvent<A: Args> {
110+
pub struct ScriptCallbackEvent {
94111
pub label: CallbackLabel,
95112
pub recipients: Recipients,
96-
pub args: A,
113+
pub args: Vec<ScriptValue>,
97114
}
98115

99-
impl<A: Args> ScriptCallbackEvent<A> {
100-
pub fn new<L: Into<CallbackLabel>>(label: L, args: A, recipients: Recipients) -> Self {
116+
impl ScriptCallbackEvent {
117+
pub fn new<L: Into<CallbackLabel>>(
118+
label: L,
119+
args: Vec<ScriptValue>,
120+
recipients: Recipients,
121+
) -> Self {
101122
Self {
102123
label: label.into(),
103124
args,
104125
recipients,
105126
}
106127
}
107128

108-
pub fn new_for_all<L: Into<CallbackLabel>>(label: L, args: A) -> Self {
129+
pub fn new_for_all<L: Into<CallbackLabel>>(label: L, args: Vec<ScriptValue>) -> Self {
109130
Self::new(label, args, Recipients::All)
110131
}
111132
}

crates/bevy_mod_scripting_core/src/handler.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ use bevy::ecs::{entity::Entity, system::Resource, world::World};
33
use crate::{
44
context::{Context, ContextPreHandlingInitializer},
55
event::CallbackLabel,
6-
prelude::ScriptError,
6+
prelude::{ScriptError, ScriptValue},
77
runtime::Runtime,
88
script::ScriptId,
99
};
1010

1111
pub trait Args: Clone + Send + Sync + 'static {}
1212
impl<T: Clone + Send + Sync + 'static> Args for T {}
1313

14-
pub type HandlerFn<A, C, R> = fn(
15-
args: A,
14+
pub type HandlerFn<C, R> = fn(
15+
args: Vec<ScriptValue>,
1616
entity: Entity,
1717
script_id: &ScriptId,
1818
callback: &CallbackLabel,
@@ -24,11 +24,11 @@ pub type HandlerFn<A, C, R> = fn(
2424

2525
/// A resource that holds the settings for the callback handler for a specific combination of type parameters
2626
#[derive(Resource)]
27-
pub struct CallbackSettings<A: Args, C: Context, R: Runtime> {
28-
pub callback_handler: Option<HandlerFn<A, C, R>>,
27+
pub struct CallbackSettings<C: Context, R: Runtime> {
28+
pub callback_handler: Option<HandlerFn<C, R>>,
2929
}
3030

31-
impl<A: Args, C: Context, R: Runtime> Default for CallbackSettings<A, C, R> {
31+
impl<C: Context, R: Runtime> Default for CallbackSettings<C, R> {
3232
fn default() -> Self {
3333
Self {
3434
callback_handler: None,

crates/bevy_mod_scripting_core/src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,20 @@ pub mod prelude {
4040
}
4141

4242
/// Bevy plugin enabling scripting within the bevy mod scripting framework
43-
pub struct ScriptingPlugin<A: Args, C: Context, R: Runtime> {
43+
pub struct ScriptingPlugin<C: Context, R: Runtime> {
4444
/// Callback for initiating the runtime
4545
pub runtime_builder: fn() -> R,
4646
/// Settings for the runtime
4747
pub runtime_settings: Option<RuntimeSettings<R>>,
4848
/// The handler used for executing callbacks in scripts
49-
pub callback_handler: Option<HandlerFn<A, C, R>>,
49+
pub callback_handler: Option<HandlerFn<C, R>>,
5050
/// The context builder for loading contexts
5151
pub context_builder: Option<ContextBuilder<C, R>>,
5252
/// The context assigner for assigning contexts to scripts, if not provided default strategy of keeping each script in its own context is used
5353
pub context_assigner: Option<ContextAssigner<C>>,
5454
}
5555

56-
impl<A: Args, C: Context, R: Runtime + Default> Default for ScriptingPlugin<A, C, R> {
56+
impl<C: Context, R: Runtime + Default> Default for ScriptingPlugin<C, R> {
5757
fn default() -> Self {
5858
Self {
5959
runtime_builder: R::default,
@@ -65,10 +65,10 @@ impl<A: Args, C: Context, R: Runtime + Default> Default for ScriptingPlugin<A, C
6565
}
6666
}
6767

68-
impl<A: Args, C: Context, R: Runtime> Plugin for ScriptingPlugin<A, C, R> {
68+
impl<C: Context, R: Runtime> Plugin for ScriptingPlugin<C, R> {
6969
fn build(&self, app: &mut bevy::prelude::App) {
7070
app.add_event::<ScriptErrorEvent>()
71-
.add_event::<ScriptCallbackEvent<A>>()
71+
.add_event::<ScriptCallbackEvent>()
7272
.init_resource::<AppReflectAllocator>()
7373
.init_resource::<ScriptAssetSettings>()
7474
.init_resource::<Scripts>()
@@ -84,7 +84,7 @@ impl<A: Args, C: Context, R: Runtime> Plugin for ScriptingPlugin<A, C, R> {
8484
runtime: (self.runtime_builder)(),
8585
})
8686
.init_non_send_resource::<ScriptContexts<C>>()
87-
.insert_resource::<CallbackSettings<A, C, R>>(CallbackSettings {
87+
.insert_resource::<CallbackSettings<C, R>>(CallbackSettings {
8888
callback_handler: self.callback_handler,
8989
})
9090
.insert_resource::<ContextLoadingSettings<C, R>>(ContextLoadingSettings {
@@ -224,13 +224,13 @@ mod test {
224224
#[derive(Default, Clone)]
225225
struct R;
226226
app.add_plugins(AssetPlugin::default());
227-
app.add_plugins(ScriptingPlugin::<A, C, R>::default());
227+
app.add_plugins(ScriptingPlugin::<C, R>::default());
228228

229229
assert!(app.world().contains_resource::<Scripts>());
230230
assert!(app.world().contains_resource::<AppTypeRegistry>());
231231
assert!(app.world().contains_resource::<ScriptAssetSettings>());
232232
assert!(app.world().contains_resource::<RuntimeSettings<R>>());
233-
assert!(app.world().contains_resource::<CallbackSettings<A, C, R>>());
233+
assert!(app.world().contains_resource::<CallbackSettings<C, R>>());
234234
assert!(app
235235
.world()
236236
.contains_resource::<ContextLoadingSettings<C, R>>());

crates/bevy_mod_scripting_core/src/systems.rs

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ macro_rules! push_err_and_continue {
9494
pub fn event_handler<L: IntoCallbackLabel, A: Args, C: Context, R: Runtime>(
9595
world: &mut World,
9696
params: &mut SystemState<(
97-
EventReader<ScriptCallbackEvent<A>>,
98-
Res<CallbackSettings<A, C, R>>,
97+
EventReader<ScriptCallbackEvent>,
98+
Res<CallbackSettings<C, R>>,
9999
Res<ContextLoadingSettings<C, R>>,
100100
Res<Scripts>,
101101
Query<(Entity, Ref<ScriptComponent>)>,
@@ -232,6 +232,7 @@ mod test {
232232
use crate::{
233233
event::CallbackLabel,
234234
handler::HandlerFn,
235+
prelude::ScriptValue,
235236
script::{Script, ScriptId},
236237
};
237238

@@ -249,20 +250,20 @@ mod test {
249250
}
250251

251252
struct TestContext {
252-
pub invocations: Vec<String>,
253+
pub invocations: Vec<ScriptValue>,
253254
}
254255

255256
fn setup_app<L: IntoCallbackLabel + 'static, A: Args, C: Context, R: Runtime>(
256-
handler_fn: HandlerFn<A, C, R>,
257+
handler_fn: HandlerFn<C, R>,
257258
runtime: R,
258259
contexts: HashMap<u32, C>,
259260
scripts: HashMap<ScriptId, Script>,
260261
) -> App {
261262
let mut app = App::new();
262263

263-
app.add_event::<ScriptCallbackEvent<A>>();
264+
app.add_event::<ScriptCallbackEvent>();
264265
app.add_event::<ScriptErrorEvent>();
265-
app.insert_resource::<CallbackSettings<A, C, R>>(CallbackSettings {
266+
app.insert_resource::<CallbackSettings<C, R>>(CallbackSettings {
266267
callback_handler: Some(handler_fn),
267268
});
268269
app.add_systems(Update, event_handler::<L, A, C, R>);
@@ -301,7 +302,7 @@ mod test {
301302
};
302303
let mut app = setup_app::<OnTestCallback, String, TestContext, TestRuntime>(
303304
|args, entity, script, _, ctxt, _, runtime, _| {
304-
ctxt.invocations.push(args);
305+
ctxt.invocations.extend(args);
305306
runtime.invocations.push((entity, script.clone()));
306307
Ok(())
307308
},
@@ -314,11 +315,10 @@ mod test {
314315
.spawn(ScriptComponent(vec![test_script_id.clone()]))
315316
.id();
316317

317-
app.world_mut()
318-
.send_event(ScriptCallbackEvent::<String>::new_for_all(
319-
OnTestCallback::into_callback_label(),
320-
"test_args".to_owned(),
321-
));
318+
app.world_mut().send_event(ScriptCallbackEvent::new_for_all(
319+
OnTestCallback::into_callback_label(),
320+
vec![ScriptValue::String("test_args".into())],
321+
));
322322
app.update();
323323

324324
let test_context = app
@@ -336,7 +336,7 @@ mod test {
336336
.get(&test_ctxt_id)
337337
.unwrap()
338338
.invocations,
339-
vec!["test_args"]
339+
vec![ScriptValue::String("test_args".into())]
340340
);
341341

342342
assert_eq!(
@@ -389,7 +389,7 @@ mod test {
389389
};
390390
let mut app = setup_app::<OnTestCallback, String, TestContext, TestRuntime>(
391391
|args, entity, script, _, ctxt, _, runtime, _| {
392-
ctxt.invocations.push(args);
392+
ctxt.invocations.extend(args);
393393
runtime.invocations.push((entity, script.clone()));
394394
Ok(())
395395
},
@@ -402,19 +402,17 @@ mod test {
402402
.spawn(ScriptComponent(vec![test_script_id.clone()]))
403403
.id();
404404

405-
app.world_mut()
406-
.send_event(ScriptCallbackEvent::<String>::new(
407-
OnTestCallback::into_callback_label(),
408-
"test_args_script".to_owned(),
409-
crate::event::Recipients::Script(test_script_id.clone()),
410-
));
405+
app.world_mut().send_event(ScriptCallbackEvent::new(
406+
OnTestCallback::into_callback_label(),
407+
vec![ScriptValue::String("test_args_script".into())],
408+
crate::event::Recipients::Script(test_script_id.clone()),
409+
));
411410

412-
app.world_mut()
413-
.send_event(ScriptCallbackEvent::<String>::new(
414-
OnTestCallback::into_callback_label(),
415-
"test_args_entity".to_owned(),
416-
crate::event::Recipients::Entity(test_entity_id),
417-
));
411+
app.world_mut().send_event(ScriptCallbackEvent::new(
412+
OnTestCallback::into_callback_label(),
413+
vec![ScriptValue::String("test_args_entity".into())],
414+
crate::event::Recipients::Entity(test_entity_id),
415+
));
418416

419417
app.update();
420418

@@ -433,7 +431,10 @@ mod test {
433431
.get(&test_ctxt_id)
434432
.unwrap()
435433
.invocations,
436-
vec!["test_args_script", "test_args_entity"]
434+
vec![
435+
ScriptValue::String("test_args_script".into()),
436+
ScriptValue::String("test_args_entity".into())
437+
]
437438
);
438439

439440
assert_eq!(

0 commit comments

Comments
 (0)