bevy_scriptum is a a plugin for Bevy that allows you to write some of your game logic in a scripting language

Overview

bevy_scriptum 📜

⚠️ Pre-release, alpha version: API is bound to change, bugs are to be expected.

bevy_scriptum is a a plugin for Bevy that allows you to write some of your game logic in a scripting language. Currently, only Rhai is supported, but more languages may be added in the future.

It's main advantages include:

  • low-boilerplate
  • easy to use
  • asynchronicity with a promise-based API
  • flexibility
  • hot-reloading

Scripts are separate files that can be hot-reloaded at runtime. This allows you to quickly iterate on your game logic without having to recompile your game.

All you need to do is register callbacks on your Bevy app like this:

use bevy::prelude::*;
use bevy_scriptum::prelude::*;

App::new()
    .add_plugins(DefaultPlugins)
    .add_plugin(ScriptingPlugin::default())
    .add_script_function(String::from("hello_bevy"), || {
      println!("hello bevy, called from script");
    });

And you can call them in your scripts like this:

hello_bevy();

Every callback function that you expose to the scripting language is also a Bevy system, so you can easily query and mutate ECS components and resources just like you would in a regular Bevy system:

>| { for player in &players { println!("player name: {}", player); } }, );">
use bevy::prelude::*;
use bevy_scriptum::prelude::*;

#[derive(Component)]
struct Player;

App::new()
    .add_plugins(DefaultPlugins)
    .add_plugin(ScriptingPlugin::default())
    .add_script_function(
        String::from("print_player_names"),
        |players: Query<&Name, With<Player>>| {
            for player in &players {
                println!("player name: {}", player);
            }
        },
    );

You can also pass arguments to your callback functions, just like you would in a regular Bevy system - using In structs with tuples:

| { println!("called with string: '{}'", x); }, );">
use bevy::prelude::*;
use bevy_scriptum::prelude::*;
use rhai::ImmutableString;

App::new()
    .add_plugins(DefaultPlugins)
    .add_plugin(ScriptingPlugin::default())
    .add_script_function(
        String::from("fun_with_string_param"),
        |In((x,)): In<(ImmutableString,)>| {
            println!("called with string: '{}'", x);
        },
    );

which you can then call in your script like this:

fun_with_string_param("Hello world!");

Usage

Add the following to your Cargo.toml:

[dependencies]
bevy_scriptum = "0.1"

or execute cargo add bevy_scriptum from your project directory.

Add the following to your main.rs:

use bevy::prelude::*;
use bevy_scriptum::prelude::*;

App::new()
    .add_plugins(DefaultPlugins)
    .add_plugin(ScriptingPlugin::default())
    .run();

You can now start exposing functions to the scripting language. For example, you can expose a function that prints a message to the console:

| { println!("my_print: '{}'", x); }, );">
use rhai::ImmutableString;
use bevy::prelude::*;
use bevy_scriptum::prelude::*;

App::new()
    .add_plugins(DefaultPlugins)
    .add_plugin(ScriptingPlugin::default())
    .add_script_function(
        String::from("my_print"),
        |In((x,)): In<(ImmutableString,)>| {
            println!("my_print: '{}'", x);
        },
    );

Then you can create a script file in assets directory called script.rhai that calls this function:

my_print("Hello world!");

And spawn a Script component with a handle to a script source file`:

use bevy::prelude::*;
use bevy_scriptum::Script;

App::new()
    .add_startup_system(|mut commands: Commands, asset_server: Res<AssetServer>| {
        commands.spawn(Script::new(asset_server.load("script.rhai")));
    });

Provided examples

You can also try running provided examples by cloning this repository and running cargo run --example . For example:

cargo run --example hello_world

The examples live in examples directory and their corresponding scripts live in assets/examples directory within the repository.

Bevy compatibility

bevy version bevy_scriptum version
0.10 0.1

Promises - getting return values from scripts

Every function called from script returns a promise that you can call .then with a callback function on. This callback function will be called when the promise is resolved, and will be passed the return value of the function called from script. For example:

get_player_name().then(|name| {
    print(name);
});

Access entity from script

A variable called entity is automatically available to all scripts - it represents bevy entity that the Script component is attached to. It exposes .index() method that returns bevy entity index. It is useful for accessing entity's components from scripts. It can be used in the following way:

print("Current entity index: " + entity.index());

Contributing

Contributions are welcome! Feel free to open an issue or submit a pull request.

License

bevy_scriptum is licensed under either of the following, at your option: Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) or MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)

You might also like...
The GameLisp scripting language

GameLisp GameLisp is a scripting language for Rust game development. To get started, take a look at the homepage. Please note that GameLisp currently

Open-source compiler for the Papyrus scripting language of Bethesda games.

Open Papyrus Compiler This project is still WORK IN PROGRESS. If you have any feature requests, head over to the Issues tab and describe your needs. Y

A safe, fast, lightweight embeddable scripting language written in Rust.

Bud (budlang) A safe, fast, lightweight embeddable scripting language written in Rust. WARNING: This crate is not anywhere near being ready to publish

Provides filesystem access for the Rhai scripting language.

About rhai-fs This crate provides filesystem access for the Rhai scripting language. Usage Cargo.toml [dependencies] rhai-fs = "0.1.2" Rhai script //

YAL is Yet Another scripting Language(but worse)

YAL - Yet Another Language YAL is yet another scripting language(but worse). Syntax Basic syntax fun main() { print("Hello, World!"); } Fibonacci

Shell scripting that will knock your socks off
Shell scripting that will knock your socks off

atom Shell scripting that will knock your socks off. NOTE: Click the image above for a video demonstration.

That program use on platform windows. And if you write any text on uncorrect keyboard layout, that program for that.
That program use on platform windows. And if you write any text on uncorrect keyboard layout, that program for that.

📌 This program is designed to translate text into the correct layout when typing is incorrect. 📌 Example ghbdtn - привет Just (by default) pressing

Rust crate that allows you to display status & progress information in a terminal

status-line This crate allows you to display status & progress information in a terminal This crate handles the problem of displaying a small amount o

Jumpy is a tool that allows to quickly jump to one of the directory you've visited in the past

Jumpy Jumpy is a tool that allows to quickly jump to one of the directory you've visited in the past. It is heavily inspired by Zoxide but is more lig

Releases(v0.1.0)
Owner
Jarosław Konik
Software developer and runner
Jarosław Konik
Sero is a web server that allows you to easily host your static sites without pain. The idea was inspired by surge.sh but gives you full control.

sero Lightning-fast, static web publishing with zero configuration and full control ?? Table Of Contents ?? Table Of Contents ?? Tools ❓ About The Pro

Dmitry Miasnenko 6 Nov 13, 2023
Simple low-level web server to serve file uploads with some shell scripting-friendly features

http_file_uploader Simple low-level web server to serve file uploads with some shell scripting-friendly features. A bridge between Web's multipart/for

Vitaly Shukela 2 Oct 27, 2022
FastSSH is a TUI that allows you to quickly connect to your services by navigating through your SSH config.

Connect quickly to your services ?? FastSSH is a TUI that allows you to quickly connect to your services by navigating through your SSH config. Instal

Julien 85 Dec 14, 2022
A filesystem driver that allows you to view your Blackboard course contents as if they were normal files and folders on your system!

BlackboardFS Blackboard: noun A website so bad that it might as well be a network drive. BlackboardFS is a filesystem driver that allows you to view y

null 22 Sep 4, 2023
Compiler for an "extended" version of the Mindustry logic language

Minblur Compiler Minblur is a compiler for a superset of "logic" programming language in the game Mindustry. It helps reduce code-duplication, making

Binder News 15 May 2, 2022
Cargo-eval - A cargo plugin to quickly evaluate some Rust source code.

cargo eval A cargo plugin to quickly evaluate some Rust source code. Installation $ cargo install --git https://github.com/timClicks/cargo-eval.git Us

Tim McNamara 9 Dec 21, 2022
Nexa programming language. A language for game developers by a game developer

NexaLang Nexa programming language. A language for game developers by a game developer. Features High-Level: Nexa is an easy high level language Two M

Sabe 3 Aug 21, 2023
The module graph logic for Deno CLI

deno_graph The module graph/dependency logic for the Deno CLI. This repository is a Rust crate which provides the foundational code to be able to buil

Deno Land 67 Dec 14, 2022
Extract core logic from qdrant and make it available as a library.

Qdrant lib Why? Qdrant is a vector search engine known for its speed, scalability, and user-friendliness. While it excels in its domain, it currently

Tyr Chen 27 Jan 1, 2024
A simple, human-friendly, embeddable scripting language

Mica Language reference · Rust API A simple, human-friendly scripting language, developed one feature at a time. Human-friendly syntax inspired by Rub

Mica programming language 32 Dec 30, 2022