Skip to content

Commit 144eaae

Browse files
committed
change handle to weak so unloading strong handle is enough to delete script, and update docs
1 parent 6a0ef28 commit 144eaae

File tree

4 files changed

+60
-9
lines changed

4 files changed

+60
-9
lines changed

crates/bevy_mod_scripting_core/src/systems.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub fn sync_script_data<C: Context, R: Runtime>(
7070
commands.queue(CreateOrUpdateScript::<C, R>::new(
7171
script_id,
7272
asset.content.clone(),
73-
Some(script_assets.reserve_handle()),
73+
Some(script_assets.reserve_handle().clone_weak()),
7474
));
7575
} else {
7676
commands.queue(DeleteScript::<C, R>::new(script_id));

docs/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
# Quick Start
66

77
- [Managing Scripts](./managing-scripts.md)
8+
- [Running Scripts](./running-scripts.md)
89
- [Controlling Script Bindings](./controlling-script-bindings.md)

docs/src/managing-scripts.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,11 @@ fn load_script(server: Res<AssetServer>, mut handle: Local<Handle<ScriptAsset>>)
1717
}
1818
```
1919

20-
In practice you will likely store this handle in a resource or component, when your load all the scripts necessary for your application.
20+
In practice you will likely store this handle in a resource or component, when your load all the scripts necessary for your application.
2121

22+
## Unloading
23+
Scripts are automatically unloaded when the asset is dropped. This means that if you have a handle to a script and it goes out of scope, the script will be unloaded.
2224

23-
## Deleting scripts
24-
In order to delete a previously loaded script, you will need to issue a `DeleteScript` command like so:
25-
26-
```rust,ignore
27-
DeleteScript::new("my_script.lua".into())
28-
```
2925

3026
This will delete references to the script and remove any internal handles to the asset. You will also need to clean up any handles to the asset you hold in your application in order for the asset to be unloaded.
3127

@@ -41,5 +37,12 @@ In order to manually re-load or load a script you can issue the `CreateOrUpdateS
4137
CreateOrUpdateScript::new("my_script.lua".into(), "print(\"hello world from new script body\")".into(), asset_handle)
4238
```
4339

44-
## Loading timeframe
40+
## Manually Deleting scripts
41+
In order to delete a previously loaded script, you will need to issue a `DeleteScript` command like so:
42+
43+
```rust,ignore
44+
DeleteScript::new("my_script.lua".into())
45+
```
46+
47+
## Loading/Unloading timeframe
4548
Scripts are processed via commands, so any asset events will be processed at the next command execution point running after BMS internal asset systems.

docs/src/running-scripts.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Attaching Scripts
2+
3+
Once you have scripts discovered and loaded, you'll want to run them. At the moment BMS supports one method of triggering scripts, and that is by attaching them to entities via `ScriptComponent`'s and then sending script event's which trigger callbacks on the scripts.
4+
5+
In order to attach a script and make it runnable simply add a `ScriptComponent` to an entity
6+
```rust,ignore
7+
commands.entity(my_entity).insert(ScriptComponent::new(vec!["my_script.lua", "my_other_script.lua"]));
8+
```
9+
10+
# Running Scripts
11+
12+
Scripts can run logic either when loaded or when triggered by an event. For example the script:
13+
14+
```lua
15+
print("hello from load time")
16+
function on_event()
17+
print("hello from event time")
18+
end
19+
```
20+
21+
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.
22+
23+
In order to trigger `on_event` you need to first define a label, then send an event containing the label:
24+
```rust,ignore
25+
// define the label
26+
struct OnEventCallback;
27+
impl IntoCallbackLabel for OnEventCallback {
28+
fn into_callback_label() -> CallbackLabel {
29+
"on_event".into()
30+
}
31+
}
32+
33+
// trigger the event
34+
fn send_event(mut writer: EventWriter<ScriptCallbackEvent<()>>) {
35+
writer.send(ScriptCallbackEvent::new_for_all(
36+
OnEventCallback::into_callback_label(),
37+
(),
38+
));
39+
}
40+
```
41+
42+
Note the `()` corresponds to the payload for the event, i.e. in this case we are not sending any arguments.
43+
44+
TODO: this should be replaced with `ScriptValue` before release
45+
```
46+
assert!(false, "TODO: replace with ScriptValue");
47+
```

0 commit comments

Comments
 (0)