Crates.io | bevy_mod_scripting |
lib.rs | bevy_mod_scripting |
version | 0.8.0-alpha.1 |
source | src |
created_at | 2022-08-02 19:39:28.035812 |
updated_at | 2024-11-10 13:47:50.149949 |
description | Multi language scripting in Bevy |
homepage | https://github.com/makspll/bevy_mod_scripting |
repository | https://github.com/makspll/bevy_mod_scripting |
max_upload_size | |
id | 637534 |
size | 305,454 |
Although Bevy doesn't directly support scripting, efforts are underway to incorporate it. This crate represents an initial attempt to enable scripting within Bevy's existing framework. It's important to note that this is a work in progress and not yet optimized or complete. As Bevy evolves, significant changes to this API are anticipated.
For a detailed look at how this crate works see architecture.md
on_update
)require
(enabled with unsafe_lua_modules
cargo feature due to potential unsafety)Support for languages is expressed in three levels:
ScriptHost
implementation, scripts can be loaded, scheduled and run in this language with support for custom APIProvider
entity
,world
etc and provides support for at least basic operations such as get_component
, add_component
, spawn
etcThe languages currently supported are as follows:
Language | Support Level | Documentation Generation |
---|---|---|
Lua | 4 | Yes |
Rhai | 2 | No |
Rune | 1 | No |
To install:
add_script_host
, add_script_host_to_set
)
add_script_handler_to_set
,add_script_handler
)An example can be seen below
fn main() -> std::io::Result<()> {
let mut app = App::new();
app.add_plugins(ScriptingPlugin)
.add_plugins(DefaultPlugins)
// pick and register only the hosts you want to use
// use any system set AFTER any systems which add/remove/modify script components
// in order for your script updates to propagate in a single frame
.add_script_host::<RhaiScriptHost<MyRhaiArgStruct>>(PostUpdate)
.add_script_host::<LuaScriptHost<MyLuaArgStruct>>(PostUpdate)
// the handlers should be ran after any systems which produce script events.
// The PostUpdate set is okay only if your API doesn't require the core Bevy systems' commands
// to run beforehand.
// Note, this setup assumes a single script handler system set with all events having identical
// priority of zero (see examples for more complex scenarios)
.add_script_handler::<LuaScriptHost<MyLuaArg>, 0, 0>(
CoreSet::PostUpdate,
)
.add_script_handler::<RhaiScriptHost<RhaiEventArgs>, 0, 0>(
CoreSet::PostUpdate,
)
// generate events for scripts to pickup
.add_system(trigger_on_update_lua)
.add_system(trigger_on_update_rhai)
// attach script components to entities
.add_startup_system(load_a_script);
app.run();
Ok(())
}
Scripts are activated by dispatching ScriptEvents
. This crate employs custom priority event writers and readers, which means events are transmitted with an associated priority. This priority, in conjunction with your event pipeline, influences the sequence in which your events are processed. A priority of 0 is considered the highest.
This mechanism can be utilized to construct game loops similar to those found in Unity or other game engines.
An example event dispatching system can be seen below:
use bevy::prelude::*;
use bevy_mod_scripting::prelude::*;
// event callback generator for lua
#[cfg(feature = "lua")]
pub fn trigger_on_update_lua(mut w: PriorityEventWriter<LuaEvent<()>>) {
let event = LuaEvent::<()> {
hook_name: "on_update".to_string(),
args: (),
recipients: Recipients::All
};
w.send(event,0);
}
A script is composed of:
Scripts are associated with entities through bevy_mod_scripting::ScriptCollection
components, as illustrated below:
use std::sync::Mutex;
use bevy::prelude::*;
use bevy_mod_scripting::prelude::*;
// An example of a startup system which loads the lua script "console_integration.lua"
// placed in "assets/scripts/" and attaches it to a new entity
#[cfg(feature = "lua")]
pub fn load_a_script(
server: Res<AssetServer>,
mut commands: Commands,
) {
// this handle is kept by the script so it will not be unloaded
let path = "scripts/console_integration.lua".to_string();
let handle = server.load::<LuaFile>(&path);
commands.spawn(()).insert(ScriptCollection::<LuaFile> {
scripts: vec![Script::<LuaFile>::new(
path, handle,
)],
});
}
To make an API accessible to your scripts, you need to implement the APIProvider
trait. This can be registered with your script host using the add_api_provider
method of App
. APIProviders
function similarly to plugins:
use ::std::sync::Mutex;
use bevy_mod_scripting::prelude::*;
#[cfg(feature = "lua")]
#[derive(Default)]
pub struct LuaAPI;
#[cfg(feature = "lua")]
impl APIProvider for LuaAPI {
type APITarget = Mutex<Lua>;
type DocTarget = LuaDocFragment;
type ScriptContext = Mutex<Lua>;
fn attach_api(&mut self, ctx: &mut Self::APITarget) -> Result<(),ScriptError> {
// ... access the lua context here when the script loads
Ok(())
}
}
Register your API providers like so:
app.add_plugins(DefaultPlugins)
.add_plugins(ScriptingPlugin)
.add_script_host::<LuaScriptHost<MyLuaArg>>(PostUpdate)
.add_api_provider::<LuaScriptHost<MyLuaArg>>(Box::new(LuaAPI))
//...
The APIProvider
interface also includes setup_script
and get_doc_fragment
methods. By default, these methods do not perform any operation. However, they can be utilized for specific purposes. For instance, get_doc_fragment
can be used to generate documentation (refer to examples), and setup_script
can ensure a one-time setup per script, like setting up a Lua package path.
Documentation features are exposed at runtime via the update_documentation
builder trait method for App
:
use bevy::prelude::*;
use bevy_mod_scripting::prelude::*;
fn main() -> std::io::Result<()> {
let mut app = App::new();
app.add_plugins(DefaultPlugins)
.add_plugins(ScriptingPlugin);
#[cfg(feature = "lua")]
{
app.add_script_host::<LuaScriptHost<()>>(PostUpdate)
// Note: This is a noop in optimized builds unless the `doc_always` feature is enabled!
// this will pickup any API providers added *BEFOREHAND* like this one
.add_api_provider::<LuaScriptHost<()>>(Box::new(LuaBevyAPIProvider))
.add_api_provider::<LuaScriptHost<()>>(Box::new(LuaCoreBevyAPIProvider))
.update_documentation::<LuaScriptHost<()>>()
.add_script_handler::<LuaScriptHost<()>, 0, 0>(PostUpdate);
}
Ok(())
}
tealr
, a wrapper around the mlua
crate, provides mechanisms for Lua documentation generation. It can generate d.tl
files for static typing in Lua via the teal
project, but using teal
isn't necessary for documentation generation.
See this example for a demonstration.
The Bevy API documentation for this crate is auto-generated with each release and can be found here and here. You may need to adjust the page_root
in the auto-generated assets/doc/tealr_doc_gen_config.json
file to a path like assets/doc/YourAPI
.
Teal is the recommended way of introducing lua to your bevy game. This functionality is locked behind the teal
cargo feature however, since it's quite opinionanted when it comes to your asset structure (script
and scripts/build
, folders under assets
), and also requires lua
+ teal
+ tealr_doc_gen
(cargo install --git https://github.com/lenscas/tealr_doc_gen --rev 91afd4a528e7f5b746ac3a6b299c422b42c05db6
) to be installed (see https://github.com/teal-language/tl and tealr
).
Once enabled, .tl
files can be loaded as lua scripts in addition to .lua
files and compiled on the fly. With full hot-reloading support. When you're ready to release your game, you just need to run tl build
from the assets/scripts
directory to compile your teal files. This will generate .lua
files under assets/scripts/build
. You can manage loading scripts using the [bevy_mod_scripting::lua_path
] macro.
If teal
is enabled and you've added the update_documentation
step to your app, every time you run/build your app in development the following will be generated/synced: - a scripts/doc
directory containing documentation for your lua exposed API - a scripts/types
directory containing .d.tl
files for your lua IDE - a scripts/tlconfig.lua
file will be generated once if it does not yet exist - any scripts with a .tl
extension will be compiled to lua code and type checked
On optimized release builds none of this happens (no debug_asserts).
The recommended workflow is to use vscode and the official teal extension with an additional tlconfig.lua
file at the root of your workspace with the
following content:
return {
include_dir = {
"path_to_your_lib/",
}
}
SCRIPT_DOC_DIR
- documentation is generated in assets/scripts/docs
or to the path in this ENV variable if it's set.To see more complex applications of this library have a look at the examples:
Below is a video showcasing the game_of_life example:
bevy_mod_scripting | bevy |
---|---|
0.7 | 0.14 |
0.6 | 0.13.1 |