|
| 1 | +# Managing Scripts |
| 2 | + |
| 3 | +Scripts live in the standard bevy `assets` directory. Loading a script means: |
| 4 | +- Parsing the script body |
| 5 | +- Creating or updating the resources which store script state |
| 6 | +- Assigning a name/id to the script so it can be referred to by the rest of the application. |
| 7 | + |
| 8 | +## Loading |
| 9 | +BMS listens to `ScriptAsset` events and reacts accordingly. In order to load a script, all you need to do is request a handle to it via the asset server and store it somewhere. |
| 10 | + |
| 11 | +Below is an example system which loads a script called `assets/my_script.lua` and stores the handle in a local system parameter: |
| 12 | + |
| 13 | +```rust,ignore |
| 14 | +fn load_script(server: Res<AssetServer>, mut handle: Local<Handle<ScriptAsset>>) { |
| 15 | + let handle_ = server.load::<ScriptAsset>("my_script.lua"); |
| 16 | + *handle = handle_; |
| 17 | +} |
| 18 | +``` |
| 19 | + |
| 20 | +In practice you will likely store this handle in a resource or component, when your load all the scripts necessary for your application. |
| 21 | + |
| 22 | + |
| 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 | +``` |
| 29 | + |
| 30 | +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. |
| 31 | + |
| 32 | +## Hot-loading scripts |
| 33 | +To enable hot-loading of assets, you need to enable the necessary bevy features as normal [see the bevy cheatbook for instructions](https://bevy-cheatbook.github.io/assets/hot-reload.html). |
| 34 | + |
| 35 | +Assuming that hot-reloading is enabled for your app, any changes to script assets will automatically be picked up and the scripts re-loaded. |
| 36 | + |
| 37 | +## Manually (re)loading scripts |
| 38 | +In order to manually re-load or load a script you can issue the `CreateOrUpdateScript` command: |
| 39 | + |
| 40 | +```rust,ignore |
| 41 | +CreateOrUpdateScript::new("my_script.lua".into(), "print(\"hello world from new script body\")".into(), asset_handle) |
| 42 | +``` |
| 43 | + |
| 44 | +## Loading timeframe |
| 45 | +Scripts are processed via commands, so any asset events will be processed at the next command execution point running after BMS internal asset systems. |
0 commit comments