1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//! 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`](crate::call::args::IntoValue):
//!
//! ```
//! # use bevy::prelude::*;
//! # use bevy_replique::prelude::*;
//!
//! #[derive(Resource)]
//! struct Gold(i64);
//!
//! /// `Alice: I have [gold()] coins.`
//! fn gold(In(()): In<()>, gold: Res<Gold>) -> i64 {
//! gold.0
//! }
//!
//! # let mut app = App::new();
//! # app.insert_resource(Gold(12));
//! # app.add_dialogue_function_named("gold", gold);
//! ```
//!
//! **Declare the name over the system.** `#[replique_function]` writes down
//! what the dialogue calls it, next to what it does, and
//! [`add_dialogue_function`](DialogueFunctionAppExt::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.
//!
//! ```
//! # use bevy::prelude::*;
//! # use bevy_replique::prelude::*;
//! # #[derive(Resource)]
//! # struct Gold(i64);
//! /// Coins the player has, as the dialogue reads it.
//! #[replique_function(name = "gold")]
//! fn coins(In(()): In<()>, gold: Res<Gold>) -> i64 {
//! gold.0
//! }
//!
//! # let mut app = App::new();
//! # app.insert_resource(Gold(12));
//! app.add_dialogue_function(coins);
//! # assert_eq!(coins::NAME, "gold");
//! ```
//!
//! [`add_dialogue_function_named`](DialogueFunctionAppExt::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`](replique::vm::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`](replique::vm::VmError::ExprEvalError).
//!
//! ```
//! # use bevy::prelude::*;
//! # use bevy_replique::prelude::*;
//! #[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"))
//! }
//! # let mut app = App::new();
//! # app.add_dialogue_function_named("hp_of", hp_of);
//! ```
//!
//! The `bevy_custom_function` example holds a full scene built this way.
use ;
use ;
use ;
type FunctionRunner =
;
pub
/// A function declared with `#[replique_function]`, name and doc included.
/// ```
/// # use bevy::prelude::*;
/// # use bevy_replique::prelude::*;
/// /// The name in upper case, to shout it.
/// #[replique_function(name = "upper")]
/// fn shout(In((text,)): In<(String,)>) -> String {
/// text.to_uppercase()
/// }
///
/// assert_eq!(shout::NAME, "upper");
/// assert_eq!(shout::DOC, "The name in upper case, to shout it.");
/// ```
///
/// Reach it through [`add_dialogue_function`], which is the whole of what it
/// is for.
///
/// [`add_dialogue_function`]: DialogueFunctionAppExt::add_dialogue_function
/// Registers the system to answer a function.
///
/// Two ways in, and [`add_dialogue_function`] is the one to reach for: the
/// name of the function then lives with the system it names, where it can be
/// read and documented, instead of being repeated as a string at the point of
/// registration. [`add_dialogue_function_named`] is for a system the attribute
/// cannot be put on — one coming from another crate, or one whose name is only
/// known at runtime.
///
/// Both refuse a name a builtin already answers to: `upper` and `len` are part
/// of the language, and a game that could shadow them would make the same line
/// read differently from one game to the next. The registration is skipped and
/// logged.
///
/// **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:
/// a function runs in the middle of a line being rendered, so an effect of its
/// own would depend on how often that happens. Writing to the world is what a
/// `>>` command is for.
///
/// [`add_dialogue_function`]: DialogueFunctionAppExt::add_dialogue_function
/// [`add_dialogue_function_named`]: DialogueFunctionAppExt::add_dialogue_function_named