-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add more information to docs * add information on world containers * add edit url to mdbook
- Loading branch information
Showing
6 changed files
with
155 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
docs/src/Development/AddingLanguages/necessary-features.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Necessary Features | ||
|
||
In order for a language to be called "implemented" in BMS, it needs to support the following features: | ||
|
||
- Every script function which is registered on a type's namespace must: | ||
- Be callable on a `ReflectReference` representing object of that type in the script | ||
```lua | ||
local my_reference = ... | ||
my_reference:my_Registered_function() | ||
``` | ||
- If it's static it must be callable from a global proxy object for that type, i.e. | ||
```lua | ||
MyType.my_static_function() | ||
``` | ||
- `ReflectReferences` must support a set of basic features: | ||
- Access to fields via reflection i.e.: | ||
```lua | ||
local my_reference = ... | ||
my_reference.my_field = 5 | ||
print(my_reference.my_field) | ||
``` | ||
- Basic operators and standard operations are overloaded with the appropriate standard dynamic function registered: | ||
- Addition: dispatches to the `add` binary function on the type | ||
- Multiplication: dispatches to the `mul` binary function on the type | ||
- Division: dispatches to the `div` binary function on the type | ||
- Subtraction: dispatches to the `sub` binary function on the type | ||
- Modulo: dispatches to the `rem` binary function on the type | ||
- Negation: dispatches to the `neg` unary function on the type | ||
- Exponentiation: dispatches to the `pow` binary function on the type | ||
- Equality: dispatches to the `eq` binary function on the type | ||
- Less than: dispatches to the `lt` binary function on the type | ||
- Length: calls the `len` method on `ReflectReference` or on the table if the value is one. | ||
- Iteration: dispatches to the `iter` method on `ReflectReference` which returns an iterator function, this can be repeatedly called until it returns `ScriptValue::Unit` to signal the end of the iteration. | ||
- Print: calls the `display_ref` method on `ReflectReference` or on the table if the value is one. | ||
- Script handlers, loaders etc. must be implemented such that the `ThreadWorldContainer` is set for every interaction with script contexts, or anywhere else it might be needed. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Modifying Script Contexts | ||
|
||
You should be able to achieve what you need by registering script functions in most cases. However sometimes you might want to override the way contexts are loaded, or how the runtime is initialized. | ||
|
||
This is possible using `Context Initializers` and `Context Pre Handling Initializers` as well as `Runtime Initializers`. | ||
|
||
|
||
## Context Initializers | ||
|
||
For example, let's say you want to set a dynamic amount of globals in your script, depending on some setting in your app. | ||
|
||
You could do this by customizing the scripting plugin: | ||
```rust,ignore | ||
let plugin = LuaScriptingPlugin::default(); | ||
plugin.add_context_initializer(|script_id: &str, context: &mut Lua| { | ||
let globals = context.globals(); | ||
for i in 0..10 { | ||
globals.set(i, i); | ||
} | ||
Ok(()) | ||
}); | ||
app.add_plugins(plugin) | ||
``` | ||
|
||
The above will run every time the script is loaded or re-loaded. | ||
|
||
## Context Pre Handling Initializers | ||
|
||
If you want to customize your context before every time it's about to handle events, you can use `Context Pre Handling Initializers`: | ||
```rust,ignore | ||
let plugin = LuaScriptingPlugin::default(); | ||
plugin.add_context_pre_handling_initializer(|script_id: &str, entity: Entity, context: &mut Lua| { | ||
let globals = context.globals(); | ||
globals.set("script_name", script_id.to_owned()); | ||
Ok(()) | ||
}); | ||
``` | ||
## Runtime Initializers | ||
|
||
Some scripting languages, have the concept of a `runtime`. This is a global object which is shared between all contexts. You can customize this object using `Runtime Initializers`: | ||
```rust,ignore | ||
let plugin = SomeScriptingPlugin::default(); | ||
plugin.add_runtime_initializer(|runtime: &mut Runtime| { | ||
runtime.set_max_stack_size(1000); | ||
Ok(()) | ||
}); | ||
``` | ||
|
||
## Accessing the World in Initializers | ||
|
||
You can access the world in these initializers by using the thread local: `ThreadWorldContainer`: | ||
```rust,ignore | ||
let plugin = LuaScriptingPlugin::default(); | ||
plugin.add_context_initializer(|script_id: &str, context: &mut Lua| { | ||
let world = ThreadWorldContainer::get_world(); | ||
world.with_resource::<MyResource>(|res| println!("My resource: {:?}", res)); | ||
Ok(()) | ||
}); | ||
``` |