This commit is contained in:
parent
f9a7a9eb77
commit
35b4317040
11 changed files with 93 additions and 100 deletions
|
|
@ -1,27 +1,27 @@
|
|||
# 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```
|
||||
- ```BevyEntity```
|
||||
- ```Bevy::Vec3```
|
||||
- ```Bevy::Entity```
|
||||
|
||||
## Vec3
|
||||
|
||||
### Constructor
|
||||
### Class Methods
|
||||
|
||||
`Vec3(x: number, y: number, z: number)`
|
||||
- `new(x, y, z)`
|
||||
- `current`
|
||||
|
||||
### Properties
|
||||
### Instance Methods
|
||||
|
||||
- `x: number`
|
||||
- `y: number`
|
||||
- `z: number`
|
||||
- `x`
|
||||
- `y`
|
||||
- `z`
|
||||
|
||||
### Example Ruby usage
|
||||
|
||||
### Example Lua usage
|
||||
|
||||
```lua
|
||||
my_vec = Vec3(1, 2, 3)
|
||||
```ruby
|
||||
my_vec = Bevy::Vec3.new(1, 2, 3)
|
||||
set_translation(entity, my_vec)
|
||||
```
|
||||
|
||||
|
|
@ -33,12 +33,12 @@ set_translation(entity, my_vec)
|
|||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
use bevy_scriptum::runtimes::ruby::prelude::*;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_scripting::<LuaRuntime>(|runtime| {
|
||||
.add_scripting::<RubyRuntime>(|runtime| {
|
||||
runtime.add_function(String::from("set_translation"), set_translation);
|
||||
})
|
||||
.run();
|
||||
|
|
@ -63,11 +63,11 @@ None - instances can only be acquired by using built-in `entity` global variable
|
|||
|
||||
- `index: integer`
|
||||
|
||||
### Example Lua usage
|
||||
### Example Ruby usage
|
||||
|
||||
```lua
|
||||
print(entity.index)
|
||||
pass_to_rust(entity)
|
||||
```ruby
|
||||
puts(Bevy::Entity.current.index)
|
||||
pass_to_rust(Bevy::Entity.current)
|
||||
```
|
||||
|
||||
### Example Rust usage
|
||||
|
|
@ -78,12 +78,12 @@ pass_to_rust(entity)
|
|||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
use bevy_scriptum::runtimes::ruby::prelude::*;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_scripting::<LuaRuntime>(|runtime| {
|
||||
.add_scripting::<RubyRuntime>(|runtime| {
|
||||
runtime.add_function(String::from("pass_to_rust"), |In((entity,)): In<(BevyEntity,)>| {
|
||||
println!("pass_to_rust called with entity: {:?}", entity);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -2,12 +2,12 @@
|
|||
|
||||
## entity
|
||||
|
||||
A variable called `entity` is automatically available to all scripts - it represents bevy entity that the `Script` component is attached to.
|
||||
It exposes `index` property that returns bevy entity index.
|
||||
Current entity that the script is atteched to can be retrieved by calling `Bevy::Entity.current`.
|
||||
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:
|
||||
```lua
|
||||
print("Current entity index: " .. entity.index)
|
||||
```ruby
|
||||
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.
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
||||
```rust,no_run
|
||||
|
|
@ -9,12 +9,12 @@ within Rust using builder pattern.
|
|||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
use bevy_scriptum::runtimes::ruby::prelude::*;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_scripting::<LuaRuntime>(|runtime| {
|
||||
.add_scripting::<RubyRuntime>(|runtime| {
|
||||
// `runtime` is a builder that you can use to register functions
|
||||
})
|
||||
.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_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
use bevy_scriptum::runtimes::ruby::prelude::*;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_scripting::<LuaRuntime>(|runtime| {
|
||||
.add_scripting::<RubyRuntime>(|runtime| {
|
||||
runtime.add_function(String::from("my_rust_func"), || {
|
||||
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
|
||||
my_rust_func()
|
||||
```ruby
|
||||
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
|
||||
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_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
use bevy_scriptum::runtimes::ruby::prelude::*;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_scripting::<LuaRuntime>(|runtime| {
|
||||
.add_scripting::<RubyRuntime>(|runtime| {
|
||||
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);
|
||||
});
|
||||
|
|
@ -83,12 +80,12 @@ To make it look nicer you can destructure the `In` struct.
|
|||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
use bevy_scriptum::runtimes::ruby::prelude::*;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.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)>| {
|
||||
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)
|
||||
```
|
||||
|
||||
|
|
@ -115,12 +112,12 @@ a callback that will receive the value returned from Rust function.
|
|||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
use bevy_scriptum::runtimes::ruby::prelude::*;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_scripting::<LuaRuntime>(|runtime| {
|
||||
.add_scripting::<RubyRuntime>(|runtime| {
|
||||
runtime.add_function(String::from("returns_value"), || {
|
||||
123
|
||||
});
|
||||
|
|
@ -129,8 +126,8 @@ fn main() {
|
|||
}
|
||||
```
|
||||
|
||||
```lua
|
||||
returns_value():and_then(function (value)
|
||||
print(value) -- 123
|
||||
end)
|
||||
```ruby
|
||||
returns_value.and_then do |value|
|
||||
puts(value) # 123
|
||||
end
|
||||
```
|
||||
|
|
|
|||
|
|
@ -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
|
||||
function on_update()
|
||||
```ruby
|
||||
def on_update
|
||||
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
|
||||
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
|
||||
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_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
use bevy_scriptum::runtimes::ruby::prelude::*;
|
||||
|
||||
fn call_lua_on_update_from_rust(
|
||||
mut scripted_entities: Query<(Entity, &mut LuaScriptData)>,
|
||||
scripting_runtime: ResMut<LuaRuntime>,
|
||||
fn call_ruby_on_update_from_rust(
|
||||
mut scripted_entities: Query<(Entity, &mut RubyScriptData)>,
|
||||
scripting_runtime: ResMut<RubyRuntime>,
|
||||
) {
|
||||
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
|
||||
.call_fn("on_update", &mut script_data, entity, ())
|
||||
.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_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
use bevy_scriptum::runtimes::ruby::prelude::*;
|
||||
|
||||
fn call_lua_on_update_from_rust(
|
||||
mut scripted_entities: Query<(Entity, &mut LuaScriptData)>,
|
||||
scripting_runtime: ResMut<LuaRuntime>,
|
||||
fn call_ruby_on_update_from_rust(
|
||||
mut scripted_entities: Query<(Entity, &mut RubyScriptData)>,
|
||||
scripting_runtime: ResMut<RubyRuntime>,
|
||||
) {
|
||||
for (entity, mut script_data) in &mut scripted_entities {
|
||||
scripting_runtime
|
||||
|
|
@ -57,13 +57,10 @@ fn call_lua_on_update_from_rust(
|
|||
}
|
||||
```
|
||||
|
||||
They will be passed to `on_update` Lua function
|
||||
```lua
|
||||
function on_update(a, b)
|
||||
print(a) -- 123
|
||||
print(b) -- hello
|
||||
They will be passed to `on_update` Ruby function
|
||||
```ruby
|
||||
def on_update(a, b)
|
||||
puts(a) # 123
|
||||
puts(b) # hello
|
||||
end
|
||||
```
|
||||
|
||||
Any type that implements `IntoLua` can be passed as an argument withing the
|
||||
tuple in `call_fn`.
|
||||
|
|
|
|||
|
|
@ -18,12 +18,12 @@ You can now start exposing functions to the scripting language. For example, you
|
|||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
use bevy_scriptum::runtimes::ruby::prelude::*;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_scripting::<LuaRuntime>(|runtime| {
|
||||
.add_scripting::<RubyRuntime>(|runtime| {
|
||||
runtime.add_function(
|
||||
String::from("my_print"),
|
||||
|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!")
|
||||
```
|
||||
|
||||
|
|
@ -49,12 +49,12 @@ And spawn an entity with attached `Script` component with a handle to a script s
|
|||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
use bevy_scriptum::runtimes::ruby::prelude::*;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_scripting::<LuaRuntime>(|runtime| {
|
||||
.add_scripting::<RubyRuntime>(|runtime| {
|
||||
runtime.add_function(
|
||||
String::from("my_print"),
|
||||
|In((x,)): In<(String,)>| {
|
||||
|
|
@ -63,7 +63,7 @@ fn main() {
|
|||
);
|
||||
})
|
||||
.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();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ Add the following to your `Cargo.toml`:
|
|||
```toml
|
||||
[dependencies]
|
||||
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
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ of all entities with `Player` component.
|
|||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
use bevy_scriptum::runtimes::ruby::prelude::*;
|
||||
|
||||
#[derive(Component)]
|
||||
struct Player;
|
||||
|
|
@ -22,7 +22,7 @@ struct Player;
|
|||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_scripting::<LuaRuntime>(|runtime| {
|
||||
.add_scripting::<RubyRuntime>(|runtime| {
|
||||
runtime.add_function(
|
||||
String::from("print_player_names"),
|
||||
|players: Query<&Name, With<Player>>| {
|
||||
|
|
@ -38,8 +38,8 @@ fn main() {
|
|||
|
||||
In script:
|
||||
|
||||
```lua
|
||||
print_player_names()
|
||||
```ruby
|
||||
print_player_names
|
||||
```
|
||||
|
||||
You can use functions that interact with Bevy entities and resources and
|
||||
|
|
@ -53,7 +53,7 @@ component.
|
|||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
use bevy_scriptum::runtimes::ruby::prelude::*;
|
||||
|
||||
#[derive(Component)]
|
||||
struct Player {
|
||||
|
|
@ -63,7 +63,7 @@ struct Player {
|
|||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_scripting::<LuaRuntime>(|runtime| {
|
||||
.add_scripting::<RubyRuntime>(|runtime| {
|
||||
runtime.add_function(
|
||||
String::from("hurt_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:
|
||||
|
||||
```lua
|
||||
```ruby
|
||||
hurt_player(5)
|
||||
```
|
||||
|
|
|
|||
|
|
@ -1,3 +0,0 @@
|
|||
# Lua
|
||||
|
||||
This chapter demonstrates how to work with bevy_scriptum when using Lua language runtime.
|
||||
|
|
@ -1 +1,3 @@
|
|||
# Ruby
|
||||
|
||||
This chapter demonstrates how to work with bevy_scriptum when using Ruby language runtime.
|
||||
|
|
|
|||
|
|
@ -9,18 +9,18 @@ bevy's `AssetServer`.
|
|||
|
||||
use bevy::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>) {
|
||||
commands.spawn(Script::<LuaScript>::new(
|
||||
assets_server.load("my_script.lua"),
|
||||
commands.spawn(Script::<RubyScript>::new(
|
||||
assets_server.load("my_script.rb"),
|
||||
));
|
||||
}
|
||||
```
|
||||
|
||||
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
|
||||
```LuaScriptData``` component will be attached.
|
||||
been attached to will get the `Script::<RubyScript>` component stripped and instead
|
||||
```RubyScriptData``` component will be attached.
|
||||
|
||||
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_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
use bevy_scriptum::runtimes::ruby::prelude::*;
|
||||
|
||||
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 {
|
||||
// do something with scripted entities
|
||||
|
|
|
|||
|
|
@ -174,7 +174,7 @@ fn then(r_self: magnus::Value) -> magnus::Value {
|
|||
.into_value()
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Debug)]
|
||||
#[magnus::wrap(class = "Bevy::Entity")]
|
||||
pub struct BevyEntity(pub Entity);
|
||||
|
||||
|
|
@ -191,7 +191,7 @@ impl TryConvert for BevyEntity {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Debug)]
|
||||
#[magnus::wrap(class = "Bevy::Vec3")]
|
||||
pub struct BevyVec3(pub Vec3);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue