Expand description
Answering the functions of a dialogue.
Where a >> command does something, a function answers something. The
dialogue asks the game for a value and carries on with it, without ever
suspending. The answer of a function is a Value
For example: [let $stat = get_stat(Alice)] reads a component off the entity Alice,
[party_size()] counts them.
The arguments arrive already typed, as they do for a command, and the
return type is written back by IntoValue:
#[derive(Resource)]
struct Gold(i64);
/// `Alice: I have [gold()] coins.`
fn gold(In(()): In<()>, gold: Res<Gold>) -> i64 {
gold.0
}
Declare the name over the system. #[replique_function] writes down
what the dialogue calls it, next to what it does, and
add_dialogue_function
registers it from there. This is the way to register a function: the name
is written once, where it can be read and documented.
/// Coins the player has, as the dialogue reads it.
#[replique_function(name = "gold")]
fn coins(In(()): In<()>, gold: Res<Gold>) -> i64 {
gold.0
}
app.add_dialogue_function(coins);add_dialogue_function_named
takes the name as a string instead, for a system the attribute cannot be
put on.
A function may only read. The ReadOnlySystem bound makes it a compile
error to register one that spawns, mutates a component or writes a message.
Writing to the world is what a >> command is for.
A function cannot suspend. It answers on the spot, inside the call to
DialogueVm::resume_with.
Anything that has to wait for the player is a command.
A function that has no answer to give says so with a Result, which is return
by the VM with an VmError::ExprEvalError.
#[derive(Component, PartialEq)]
struct Name(String);
/// `[let $stat = hp_of("Alice")]`
fn hp_of(In((who,)): In<(String,)>, names: Query<&Name>) -> Result<i64, String> {
names
.iter()
.find(|name| name.0 == who)
.map(|_| 12)
.ok_or_else(|| format!("{who} is not on the scene"))
}The bevy_custom_function example holds a full scene built this way.
Traitsยง
- Dialogue
Function AppExt - Registers the system to answer a function.
- Replique
Function - A function declared with
#[replique_function], name and doc included.