2022-05-16 13:53:20 +00:00
|
|
|
//! Demonstrates how reflection is used with generic Rust types.
|
|
|
|
|
2021-03-09 01:07:01 +00:00
|
|
|
use bevy::{prelude::*, reflect::TypeRegistry};
|
2020-11-28 00:39:59 +00:00
|
|
|
use std::any::TypeId;
|
|
|
|
|
|
|
|
fn main() {
|
2021-07-27 20:21:06 +00:00
|
|
|
App::new()
|
2020-11-28 00:39:59 +00:00
|
|
|
.add_plugins(DefaultPlugins)
|
2022-05-16 13:53:20 +00:00
|
|
|
// You must manually register each instance of a generic type
|
2020-11-28 00:39:59 +00:00
|
|
|
.register_type::<MyType<u32>>()
|
2021-07-27 23:42:36 +00:00
|
|
|
.add_startup_system(setup)
|
2020-11-28 00:39:59 +00:00
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Reflect)]
|
|
|
|
struct MyType<T: Reflect> {
|
|
|
|
value: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup(type_registry: Res<TypeRegistry>) {
|
|
|
|
let type_registry = type_registry.read();
|
|
|
|
|
|
|
|
let registration = type_registry.get(TypeId::of::<MyType<u32>>()).unwrap();
|
2021-04-22 23:30:48 +00:00
|
|
|
info!("Registration for {} exists", registration.short_name());
|
2020-11-28 00:39:59 +00:00
|
|
|
|
|
|
|
// MyType<String> was not manually registered, so it does not exist
|
|
|
|
assert!(type_registry.get(TypeId::of::<MyType<String>>()).is_none());
|
|
|
|
}
|