Skip to content

Commit bdffa8e

Browse files
committed
add information on context sharing
1 parent ecf613d commit bdffa8e

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

crates/bevy_mod_scripting_core/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ pub trait ConfigureScriptPlugin {
143143
initializer: ContextPreHandlingInitializer<Self::P>,
144144
) -> Self;
145145
fn add_runtime_initializer(self, initializer: RuntimeInitializer<Self::P>) -> Self;
146+
147+
/// Switch the context assigning strategy to a global context assigner.
148+
///
149+
/// This means that all scripts will share the same context. This is useful for when you want to share data between scripts easilly.
150+
/// Be careful however as this also means that scripts can interfere with each other in unexpected ways!.
151+
fn enable_context_sharing(self);
146152
}
147153

148154
impl<P: IntoScriptPluginParams + AsMut<ScriptingPlugin<P>>> ConfigureScriptPlugin for P {
@@ -166,6 +172,10 @@ impl<P: IntoScriptPluginParams + AsMut<ScriptingPlugin<P>>> ConfigureScriptPlugi
166172
self.as_mut().add_runtime_initializer(initializer);
167173
self
168174
}
175+
176+
fn enable_context_sharing(mut self) {
177+
self.as_mut().context_assigner = ContextAssigner::new_global_context_assigner();
178+
}
169179
}
170180

171181
// One of registration of things that need to be done only once per app

docs/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- [Running Scripts](./Summary/running-scripts.md)
99
- [Controlling Script Bindings](./Summary/controlling-script-bindings.md)
1010
- [Modifying Script Contexts](./Summary/customizing-script-contexts.md)
11+
- [Shared Contexts](./Summary/sharing-contexts-between-scripts.md)
1112

1213
# Scripting Reference
1314

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Shared Contexts
2+
3+
By default BMS will create an individual script context, or sandbox, for each script that is run. This means that each script will have its own set of global variables and functions that are isolated from other scripts. However, sometimes this might not be desirable, if you aren't worried about scripts interfering with each other, or if you want to easilly share data between scripts. In these cases, you can use shared contexts.
4+
5+
## Enabling Shared Contexts
6+
7+
You can enable shared contexts by configuring the relevant scripting plugin like so:
8+
```rust,ignore
9+
let mut plugin = LuaScriptingPlugin::default().enable_context_sharing();
10+
11+
app.add_plugins(plugin);
12+
```
13+
14+
## Context Loading Settings
15+
16+
All context loading settings are stored in a separate resource per scripting plugin namely: `ContextLoadingSettings<Plugin>`.
17+
18+
The settings are as follows:
19+
- `loader` - the load and unload strategy for contexts. Each scripting plugin will have a load and unload function which is hooked up through here
20+
- `assigner` - the strategy for assigning/unassigning contexts to scripts. This is used to determine how to assign a context to a script when it is run, and what to do with the context when the script is finished.
21+
- `context_initializers` - stores all context initializers for the plugin
22+
- `context_pre_handling_initializers` - stores all context pre-handling initializers for the plugin
23+
24+
More advanced applications might want to customize these settings to suit their needs.

0 commit comments

Comments
 (0)