-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #166 from yarn-slinger/book
Write more docs
- Loading branch information
Showing
9 changed files
with
179 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,64 @@ | ||
# Custom Commands | ||
|
||
TODO (Feel free to [contribute](https://github.com/yarn-slinger/yarn-slinger/edit/main/docs/src/bevy_plugin/custom_commands.md)!) | ||
[Commands](../yarn_files/commands.md) work very similar to Yarn [functions](../yarn_files/functions.md), but | ||
use a different syntax and are able to modify the game world. As a consequence of their similarity, | ||
registering custom commands is very similar to registering [custom functions](./custom_functions.md). | ||
|
||
## Command Registration | ||
|
||
Just as with Yarn functions, registration happens when creating a `DialogRunner`. | ||
Let's again modify the example from the [setup](./setup.md): | ||
|
||
```rust | ||
fn spawn_dialogue_runner(mut commands: Commands, project: Res<YarnProject>) { | ||
let mut dialogue_runner = project.create_dialogue_runner(); | ||
// Add our custom command to the dialogue runner | ||
dialogue_runner | ||
.commands_mut() | ||
.add_command("print_addition", print_addition); | ||
dialogue_runner.start_node("Start"); | ||
commands.spawn(dialogue_runner); | ||
} | ||
|
||
fn print_addition(In((a, b)): In<(f32, f32)>) { | ||
print!("{a} + {b} = {c}", c = a + b) | ||
} | ||
``` | ||
|
||
We call the command like this: | ||
|
||
```text | ||
title: Start | ||
--- | ||
Let's print the addition of 1 and 3 in the console: | ||
<<print_addition(1, 3)>> | ||
=== | ||
``` | ||
|
||
You will have seen one crucial difference to Yarn functions immediately. | ||
The parameters are not passed in directly to the Rust function, but are wrapped in an `In` struct. | ||
This is because Rust functions that are registered as commands are always valid Bevy systems. | ||
The `In` parameter just tells the function which values come from the Yarn file, but we can additionally query the Bevy world as we want: | ||
|
||
```rust | ||
fn spawn_dialogue_runner(mut commands: Commands, project: Res<YarnProject>) { | ||
let mut dialogue_runner = project.create_dialogue_runner(); | ||
// Add our custom command to the dialogue runner | ||
dialogue_runner | ||
.commands_mut() | ||
.add_command("insert_resource", insert_resource); | ||
dialogue_runner.start_node("Start"); | ||
commands.spawn(dialogue_runner); | ||
} | ||
|
||
TODO | ||
fn insert_resource(In(name): In<&str>) { | ||
print!("{a} + {b} = {c}", c = a + b) | ||
} | ||
``` | ||
|
||
TODO | ||
|
||
TODO mention return types | ||
|
||
TODO mention that no params still needs In param |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,93 @@ | ||
# Custom Functions | ||
|
||
TODO (Feel free to [contribute](https://github.com/yarn-slinger/yarn-slinger/edit/main/docs/src/bevy_plugin/custom_functions.md)!) | ||
As mentioned in the chapter [Functions](../yarn_files/functions.md), Yarn can access user-defined functions. | ||
A collection of functions is called a *library* and can be accessed through a `DialogRunner`. | ||
|
||
## Function Registration | ||
|
||
For an easy example, let's modify the code used in the [setup](./setup.md) to provide a simple `pow` function to Yarn: | ||
```rust | ||
fn spawn_dialogue_runner(mut commands: Commands, project: Res<YarnProject>) { | ||
let mut dialogue_runner = project.create_dialogue_runner(); | ||
// Add our custom function to the dialogue runner | ||
dialogue_runner.library_mut().add_function("pow", pow); | ||
dialogue_runner.start_node("Start"); | ||
commands.spawn(dialogue_runner); | ||
} | ||
|
||
fn pow(base: f32, exponent: f32) -> f32 { | ||
base.powf(exponent) | ||
} | ||
``` | ||
|
||
The following snippet is of special importance: | ||
```rust | ||
dialogue_runner.library_mut().add_function("pow", pow); | ||
``` | ||
The first parameter of `add_function()` is the name of the function as seen by Yarn, `"pow"` in this case. | ||
The second parameter is the Rust function that will be called in the background. | ||
Here, we reference the function definition of `fn pow(...)`, but you could also register a lambda. | ||
|
||
|
||
This `pow` function can now be called from the Yarn file like this: | ||
|
||
```text | ||
title: Start | ||
--- | ||
Two to the power of three is {pow(2,3)} | ||
=== | ||
``` | ||
|
||
Which will result in the following output: | ||
 | ||
|
||
## Allowed Signatures | ||
|
||
Custom functions need to follow some rules. Don't worry, they're pretty lax. | ||
|
||
- Their parameter and output types need to be primitive types or `String` | ||
- Parameters are allowed to be references | ||
- Parameters can have the special type `YarnValue`, which stands for any input type. | ||
Additionally, functions are assumed to have no side effects. You can read the full list of requirements in the docs for `YarnFn`. | ||
|
||
Here are some examples of valid functions: | ||
```rust | ||
fn add(a: f32, b: f32) -> f32 { | ||
a + b | ||
} | ||
|
||
fn concat(a: &str, b: &str) -> String { | ||
format!("{a}{b}") | ||
} | ||
|
||
fn greet(name: &str, age: usize) -> String { | ||
format!("Hello {name}, you are {age} years old!") | ||
} | ||
|
||
fn format_anything(value: YarnValue) -> String { | ||
format!("Got the following value: {value}") | ||
} | ||
``` | ||
|
||
If you need functions that have side effects, e.g. for manipulating the game world, use [custom commands](./custom_commands.md) instead. | ||
|
||
## Size constraints | ||
|
||
Registered Rust functions can have a maximum of 16 parameters. | ||
If you need more, you can wrap parameters in tuples: | ||
|
||
```rust | ||
fn add((a, b): (f32, f32)) -> f32 { | ||
a + b | ||
} | ||
``` | ||
|
||
Tuples are treated as separate parameters when calling the function from Yarn: | ||
```text | ||
title: Start | ||
--- | ||
Two plus three is {add(2, 3)} | ||
=== | ||
``` | ||
|
||
Since tuples can be nested, you can use have potentially infinite parameters. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters