Compare commits
5 commits
ba799ee110
...
35b4317040
| Author | SHA1 | Date | |
|---|---|---|---|
| 35b4317040 | |||
| f9a7a9eb77 | |||
| 97077338f9 | |||
| 4b25bd0f18 | |||
| 0d03a47eed |
21 changed files with 242 additions and 158 deletions
15
book/justfile
Normal file
15
book/justfile
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
export CARGO_MANIFEST_DIR := `pwd`
|
||||
|
||||
build-deps:
|
||||
cargo clean && cargo build
|
||||
|
||||
_test:
|
||||
mdbook test -L target/debug/deps/
|
||||
|
||||
test: build-deps _test
|
||||
|
||||
test-watch: build-deps
|
||||
watchexec --exts md -r just _test
|
||||
|
||||
serve:
|
||||
mdbook serve
|
||||
|
|
@ -24,7 +24,10 @@ bevy_scriptum's main advantages include:
|
|||
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 it.
|
||||
|
||||
All you need to do is register callbacks on your Bevy app like this:
|
||||
```rust
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
|
|
@ -47,7 +50,11 @@ 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:
|
||||
|
||||
```rust
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_ecs;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
|
|
@ -73,7 +80,10 @@ fn main() {
|
|||
```
|
||||
|
||||
You can also pass arguments to your callback functions, just like you would in a regular Bevy system - using `In` structs with tuples:
|
||||
```rust
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
|
|
@ -110,7 +120,10 @@ or execute `cargo add bevy_scriptum --features lua` from your project directory.
|
|||
|
||||
You can now start exposing functions to the scripting language. For example, you can expose a function that prints a message to the console:
|
||||
|
||||
```rust
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
|
|
@ -138,7 +151,10 @@ my_print("Hello world!")
|
|||
|
||||
And spawn an entity with attached `Script` component with a handle to a script source file:
|
||||
|
||||
```rust
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
|
|
@ -183,7 +199,10 @@ end)
|
|||
```
|
||||
which will print out `John` when used with following exposed function:
|
||||
|
||||
```rust
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
|
|
|
|||
|
|
@ -20,14 +20,17 @@ bevy_scriptum provides following types that can be used in Lua:
|
|||
|
||||
### Example Lua usage
|
||||
|
||||
```
|
||||
```lua
|
||||
my_vec = Vec3(1, 2, 3)
|
||||
set_translation(entity, my_vec)
|
||||
```
|
||||
|
||||
### Example Rust usage
|
||||
|
||||
```rust
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
|
|
@ -69,7 +72,10 @@ pass_to_rust(entity)
|
|||
|
||||
### Example Rust usage
|
||||
|
||||
```rust
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
|
|
|
|||
|
|
@ -3,7 +3,10 @@
|
|||
To call a rust function from Lua first you need to register a function
|
||||
within Rust using builder pattern.
|
||||
|
||||
```rust
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
|
|
@ -20,7 +23,10 @@ fn main() {
|
|||
|
||||
For example to register a function called `my_rust_func` you can do the following:
|
||||
|
||||
```rust
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
|
|
@ -49,7 +55,10 @@ 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.
|
||||
|
||||
```rust
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
|
|
@ -68,7 +77,10 @@ fn main() {
|
|||
|
||||
To make it look nicer you can destructure the `In` struct.
|
||||
|
||||
```rust
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
|
|
@ -97,7 +109,10 @@ Any registered rust function that returns a value will retrurn a promise when
|
|||
called within a script. By calling `:and_then` on the promise you can register
|
||||
a callback that will receive the value returned from Rust function.
|
||||
|
||||
```rust
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
|
|
|
|||
|
|
@ -13,7 +13,10 @@ of the function to call, `LuaScriptData` 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.
|
||||
|
||||
```rust
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
|
|
@ -29,14 +32,15 @@ fn call_lua_on_update_from_rust(
|
|||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
```
|
||||
|
||||
We can also pass some arguments by providing a tuple or `Vec` as the last
|
||||
`call_fn` argument.
|
||||
|
||||
```rust
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
|
|
@ -51,8 +55,6 @@ fn call_lua_on_update_from_rust(
|
|||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
```
|
||||
|
||||
They will be passed to `on_update` Lua function
|
||||
|
|
|
|||
|
|
@ -12,7 +12,10 @@ a create feature.
|
|||
|
||||
You can now start exposing functions to the scripting language. For example, you can expose a function that prints a message to the console:
|
||||
|
||||
```rust
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
|
|
@ -40,7 +43,10 @@ my_print("Hello world!")
|
|||
|
||||
And spawn an entity with attached `Script` component with a handle to a script source file:
|
||||
|
||||
```rust
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
|
|
|
|||
|
|
@ -7,7 +7,11 @@ That allows you to do anything you would do in a Bevy system.
|
|||
You could for example create a callback system function that prints names
|
||||
of all entities with `Player` component.
|
||||
|
||||
```rust
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_ecs;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
|
|
@ -42,7 +46,11 @@ You can use functions that interact with Bevy entities and resources and
|
|||
take arguments at the same time. It could be used for example to mutate a
|
||||
component.
|
||||
|
||||
```rust
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_ecs;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
|
|
@ -59,7 +67,7 @@ fn main() {
|
|||
runtime.add_function(
|
||||
String::from("hurt_player"),
|
||||
|In((hit_value,)): In<(i32,)>, mut players: Query<&mut Player>| {
|
||||
let mut player = players.single_mut();
|
||||
let mut player = players.single_mut().unwrap();
|
||||
player.health -= hit_value;
|
||||
},
|
||||
);
|
||||
|
|
|
|||
|
|
@ -4,6 +4,9 @@ To spawn a Lua script you will need to get a handle to a script asset using
|
|||
bevy's `AssetServer`.
|
||||
|
||||
```rust
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
|
|
@ -13,8 +16,6 @@ fn my_spawner(mut commands: Commands, assets_server: Res<AssetServer>) {
|
|||
assets_server.load("my_script.lua"),
|
||||
));
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
```
|
||||
|
||||
After they scripts have been evaled by bevy_scriptum, the entities that they've
|
||||
|
|
@ -24,6 +25,9 @@ been attached to will get the `Script::<LuaScript>` component stripped and inste
|
|||
So to query scipted entities you could do something like:
|
||||
|
||||
```rust
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
|
|
@ -35,6 +39,4 @@ fn my_system(
|
|||
// do something with scripted entities
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
```
|
||||
|
|
|
|||
|
|
@ -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,17 +1,20 @@
|
|||
# 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
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
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();
|
||||
|
|
@ -20,15 +23,18 @@ fn main() {
|
|||
|
||||
For example to register a function called `my_rust_func` you can do the following:
|
||||
|
||||
```rust
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
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");
|
||||
});
|
||||
|
|
@ -37,27 +43,27 @@ 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.
|
||||
|
||||
```rust
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
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);
|
||||
});
|
||||
|
|
@ -68,15 +74,18 @@ fn main() {
|
|||
|
||||
To make it look nicer you can destructure the `In` struct.
|
||||
|
||||
```rust
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
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);
|
||||
});
|
||||
|
|
@ -85,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)
|
||||
```
|
||||
|
||||
|
|
@ -97,15 +106,18 @@ Any registered rust function that returns a value will retrurn a promise when
|
|||
called within a script. By calling `:and_then` on the promise you can register
|
||||
a callback that will receive the value returned from Rust function.
|
||||
|
||||
```rust
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
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
|
||||
});
|
||||
|
|
@ -114,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,49 +1,53 @@
|
|||
# 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.
|
||||
|
||||
```rust
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
```
|
||||
|
||||
We can also pass some arguments by providing a tuple or `Vec` as the last
|
||||
`call_fn` argument.
|
||||
|
||||
```rust
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
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
|
||||
|
|
@ -51,17 +55,12 @@ fn call_lua_on_update_from_rust(
|
|||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
```
|
||||
|
||||
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`.
|
||||
|
|
|
|||
|
|
@ -12,15 +12,18 @@ a create feature.
|
|||
|
||||
You can now start exposing functions to the scripting language. For example, you can expose a function that prints a message to the console:
|
||||
|
||||
```rust
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
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,)>| {
|
||||
|
|
@ -32,23 +35,26 @@ 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!")
|
||||
```
|
||||
|
||||
And spawn an entity with attached `Script` component with a handle to a script source file:
|
||||
|
||||
```rust
|
||||
```rust,no_run
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
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,)>| {
|
||||
|
|
@ -57,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.
|
||||
|
|
|
|||
|
|
@ -4,37 +4,39 @@ To spawn a Lua script you will need to get a handle to a script asset using
|
|||
bevy's `AssetServer`.
|
||||
|
||||
```rust
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
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"),
|
||||
));
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
```
|
||||
|
||||
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:
|
||||
|
||||
```rust
|
||||
# extern crate bevy;
|
||||
# extern crate bevy_scriptum;
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
```
|
||||
|
|
|
|||
|
|
@ -95,8 +95,6 @@ fn teardown(
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
```
|
||||
|
||||
And to tie this all together we do the following:
|
||||
|
|
@ -122,11 +120,11 @@ fn main() {
|
|||
.run();
|
||||
}
|
||||
|
||||
fn init() {} // Implemented elsewhere
|
||||
fn update() {} // Implemented elsewhere
|
||||
fn despawn() {} // Implemented elsewhere
|
||||
fn teardown() {} // Implemented elsewhere
|
||||
fn spawn_player() {} // Implemented elsewhere
|
||||
# fn init() {}
|
||||
# fn update() {}
|
||||
# fn despawn() {}
|
||||
# fn teardown() {}
|
||||
# fn spawn_player() {}
|
||||
```
|
||||
|
||||
`despawn` can be implemented as:
|
||||
|
|
@ -141,8 +139,6 @@ use bevy_scriptum::runtimes::lua::prelude::*;
|
|||
fn despawn(In((entity,)): In<(BevyEntity,)>, mut commands: Commands) {
|
||||
commands.entity(entity.0).despawn();
|
||||
}
|
||||
|
||||
fn main() {} // Implemented elsewhere
|
||||
```
|
||||
|
||||
Implementation of `spawn_player` has been left out as an exercise for the reader.
|
||||
|
|
|
|||
|
|
@ -1,3 +0,0 @@
|
|||
#!/bin/sh
|
||||
|
||||
CARGO_MANIFEST_DIR=$(pwd) cargo clean && cargo build && mdbook test -L target/debug/deps/
|
||||
|
|
@ -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