Skip to content

Commit fc2e522

Browse files
authored
docs: Improve docs (#236)
* add more info about sending reflect types as payloads, streamline interface * add arg info to callback
1 parent f1b93bd commit fc2e522

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

crates/bevy_mod_scripting_core/src/script.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ impl Deref for ScriptComponent {
2626

2727
impl ScriptComponent {
2828
/// Creates a new [`ScriptComponent`] with the given ScriptID's
29-
pub fn new(components: Vec<ScriptId>) -> Self {
30-
Self(components)
29+
pub fn new<S: Into<ScriptId>, I: IntoIterator<Item = S>>(components: I) -> Self {
30+
Self(components.into_iter().map(Into::into).collect())
3131
}
3232
}
3333

docs/src/Summary/running-scripts.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,39 @@ Scripts can run logic either when loaded or when triggered by an event. For exam
1313

1414
```lua
1515
print("hello from load time")
16-
function on_event()
16+
function on_event(arg1)
1717
print("hello from event time")
18+
print(arg1)
1819
end
1920
```
2021

2122
Will print "hello from load time" when the script is loaded, and "hello from event time" when the script receives an event targeting the `on_event` callback with a receiver list including this script or entity.
2223

2324
In order to trigger `on_event` you need to first define a label, then send an event containing the label:
2425
```rust,ignore
26+
27+
#[derive(Reflect)]
28+
pub struct MyReflectType;
29+
2530
// define the label, you can define as many as you like here
2631
callback_labels!(OnEvent => "on_event");
2732
2833
// trigger the event
29-
fn send_event(mut writer: EventWriter<ScriptCallbackEvent>) {
34+
fn send_event(mut writer: EventWriter<ScriptCallbackEvent>, mut allocator: ResMut<AppReflectAllocator>) {
35+
36+
let allocator = allocator.write();
37+
let my_reflect_payload = ReflectReference::new_allocated(MyReflectType, &mut allocator);
38+
3039
writer.send(ScriptCallbackEvent::new_for_all(
3140
OnEvent,
32-
vec![ScriptValue::Unit],
41+
vec![my_reflect_payload.into()],
3342
));
3443
}
3544
```
3645

37-
Note the second argument is the payload we are sending with the event, in this case we are sending an empty payload.
46+
Note the second argument is the payload we are sending with the event, in this case we are sending an arbitrary reflect type `MyReflectType`. This can be any type you like, as long as it implements `Reflect`.
47+
48+
Other variants of the `ScriptValue` enum are available for sending different types of data, such as `ScriptValue::Integer` for primtive, types.
3849

3950

4051
# Event Handlers

examples/game_of_life.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ fn run_script_cmd(
6161
);
6262
commands.spawn(ScriptComponent::new(vec![format!(
6363
"scripts/game_of_life.{language}"
64-
)
65-
.into()]));
64+
)]));
6665
}
6766
GameOfLifeCommand::Stop => {
6867
// we can simply drop the handle, or manually delete, I'll just drop the handle

0 commit comments

Comments
 (0)