Skip to content

Commit 1bb1b69

Browse files
committed
docs
1 parent 19ea3c9 commit 1bb1b69

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

docs/src/ScriptPipeline/pipeline.md

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,72 @@ In versions prior to `16.0` the pipeline was represented by load unload commands
99

1010
The new pipeline works like this:
1111
- A set of systems in `PostUpdate` "filter" incoming `ScriptAttachedEvent` and `ScriptDetachedEvent`'s as well as `ScriptAssetModifiedEvent`'s (some generated from asset events, others from component hooks). The filtered events are wrapped in `ForPlugin<P>(inner)` events based on the language of the underlying event's.
12-
- Then inside the `ScriptProcessingSchedule<P>`
12+
- A set of other systems then converts those into active `ScriptMachine`'s
13+
- Then inside the `ScriptProcessingSchedule<P>`, which is only run if there are active machines, we tick each machine until it either errors or reaches its final state.
14+
15+
## Script Machines
16+
17+
### State Machine
18+
Script loading is handled by the following state machines:
19+
```mermaid
20+
graph TD;
21+
A[Loading Initialized]
22+
B[Reloading Initialized \n Enter: OnReloaded callback]
23+
24+
A --[ new context ]--> C
25+
B --[ existing context]--> C
26+
C[Context Assigned \n Enter: OnReloaded + OnLoaded callbacks]
27+
28+
C --> D
29+
30+
D[Loading Completed]
31+
32+
E[Unloading Initialized \n Enter: OnUnloaded callback]
33+
F[Context Removed]
34+
G[Resident Removed]
35+
E --[Last resident]--> F
36+
E --[More residents]--> G
37+
H[Unlading Completed]
38+
F --> H
39+
G --> H
40+
```
41+
42+
Hooks, such as `on_script_loaded` are implemented using `TransitionListener`'s which can be inserted into the `ActiveMachines` resource. This allows you to hook into various parts of the loading process and add your own callbacks, i.e.:
43+
44+
```rust
45+
pub(crate) struct MyUnloadListener;
46+
impl<P: IntoScriptPluginParams> TransitionListener<UnloadingInitialized<P>>
47+
for OnUnloadedForUnloadListener
48+
{
49+
fn on_enter(
50+
&self,
51+
state: &mut UnloadingInitialized<P>,
52+
world: &mut World,
53+
ctxt: &mut Context,
54+
) -> Result<(), ScriptError> {
55+
info!("doing unload things")
56+
Ok(())
57+
}
58+
}
59+
60+
// example config, if you do this before the scripting plugin is initialized, the ordering of callbacks might be different to if you do this after.
61+
fn main(&mut app: App) {
62+
let machines = app.world_mut().get_resource_or_init::<ActiveMachines<LuaScriptingPlugin>>();
63+
machines.push_listener::<UnloadingInitialized<LuaScriptingPlugin>>(MyUnloadListener);
64+
}
65+
```
66+
67+
68+
## Timing & Order
69+
70+
The machine ticking by default is limited to 16ms per run (i.e. 60fps assuming it has the entirety of the frame to run). This is not entirely effective as some operations are not currently very fine grained, for example if a callback takes more than 16ms it will easilly hog the frame time.
71+
72+
This is however effective when a lot of scripts are loaded in at once, as loading will be spread out over multiple frames.
73+
74+
You can override this and many other settings (including disabling the core callbacks) by changing the `ScriptLoadingPipeline<P>` settings via your scripting plugin's `set_pipeline_settings`.
75+
76+
The script loading/unloading order will look as follows:
77+
- the order in which components are attached/detached, will determing what order scripts will be processed
78+
- scripts are processed one-by-one, i.e. each machine is ticked to completion before the next one is started
79+
- meaning for example if two scripts are loaded, their `on_script_loaded` hooks will not run at the same "lockstep".
80+
- loading/unloading might happen over multiple frames, depending on the pipeline's settings.

0 commit comments

Comments
 (0)