Skip to content

docs: Improve docs #236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/bevy_mod_scripting_core/src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ impl Deref for ScriptComponent {

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

Expand Down
19 changes: 15 additions & 4 deletions docs/src/Summary/running-scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,39 @@ Scripts can run logic either when loaded or when triggered by an event. For exam

```lua
print("hello from load time")
function on_event()
function on_event(arg1)
print("hello from event time")
print(arg1)
end
```

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.

In order to trigger `on_event` you need to first define a label, then send an event containing the label:
```rust,ignore

#[derive(Reflect)]
pub struct MyReflectType;

// define the label, you can define as many as you like here
callback_labels!(OnEvent => "on_event");

// trigger the event
fn send_event(mut writer: EventWriter<ScriptCallbackEvent>) {
fn send_event(mut writer: EventWriter<ScriptCallbackEvent>, mut allocator: ResMut<AppReflectAllocator>) {

let allocator = allocator.write();
let my_reflect_payload = ReflectReference::new_allocated(MyReflectType, &mut allocator);

writer.send(ScriptCallbackEvent::new_for_all(
OnEvent,
vec![ScriptValue::Unit],
vec![my_reflect_payload.into()],
));
}
```

Note the second argument is the payload we are sending with the event, in this case we are sending an empty payload.
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`.

Other variants of the `ScriptValue` enum are available for sending different types of data, such as `ScriptValue::Integer` for primtive, types.


# Event Handlers
Expand Down
3 changes: 1 addition & 2 deletions examples/game_of_life.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ fn run_script_cmd(
);
commands.spawn(ScriptComponent::new(vec![format!(
"scripts/game_of_life.{language}"
)
.into()]));
)]));
}
GameOfLifeCommand::Stop => {
// we can simply drop the handle, or manually delete, I'll just drop the handle
Expand Down
Loading