is inheritence possible? #680
Unanswered
SDFTDusername
asked this question in
Q&A
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,
})?,
)?; |
0 replies
|
that does work, though the only issue that i have is that if the key doesn't exist, it would return |
0 replies
you sure? Accessing This can happen though for static fields. In Lua |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?All reactions