Skip to content

Commit 4ce4714

Browse files
authored
Merge pull request #538 from godot-rust/feature/onready-property
Enable `#[var]` and `#[export]` for `OnReady<T>` fields
2 parents f014ba5 + 41daed2 commit 4ce4714

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

godot-core/src/obj/onready.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
66
*/
77

8+
use crate::property::{Export, Property, PropertyHintInfo};
89
use std::mem;
910

1011
/// Ergonomic late-initialization container with `ready()` support.
@@ -186,7 +187,32 @@ impl<T> std::ops::DerefMut for OnReady<T> {
186187
}
187188
}
188189

190+
impl<T: Property> Property for OnReady<T> {
191+
type Intermediate = T::Intermediate;
192+
193+
fn get_property(&self) -> Self::Intermediate {
194+
let deref: &T = self;
195+
deref.get_property()
196+
}
197+
198+
fn set_property(&mut self, value: Self::Intermediate) {
199+
let deref: &mut T = self;
200+
deref.set_property(value);
201+
}
202+
203+
fn property_hint() -> PropertyHintInfo {
204+
T::property_hint()
205+
}
206+
}
207+
208+
impl<T: Export> Export for OnReady<T> {
209+
fn default_export_info() -> PropertyHintInfo {
210+
T::default_export_info()
211+
}
212+
}
213+
189214
// ----------------------------------------------------------------------------------------------------------------------------------------------
215+
// Implementation
190216

191217
enum InitState<T> {
192218
ManualUninitialized,

itest/rust/src/object_tests/onready_test.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use godot::engine::notify::NodeNotification;
1111
use godot::engine::INode;
1212

1313
use godot::obj::{Gd, OnReady};
14+
use godot::prelude::ToGodot;
1415

1516
#[itest]
1617
fn onready_deref() {
@@ -116,12 +117,36 @@ fn onready_lifecycle_with_impl_without_ready() {
116117
obj.free();
117118
}
118119

120+
#[itest]
121+
fn onready_property_access() {
122+
let mut obj = OnReadyWithImpl::create(true);
123+
obj.notify(NodeNotification::Ready);
124+
125+
obj.set("auto".into(), 33.to_variant());
126+
obj.set("manual".into(), 44.to_variant());
127+
128+
{
129+
let obj = obj.bind();
130+
assert_eq!(*obj.auto, 33);
131+
assert_eq!(*obj.manual, 44);
132+
}
133+
134+
let auto = obj.get("auto".into()).to::<i32>();
135+
let manual = obj.get("manual".into()).to::<i64>();
136+
assert_eq!(auto, 33);
137+
assert_eq!(manual, 44);
138+
139+
obj.free();
140+
}
141+
119142
// ----------------------------------------------------------------------------------------------------------------------------------------------
120143

121144
#[derive(GodotClass)]
122145
#[class(base=Node)]
123146
struct OnReadyWithImpl {
147+
#[export]
124148
auto: OnReady<i32>,
149+
#[var]
125150
manual: OnReady<i32>,
126151
runs_manual_init: bool,
127152
}

0 commit comments

Comments
 (0)