is inheritence possible? #680
Replies: 3 comments
-
|
You can do something like: struct Instance(String);
impl UserData for Instance {
fn register(registry: &mut UserDataRegistry<Self>) {
registry.add_field_method_get("Name", |_, this| Ok(this.0.clone()));
}
}
struct WorldInstance {
parent: AnyUserData,
position: f32,
}
impl UserData for WorldInstance {
fn register(registry: &mut UserDataRegistry<Self>) {
registry.add_field_method_get("Position", |_, this| Ok(this.position));
registry.add_meta_method("__index", |_, this, key: String| {
use mlua::ObjectLike as _;
this.parent.get::<Value>(key)
});
}
}
let instance = lua.create_userdata(Instance("example".to_string()))?;
lua.globals().set("Instance", &instance)?;
lua.globals().set(
"WorldInstance",
lua.create_userdata(WorldInstance {
parent: instance,
position: 1.23,
})?,
)?; |
Beta Was this translation helpful? Give feedback.
-
|
that does work, though the only issue that i have is that if the key doesn't exist, it would return |
Beta Was this translation helpful? Give feedback.
-
you sure? Accessing This can happen though for static fields. In Lua |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
i was wondering if it is possible to have userdata that inherits another userdata with the luau feature.
i've already tried setting
__indexto return the super class. i also tried returning the metatable of the super, though i wasn't able to due to the metatable being wrapped aroundUserDataMetatable.my goal is to have two types named
InstanceandWorldInstance, whereInstancehas a field namedNameandWorldInstancehas a field namedPosition. the user would then be able to access theNamefield from an instance ofWorldInstance. would that be possible?Beta Was this translation helpful? Give feedback.
All reactions