Skip to main content

TypedUserData

Derive Macro TypedUserData 

Source
#[derive(TypedUserData)]
{
    // Attributes available to this derive:
    #[lua]
}
Expand description

Generates a [mlua_extras::typed::TypedUserData] implementation from struct fields.

Each named and unnamed field is automatically exposed to Lua as a read and/or write property or index.

Use #[lua(...)] attributes to control access and naming:

  • readonly: Set the field to only be readable within Lua
  • writeonly: Set the field to only be writable within Lua
  • skip: Ignore generating and exposing the field
  • rename: Rename the field to a string for a named field and a digit for an indexed field

Note: readonly + writeonly together is the same as having neither, the field will be exposed for both read and write.

Optionally combine with [macro@typed_user_data_impl] to also register methods in a rust like manner.

§Example

ⓘ
#[derive(Clone, TypedUserData)]
struct Player {
    name: String,
    health: f64,
    #[lua(skip)]
    handle: u64,
    #[lua(readonly)]
    score: i32,
    #[lua(name = "pos_x")]
    position_x: f64,
}
ⓘ
#[derive(Clone, TypedUserData)]
enum PlayerAction {
    Idle,
    Move,
    Attack,
    Quit,
}