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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* Copyright (c) godot-rust; Bromeon and contributors.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
use crateGodotShape;
use crate;
use crate;
/// Exported property that must be initialized in the editor (or associated code) before use.
///
/// Use this type whenever your Rust code cannot provide a value for a field, but expects one to be specified in the Godot editor.
///
/// If you need automatic initialization during `ready()`, e.g. for loading nodes or resources, use [`OnReady<Gd<T>>`](crate::obj::OnReady)
/// instead. As a general "maybe initialized" type, `Option<Gd<T>>` is always available, even if more verbose.
///
///
/// # What constitutes "initialized"?
/// Whether a value is considered initialized or not depends on `T`.
///
/// - For objects, a value is initialized if it is not null. Exported object propreties in Godot are nullable, but `Gd<T>` and `DynGd<T, D>` do
/// not support nullability and can thus not directly be exported with `#[export]`. `OnEditor` can bridge this gap, by expecting users
/// to set a non-null value, and panicking if they don't.
/// - For built-in types, a value is initialized if it is different from a user-selected sentinel value (e.g. `-1`).
///
/// More on this below (see also table-of-contents sidebar).
///
/// # Initialization semantics
/// Panics during access (`Deref/DerefMut` impls) if uninitialized.
///
/// When used inside a node class, `OnEditor` checks if a value has been set before `ready()` is run, and panics otherwise.
/// This validation is performed for all `OnEditor` fields declared in a given `GodotClass`, regardless of whether they are `#[var]`, `#[export]`, or neither.
/// Once initialized, `OnEditor` can be used almost as if it were a `T` value itself, due to `Deref`/`DerefMut` impls.
///
/// `OnEditor<T>` should always be used as a struct field, preferably in tandem with an `#[export]` or `#[var]`.
/// Initializing `OnEditor` values via code before the first use is supported, but should be limited to use cases involving builder or factory patterns.
///
///
/// # Using `OnEditor` with classes
/// You can wrap class smart pointers `Gd<T>` and `DynGd<T, D>` inside `OnEditor`, to make them exportable.
/// `Gd<T>` itself does not implement the `Export` trait.
///
/// ## Example: automatic init
/// This example uses the `Default` impl, which expects a non-null value to be provided.
///
/// ```
/// # use godot::prelude::*;
/// #[derive(GodotClass)]
/// #[class(init, base = Node)]
/// struct ResourceHolder {
/// #[export]
/// editor_property: OnEditor<Gd<Resource>>,
/// }
///
/// #[godot_api]
/// impl INode for ResourceHolder {
/// fn ready(&mut self) {
/// // Will always be valid and **must** be set via editor.
/// // Additional check is being run before ready(),
/// // to ensure that given value can't be null.
/// let some_variant = self.editor_property.get_meta("SomeName");
/// }
/// }
/// ```
///
/// ## Example: user-defined init
/// Uninitialized `OnEditor<Gd<T>>` and `OnEditor<DynGd<T, D>>` can be created with `OnEditor::default()`.
///
/// ```
/// # use godot::prelude::*;
/// #[derive(GodotClass)]
/// #[class(base = Node)]
/// struct NodeHolder {
/// #[export]
/// required_node: OnEditor<Gd<Node>>,
///
/// base: Base<Node>
/// }
///
/// #[godot_api]
/// impl INode for NodeHolder {
/// fn init(base: Base<Node>) -> Self {
/// Self {
/// base,
/// required_node: OnEditor::default(),
/// }
/// }
/// }
///```
///
/// ## Example: factory pattern
///
/// ```
/// # use godot::prelude::*;
/// #[derive(GodotClass)]
/// #[class(init, base = Node)]
/// struct NodeHolder {
/// #[export]
/// required_node: OnEditor<Gd<Node>>,
/// }
///
/// fn create_and_add(
/// mut this: Gd<Node>,
/// some_class_scene: Gd<PackedScene>,
/// some_node: Gd<Node>,
/// ) -> Gd<NodeHolder> {
/// let mut my_node = some_class_scene.instantiate_as::<NodeHolder>();
///
/// // Would cause a panic:
/// // this.add_child(&my_node);
///
/// // It's possible to initialize the value programmatically, although typically
/// // it is set in the editor and stored in a .tscn file.
/// // Note: nodes are manually managed and leak memory unless tree-attached or freed.
/// my_node.bind_mut().required_node.init(some_node);
///
/// // Will not panic, since the node is initialized now.
/// this.add_child(&my_node);
///
/// my_node
/// }
/// ```
///
/// # Using `OnEditor` with built-in types
/// `OnEditor<T>` can be used with any `#[export]`-enabled builtins, to provide domain-specific validation logic.
/// An example might be to check whether a game entity has been granted a non-zero ID.
///
/// To detect whether a value has been set in the editor, `OnEditor<T>` uses a _sentinel value_. This is a special marker value for
/// "uninitialized" and is selected by the user. For example, a sentinel value of `-1` or `0` might be used to represent an uninitialized `i32`.
///
/// There is deliberately no `Default` implementation for `OnEditor` with builtins, as the sentinel is highly domain-specific.
///
/// ## Example
///
/// ```
/// use godot::prelude::*;
///
/// #[derive(GodotClass)]
/// #[class(init, base = Node)]
/// struct IntHolder {
/// // Uninitialized value will be represented by `42` in the editor.
/// // Will cause panic if not set via the editor or code before use.
/// #[export]
/// #[init(sentinel = 42)]
/// some_primitive: OnEditor<i64>,
/// }
///
/// fn create_and_add(mut this: Gd<Node>, val: i64) -> Gd<IntHolder> {
/// let mut my_node = IntHolder::new_alloc();
///
/// // Would cause a panic:
/// // this.add_child(&my_node);
///
/// // It's possible to initialize the value programmatically, although typically
/// // it is set in the editor and stored in a .tscn file.
/// my_node.bind_mut().some_primitive.init(val);
///
/// // Will not panic, since the node is initialized now.
/// this.add_child(&my_node);
///
/// my_node
/// }
/// ```
///
/// # Custom getters and setters for `OnEditor`
/// Custom setters and/or getters for `OnEditor` are declared by accepting/returning the inner type.
/// In their implementation, delegate to the `Var` trait methods, rather than dereferencing directly.
///
/// ```
/// # use godot::prelude::*;
/// #[derive(GodotClass)]
/// #[class(init)]
/// struct MyStruct {
/// #[var(get, set)]
/// my_node: OnEditor<Gd<Node>>,
///
/// #[var(get, set)]
/// #[init(sentinel = -1)]
/// my_value: OnEditor<i32>,
/// }
///
/// #[godot_api]
/// impl MyStruct {
/// #[func]
/// pub fn get_my_node(&self) -> Gd<Node> {
/// let ret = Var::var_pub_get(&self.my_node);
/// // Do something with the value...
/// ret
/// }
///
/// #[func]
/// pub fn set_my_node(&mut self, value: Gd<Node>) {
/// // Validate, pre-process, etc...
/// Var::var_pub_set(&mut self.my_node, value);
/// }
///
/// #[func]
/// pub fn get_my_value(&self) -> i32 {
/// let ret = Var::var_pub_get(&self.my_value);
/// // Do something with the value...
/// ret
/// }
///
/// #[func]
/// pub fn set_my_value(&mut self, value: i32) {
/// if value == 13 {
/// godot_warn!("13 is unlucky number.");
/// return;
/// }
///
/// Var::var_pub_set(&mut self.my_value, value);
/// }
/// }
/// ```
///
/// See also: [Register properties -- `#[var]`](../register/derive.GodotClass.html#register-properties--var)
///
/// # Using `OnEditor` with `#[class(tool)]`
/// When used with `#[class(tool)]`, the before-ready checks are omitted.
/// Otherwise, `OnEditor<T>` behaves the same — accessing an uninitialized value will cause a panic.
pub
/// `OnEditor<T>` is usable only for properties – which is enforced via `Var` and `FromGodot` bounds.
///
/// Furthermore, `PartialEq` is needed to compare against uninitialized sentinel values.