Skip to content

is inheritence possible? #679

Closed
Closed

Description

@SDFTDusername

i was wondering if it is possible to have userdata that inherits another userdata with the luau feature.
i've already tried setting __index to 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 around UserDataMetatable.

my goal is to have two types named Instance and WorldInstance, where Instance has a field named Name and WorldInstance has a field named Position. the user would then be able to access the Name field from an instance of WorldInstance. would that be possible?

Activity

  1. khvzak commented on Feb 9, 2026

    @khvzak
    Member

    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,
        })?,
    )?;
  2. SDFTDusername commented on Feb 10, 2026

    @SDFTDusername
    Author

    that does work, though the only issue that i have is that if the key doesn't exist, it would return nil instead of giving me an error. this also makes it unknown if its a field with a value of nil, or if the field doesn't exist.
    since i can't find any method for checking if a key exists, is there another way make it return an error for when the parent field doesn't exist?

  3. khvzak commented on Feb 10, 2026

    @khvzak
    Member

    if the key doesn't exist, it would return nil instead of giving me an error

    you sure? Accessing WorldInstance.NonExistent raise an exception runtime error: attempt to get an unknown field 'NonExistent'.

    This can happen though for static fields. In Lua nil and non-existent is the same (for example you cannot have a table with a key set to nil).

  4. locked and limited conversation to collaborators on Feb 10, 2026
  5. converted this issue into a discussion #680 on Feb 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions