Skip to content

Commit 60857bb

Browse files
committed
register vec lua types FOR ALL THE TYPES BABY
1 parent d1f37a8 commit 60857bb

File tree

5 files changed

+308
-63
lines changed

5 files changed

+308
-63
lines changed

assets/scripts/bevy_api.lua

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ function on_event()
2525
print(comp.option_usize)
2626
comp.option_usize = nil
2727
print(comp.option_usize)
28-
world:exit()
2928

29+
print("vec")
3030
print(comp.vec_of_usize)
3131
print(comp.vec_of_usize[2])
3232
comp.vec_of_usize[2] = 69
@@ -35,16 +35,6 @@ function on_event()
3535

3636
print("============")
3737

38-
-- the index metamethod on ReflectValue's uses bevy's reflection mechanism on top of some custom sub-reflection logic to
39-
-- allow reflecting inside Options, Vectors etc.
40-
-- when we index into ReflectValue's we either get back a custom proxy or another ReflectValue
41-
42-
-- the LuaBevyAPIProvider provides us custom proxies for many bevy types as well as std types.
43-
-- all of these implementations can be overridden via the bevy TypeRegistry
44-
print("Hello:", comp.usize._1)
45-
comp.usize[1] = 2
46-
print("comp.usize after assigning to 2: ", comp.usize._1)
47-
4838
-- vec's and matrices have custom __index and __newindex overrides
4939
print("comp.vec2 before: ", comp.vec2)
5040
comp.vec2[1] = 69

crates/bevy_mod_scripting_core/src/reflection_extensions.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ pub trait PartialReflectExt {
1414
/// If the type is an option, returns either the inner value or None if the option is None.
1515
/// Errors if the type is not an option.
1616
fn as_option(&self) -> Result<Option<&dyn PartialReflect>, ScriptError>;
17+
18+
/// If the type is an iterable list-like type, returns an iterator over the elements.
19+
fn as_list(&self) -> Result<impl Iterator<Item = &dyn PartialReflect>, ScriptError>;
1720
}
1821

1922
impl<T: PartialReflect + ?Sized> PartialReflectExt for T {
@@ -39,8 +42,6 @@ impl<T: PartialReflect + ?Sized> PartialReflectExt for T {
3942
Ok(())
4043
}
4144

42-
/// If the type is an option, returns either the inner value or None if the option is None.
43-
/// Errors if the type is not an option.
4445
fn as_option(&self) -> Result<Option<&dyn PartialReflect>, ScriptError> {
4546
self.expect_type(Some("core"), "Option")?;
4647

@@ -54,6 +55,19 @@ impl<T: PartialReflect + ?Sized> PartialReflectExt for T {
5455

5556
unreachable!("core::Option is an enum with a tuple variant")
5657
}
58+
59+
fn as_list(&self) -> Result<impl Iterator<Item = &dyn PartialReflect>, ScriptError> {
60+
if let bevy::reflect::ReflectRef::List(l) = self.reflect_ref() {
61+
Ok(l.iter())
62+
} else {
63+
Err(ScriptError::new_runtime_error(format!(
64+
"Expected list-like type from crate core, but got {}",
65+
self.get_represented_type_info()
66+
.map(|ti| ti.type_path())
67+
.unwrap_or_else(|| "dynamic type with no type information")
68+
)))
69+
}
70+
}
5771
}
5872

5973
#[cfg(test)]

0 commit comments

Comments
 (0)