Skip to content

Commit 36c426d

Browse files
committed
modify docs
1 parent 4e6e162 commit 36c426d

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

docs/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- [Controlling Script Bindings](./Summary/controlling-script-bindings.md)
1010
- [Modifying Script Contexts](./Summary/customizing-script-contexts.md)
1111
- [Contexts](./Summary/contexts.md)
12+
- [Callbacks](./Summary/callbacks.md)
1213
- [Script Systems](./ScriptSystems/introduction.md)
1314
- [Examples](./Examples/introduction.md)
1415

docs/src/ScriptingReference/core-callbacks.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# Core Callbacks
22

3-
On top of callbacks which are registered by your application, BMS provides a set of core callbacks which are always available.
3+
On top of callbacks which are registered by your application, BMS provides a set of core callbacks which are always available (unless disabled via plugin settings).
44

55
The three core callbacks are:
66
- `on_script_loaded`
77
- `on_script_unloaded`
88
- `on_script_reloaded`
99

10+
For more information on how callbacks generally work see [the callbacks section](../Summary/callbacks.md).
11+
1012
## `on_script_loaded`
1113

1214
This will be called right after a script has been loaded or reloaded. This is a good place to initialize your script. You should avoid placing a lot of logic into the global body of your script, and instead put it into this callback. Otherwise errors in the initialization will fail the loading of the script.

docs/src/Summary/callbacks.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Callbacks
2+
3+
Callbacks generally refer to hooks called either manually or via rust event handlers, on scripts which can choose to subscribe to them.
4+
5+
Callbacks come in two variants:
6+
- Freestanding, top level functions
7+
- Registered, or "frozen" callbacks
8+
9+
## Freestanding callbacks
10+
An example of a freestanding callback:
11+
```lua
12+
function on_script_loaded()
13+
print("doing things")
14+
end
15+
```
16+
17+
this callback is refered to by "name", and on the rust side can be invoked either via `RunScriptCallback` command, or by setting up an event handler system which passes on `ScriptCallbackEvent`'s to scripts.
18+
19+
The key thing to note about this type of callback, is that if the script is ever reloaded, and the contents of this callback change, the logic inside it will also be hot-reloaded.
20+
21+
## Registerd Callbacks
22+
You can also register a callback like so:
23+
```lua
24+
register_callback("on_script_loaded", my_registered_callback)
25+
26+
function my_registered_callback()
27+
print("doing things")
28+
end
29+
```
30+
31+
Registered callbacks, take priority over freestanding ones, and contrary to freestanding callbacks, they are "frozen". I.e. once a callback is registereed in this manner,
32+
hot reloads won't affect the logic inside them.
33+
34+
This works well when using shared contexts, where scripts will overwrite top level functions when being loaded. You can use the `on_script_loaded` callback to register all your scripts callbacks while they are loaded as top level functions, and when future loads happen, every callback will be issued correctly.
35+
36+
This functionality is implemented at script plugin level, so some languages might not support this. All core languages do however.

docs/src/Summary/contexts.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ app.add_plugins(LuaScriptingPlugin::default().set_context_policy(
1414
));
1515
```
1616

17+
<div class="warning">
18+
19+
Shared contexts require your scripts to be more careful about how they register callbacks due to the nature of top level functions belonging to the "currently loaded" global script. See [the callbacks section](./callbacks.md)
20+
21+
</div>
22+
1723
### Per Script Context
1824
A per script context provides each script with their own context. However, scripts may be attached to multiple entities, in which case a single script context is shared by multiple entities.
1925

0 commit comments

Comments
 (0)