Ruby support #1

Open
jaroslaw wants to merge 165 commits from ruby into main
11 changed files with 93 additions and 100 deletions
Showing only changes of commit 35b4317040 - Show all commits

View file

@ -1,27 +1,27 @@
# Builtin types # Builtin types
bevy_scriptum provides following types that can be used in Lua: bevy_scriptum provides following types that can be used in Ruby:
- ```Vec3``` - ```Bevy::Vec3```
- ```BevyEntity``` - ```Bevy::Entity```
## Vec3 ## Vec3
### Constructor ### Class Methods
`Vec3(x: number, y: number, z: number)` - `new(x, y, z)`
- `current`
### Properties ### Instance Methods
- `x: number` - `x`
- `y: number` - `y`
- `z: number` - `z`
### Example Ruby usage
### Example Lua usage ```ruby
my_vec = Bevy::Vec3.new(1, 2, 3)
```lua
my_vec = Vec3(1, 2, 3)
set_translation(entity, my_vec) set_translation(entity, my_vec)
``` ```
@ -33,12 +33,12 @@ set_translation(entity, my_vec)
use bevy::prelude::*; use bevy::prelude::*;
use bevy_scriptum::prelude::*; use bevy_scriptum::prelude::*;
use bevy_scriptum::runtimes::lua::prelude::*; use bevy_scriptum::runtimes::ruby::prelude::*;
fn main() { fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_scripting::<LuaRuntime>(|runtime| { .add_scripting::<RubyRuntime>(|runtime| {
runtime.add_function(String::from("set_translation"), set_translation); runtime.add_function(String::from("set_translation"), set_translation);
}) })
.run(); .run();
@ -63,11 +63,11 @@ None - instances can only be acquired by using built-in `entity` global variable
- `index: integer` - `index: integer`
### Example Lua usage ### Example Ruby usage
```lua ```ruby
print(entity.index) puts(Bevy::Entity.current.index)
pass_to_rust(entity) pass_to_rust(Bevy::Entity.current)
``` ```
### Example Rust usage ### Example Rust usage
@ -78,12 +78,12 @@ pass_to_rust(entity)
use bevy::prelude::*; use bevy::prelude::*;
use bevy_scriptum::prelude::*; use bevy_scriptum::prelude::*;
use bevy_scriptum::runtimes::lua::prelude::*; use bevy_scriptum::runtimes::ruby::prelude::*;
fn main() { fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_scripting::<LuaRuntime>(|runtime| { .add_scripting::<RubyRuntime>(|runtime| {
runtime.add_function(String::from("pass_to_rust"), |In((entity,)): In<(BevyEntity,)>| { runtime.add_function(String::from("pass_to_rust"), |In((entity,)): In<(BevyEntity,)>| {
println!("pass_to_rust called with entity: {:?}", entity); println!("pass_to_rust called with entity: {:?}", entity);
}); });

View file

@ -2,12 +2,12 @@
## entity ## entity
A variable called `entity` is automatically available to all scripts - it represents bevy entity that the `Script` component is attached to. Current entity that the script is atteched to can be retrieved by calling `Bevy::Entity.current`.
It exposes `index` property that returns bevy entity index. It exposes `index` method that returns bevy entity index.
It is useful for accessing entity's components from scripts. It is useful for accessing entity's components from scripts.
It can be used in the following way: It can be used in the following way:
```lua ```ruby
print("Current entity index: " .. entity.index) puts("Current entity index: #{Bevy::Entity.current.index}")
``` ```
`entity` variable is currently not available within promise callbacks. `Bevy::Entity.current` variable is currently not available within promise callbacks.

View file

@ -1,6 +1,6 @@
# Calling Rust from Lua # Calling Rust from Ruby
To call a rust function from Lua first you need to register a function To call a rust function from Ruby first you need to register a function
within Rust using builder pattern. within Rust using builder pattern.
```rust,no_run ```rust,no_run
@ -9,12 +9,12 @@ within Rust using builder pattern.
use bevy::prelude::*; use bevy::prelude::*;
use bevy_scriptum::prelude::*; use bevy_scriptum::prelude::*;
use bevy_scriptum::runtimes::lua::prelude::*; use bevy_scriptum::runtimes::ruby::prelude::*;
fn main() { fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_scripting::<LuaRuntime>(|runtime| { .add_scripting::<RubyRuntime>(|runtime| {
// `runtime` is a builder that you can use to register functions // `runtime` is a builder that you can use to register functions
}) })
.run(); .run();
@ -29,12 +29,12 @@ For example to register a function called `my_rust_func` you can do the followin
use bevy::prelude::*; use bevy::prelude::*;
use bevy_scriptum::prelude::*; use bevy_scriptum::prelude::*;
use bevy_scriptum::runtimes::lua::prelude::*; use bevy_scriptum::runtimes::ruby::prelude::*;
fn main() { fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_scripting::<LuaRuntime>(|runtime| { .add_scripting::<RubyRuntime>(|runtime| {
runtime.add_function(String::from("my_rust_func"), || { runtime.add_function(String::from("my_rust_func"), || {
println!("my_rust_func has been called"); println!("my_rust_func has been called");
}); });
@ -43,15 +43,12 @@ fn main() {
} }
``` ```
After you do that the function will be available to Lua code in your spawned scripts. After you do that the function will be available to Ruby code in your spawned scripts.
```lua ```ruby
my_rust_func() my_rust_func
``` ```
Registered functions can also take parameters. A parameter can be any type
that implements `FromLua`.
Since a registered callback function is a Bevy system, the parameters are passed Since a registered callback function is a Bevy system, the parameters are passed
to it as `In` struct with tuple, which has to be the first parameter of the closure. to it as `In` struct with tuple, which has to be the first parameter of the closure.
@ -61,12 +58,12 @@ to it as `In` struct with tuple, which has to be the first parameter of the clos
use bevy::prelude::*; use bevy::prelude::*;
use bevy_scriptum::prelude::*; use bevy_scriptum::prelude::*;
use bevy_scriptum::runtimes::lua::prelude::*; use bevy_scriptum::runtimes::ruby::prelude::*;
fn main() { fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_scripting::<LuaRuntime>(|runtime| { .add_scripting::<RubyRuntime>(|runtime| {
runtime.add_function(String::from("func_with_params"), |args: In<(String, i64)>| { runtime.add_function(String::from("func_with_params"), |args: In<(String, i64)>| {
println!("my_rust_func has been called with string {} and i64 {}", args.0.0, args.0.1); println!("my_rust_func has been called with string {} and i64 {}", args.0.0, args.0.1);
}); });
@ -83,12 +80,12 @@ To make it look nicer you can destructure the `In` struct.
use bevy::prelude::*; use bevy::prelude::*;
use bevy_scriptum::prelude::*; use bevy_scriptum::prelude::*;
use bevy_scriptum::runtimes::lua::prelude::*; use bevy_scriptum::runtimes::ruby::prelude::*;
fn main() { fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_scripting::<LuaRuntime>(|runtime| { .add_scripting::<RubyRuntime>(|runtime| {
runtime.add_function(String::from("func_with_params"), |In((a, b)): In<(String, i64)>| { runtime.add_function(String::from("func_with_params"), |In((a, b)): In<(String, i64)>| {
println!("my_rust_func has been called with string {} and i64 {}", a, b); println!("my_rust_func has been called with string {} and i64 {}", a, b);
}); });
@ -97,9 +94,9 @@ fn main() {
} }
``` ```
The above function can be called from Lua The above function can be called from Ruby
```lua ```ruby
func_with_params("abc", 123) func_with_params("abc", 123)
``` ```
@ -115,12 +112,12 @@ a callback that will receive the value returned from Rust function.
use bevy::prelude::*; use bevy::prelude::*;
use bevy_scriptum::prelude::*; use bevy_scriptum::prelude::*;
use bevy_scriptum::runtimes::lua::prelude::*; use bevy_scriptum::runtimes::ruby::prelude::*;
fn main() { fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_scripting::<LuaRuntime>(|runtime| { .add_scripting::<RubyRuntime>(|runtime| {
runtime.add_function(String::from("returns_value"), || { runtime.add_function(String::from("returns_value"), || {
123 123
}); });
@ -129,8 +126,8 @@ fn main() {
} }
``` ```
```lua ```ruby
returns_value():and_then(function (value) returns_value.and_then do |value|
print(value) -- 123 puts(value) # 123
end) end
``` ```

View file

@ -1,15 +1,15 @@
# Calling Lua from Rust # Calling Ruby from Rust
To call a function defined in Lua To call a function defined in Ruby
```lua ```ruby
function on_update() def on_update
end end
``` ```
We need to acquire `LuaRuntime` resource within a bevy system. We need to acquire `RubyRuntime` resource within a bevy system.
Then we will be able to call `call_fn` on it, providing the name Then we will be able to call `call_fn` on it, providing the name
of the function to call, `LuaScriptData` that has been automatically of the function to call, `RubyScriptData` that has been automatically
attached to entity after an entity with script attached has been spawned attached to entity after an entity with script attached has been spawned
and its script evaluated, the entity and optionally some arguments. and its script evaluated, the entity and optionally some arguments.
@ -19,14 +19,14 @@ and its script evaluated, the entity and optionally some arguments.
use bevy::prelude::*; use bevy::prelude::*;
use bevy_scriptum::prelude::*; use bevy_scriptum::prelude::*;
use bevy_scriptum::runtimes::lua::prelude::*; use bevy_scriptum::runtimes::ruby::prelude::*;
fn call_lua_on_update_from_rust( fn call_ruby_on_update_from_rust(
mut scripted_entities: Query<(Entity, &mut LuaScriptData)>, mut scripted_entities: Query<(Entity, &mut RubyScriptData)>,
scripting_runtime: ResMut<LuaRuntime>, scripting_runtime: ResMut<RubyRuntime>,
) { ) {
for (entity, mut script_data) in &mut scripted_entities { for (entity, mut script_data) in &mut scripted_entities {
// calling function named `on_update` defined in lua script // calling function named `on_update` defined in Ruby script
scripting_runtime scripting_runtime
.call_fn("on_update", &mut script_data, entity, ()) .call_fn("on_update", &mut script_data, entity, ())
.unwrap(); .unwrap();
@ -43,11 +43,11 @@ We can also pass some arguments by providing a tuple or `Vec` as the last
use bevy::prelude::*; use bevy::prelude::*;
use bevy_scriptum::prelude::*; use bevy_scriptum::prelude::*;
use bevy_scriptum::runtimes::lua::prelude::*; use bevy_scriptum::runtimes::ruby::prelude::*;
fn call_lua_on_update_from_rust( fn call_ruby_on_update_from_rust(
mut scripted_entities: Query<(Entity, &mut LuaScriptData)>, mut scripted_entities: Query<(Entity, &mut RubyScriptData)>,
scripting_runtime: ResMut<LuaRuntime>, scripting_runtime: ResMut<RubyRuntime>,
) { ) {
for (entity, mut script_data) in &mut scripted_entities { for (entity, mut script_data) in &mut scripted_entities {
scripting_runtime scripting_runtime
@ -57,13 +57,10 @@ fn call_lua_on_update_from_rust(
} }
``` ```
They will be passed to `on_update` Lua function They will be passed to `on_update` Ruby function
```lua ```ruby
function on_update(a, b) def on_update(a, b)
print(a) -- 123 puts(a) # 123
print(b) -- hello puts(b) # hello
end end
``` ```
Any type that implements `IntoLua` can be passed as an argument withing the
tuple in `call_fn`.

View file

@ -18,12 +18,12 @@ You can now start exposing functions to the scripting language. For example, you
use bevy::prelude::*; use bevy::prelude::*;
use bevy_scriptum::prelude::*; use bevy_scriptum::prelude::*;
use bevy_scriptum::runtimes::lua::prelude::*; use bevy_scriptum::runtimes::ruby::prelude::*;
fn main() { fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_scripting::<LuaRuntime>(|runtime| { .add_scripting::<RubyRuntime>(|runtime| {
runtime.add_function( runtime.add_function(
String::from("my_print"), String::from("my_print"),
|In((x,)): In<(String,)>| { |In((x,)): In<(String,)>| {
@ -35,9 +35,9 @@ fn main() {
} }
``` ```
Then you can create a script file in `assets` directory called `script.lua` that calls this function: Then you can create a script file in `assets` directory called `script.rb` that calls this function:
```lua ```ruby
my_print("Hello world!") my_print("Hello world!")
``` ```
@ -49,12 +49,12 @@ And spawn an entity with attached `Script` component with a handle to a script s
use bevy::prelude::*; use bevy::prelude::*;
use bevy_scriptum::prelude::*; use bevy_scriptum::prelude::*;
use bevy_scriptum::runtimes::lua::prelude::*; use bevy_scriptum::runtimes::ruby::prelude::*;
fn main() { fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_scripting::<LuaRuntime>(|runtime| { .add_scripting::<RubyRuntime>(|runtime| {
runtime.add_function( runtime.add_function(
String::from("my_print"), String::from("my_print"),
|In((x,)): In<(String,)>| { |In((x,)): In<(String,)>| {
@ -63,7 +63,7 @@ fn main() {
); );
}) })
.add_systems(Startup,|mut commands: Commands, asset_server: Res<AssetServer>| { .add_systems(Startup,|mut commands: Commands, asset_server: Res<AssetServer>| {
commands.spawn(Script::<LuaScript>::new(asset_server.load("script.lua"))); commands.spawn(Script::<RubyScript>::new(asset_server.load("script.rb")));
}) })
.run(); .run();
} }

View file

@ -5,7 +5,7 @@ Add the following to your `Cargo.toml`:
```toml ```toml
[dependencies] [dependencies]
bevy = "0.16" bevy = "0.16"
bevy_scriptum = { version = "0.8", features = ["lua"] } bevy_scriptum = { version = "0.8", features = ["ruby"] }
``` ```
If you need a different version of bevy you need to use a matching bevy_scriptum If you need a different version of bevy you need to use a matching bevy_scriptum

View file

@ -14,7 +14,7 @@ of all entities with `Player` component.
use bevy::prelude::*; use bevy::prelude::*;
use bevy_scriptum::prelude::*; use bevy_scriptum::prelude::*;
use bevy_scriptum::runtimes::lua::prelude::*; use bevy_scriptum::runtimes::ruby::prelude::*;
#[derive(Component)] #[derive(Component)]
struct Player; struct Player;
@ -22,7 +22,7 @@ struct Player;
fn main() { fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_scripting::<LuaRuntime>(|runtime| { .add_scripting::<RubyRuntime>(|runtime| {
runtime.add_function( runtime.add_function(
String::from("print_player_names"), String::from("print_player_names"),
|players: Query<&Name, With<Player>>| { |players: Query<&Name, With<Player>>| {
@ -38,8 +38,8 @@ fn main() {
In script: In script:
```lua ```ruby
print_player_names() print_player_names
``` ```
You can use functions that interact with Bevy entities and resources and You can use functions that interact with Bevy entities and resources and
@ -53,7 +53,7 @@ component.
use bevy::prelude::*; use bevy::prelude::*;
use bevy_scriptum::prelude::*; use bevy_scriptum::prelude::*;
use bevy_scriptum::runtimes::lua::prelude::*; use bevy_scriptum::runtimes::ruby::prelude::*;
#[derive(Component)] #[derive(Component)]
struct Player { struct Player {
@ -63,7 +63,7 @@ struct Player {
fn main() { fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_scripting::<LuaRuntime>(|runtime| { .add_scripting::<RubyRuntime>(|runtime| {
runtime.add_function( runtime.add_function(
String::from("hurt_player"), String::from("hurt_player"),
|In((hit_value,)): In<(i32,)>, mut players: Query<&mut Player>| { |In((hit_value,)): In<(i32,)>, mut players: Query<&mut Player>| {
@ -78,6 +78,6 @@ fn main() {
And it could be called in script like: And it could be called in script like:
```lua ```ruby
hurt_player(5) hurt_player(5)
``` ```

View file

@ -1,3 +0,0 @@
# Lua
This chapter demonstrates how to work with bevy_scriptum when using Lua language runtime.

View file

@ -1 +1,3 @@
# Ruby # Ruby
This chapter demonstrates how to work with bevy_scriptum when using Ruby language runtime.

View file

@ -9,18 +9,18 @@ bevy's `AssetServer`.
use bevy::prelude::*; use bevy::prelude::*;
use bevy_scriptum::prelude::*; use bevy_scriptum::prelude::*;
use bevy_scriptum::runtimes::lua::prelude::*; use bevy_scriptum::runtimes::ruby::prelude::*;
fn my_spawner(mut commands: Commands, assets_server: Res<AssetServer>) { fn my_spawner(mut commands: Commands, assets_server: Res<AssetServer>) {
commands.spawn(Script::<LuaScript>::new( commands.spawn(Script::<RubyScript>::new(
assets_server.load("my_script.lua"), assets_server.load("my_script.rb"),
)); ));
} }
``` ```
After they scripts have been evaled by bevy_scriptum, the entities that they've After they scripts have been evaled by bevy_scriptum, the entities that they've
been attached to will get the `Script::<LuaScript>` component stripped and instead been attached to will get the `Script::<RubyScript>` component stripped and instead
```LuaScriptData``` component will be attached. ```RubyScriptData``` component will be attached.
So to query scipted entities you could do something like: So to query scipted entities you could do something like:
@ -30,10 +30,10 @@ So to query scipted entities you could do something like:
use bevy::prelude::*; use bevy::prelude::*;
use bevy_scriptum::prelude::*; use bevy_scriptum::prelude::*;
use bevy_scriptum::runtimes::lua::prelude::*; use bevy_scriptum::runtimes::ruby::prelude::*;
fn my_system( fn my_system(
mut scripted_entities: Query<(Entity, &mut LuaScriptData)>, mut scripted_entities: Query<(Entity, &mut RubyScriptData)>,
) { ) {
for (entity, mut script_data) in &mut scripted_entities { for (entity, mut script_data) in &mut scripted_entities {
// do something with scripted entities // do something with scripted entities

View file

@ -174,7 +174,7 @@ fn then(r_self: magnus::Value) -> magnus::Value {
.into_value() .into_value()
} }
#[derive(Clone)] #[derive(Clone, Debug)]
#[magnus::wrap(class = "Bevy::Entity")] #[magnus::wrap(class = "Bevy::Entity")]
pub struct BevyEntity(pub Entity); pub struct BevyEntity(pub Entity);
@ -191,7 +191,7 @@ impl TryConvert for BevyEntity {
} }
} }
#[derive(Clone)] #[derive(Clone, Debug)]
#[magnus::wrap(class = "Bevy::Vec3")] #[magnus::wrap(class = "Bevy::Vec3")]
pub struct BevyVec3(pub Vec3); pub struct BevyVec3(pub Vec3);