mLua

# Course Introduction

Scripts in an editor outside of MapleStory World maker must follow mLua syntax. Let's learn about mLua syntax.

##### Reference Guide

[LocalWorkspace](/docs/?postId=1165%7B%22target%22:%22_blank%22%7D) [ExtendedScriptFormat](/docs/?postId=1166%7B%22target%22:%22_blank%22%7D) [mLua Extension](/docs?postId=1209%7B%22target%22:%22_blank%22%7D) [Coproduction](/docs/?postId=670%7B%22target%22:%22_blank%22%7D)

# mLua

This is the programming language for expressing MSW maker's script entries.

# Definition Statement

## Script Definition Statement

You can define scripts by using script definition statements. The following is the script definition statement syntax.
    
    
    @Component
    script TemplateScript_1287_1675 extends Component
        -- [Original MLUA template/example preserved as comment]
        -- script Type Name
        -- 
        -- end
    end
    

If there is an inheritance relationship, you can attach extends to mark which type the inheritance comes from. The following is the definition statement syntax for script with extends.
    
    
    @Component
    script TemplateScript_1287_1676 extends Component
        -- [Original MLUA template/example preserved as comment]
        -- script Type Name extends Type Name
        -- 
        -- end
    end
    

## Property Definition Statement

You can define a script's property through a property definition statement. For property definition statements, the right-hand expression must be written with the exception of some types. SyncTable types are unassignable, and the right-side expressions are omitted.
    
    
    @Component
    script TemplateScript_1287_1677 extends Component
        -- [Original MLUA template/example preserved as comment]
        -- property TypeName Name = Expression
    end
    

## Method Definition Statement

You can define methods in a script through a method definition statement. The following is the syntax for method definition statements.
    
    
    @Component
    script TemplateScript_1287_1678 extends Component
        -- [Original MLUA template/example preserved as comment]
        -- method Return Type Name Method Name (Parameter Type Name Parameter Name)
        -- 
        -- end
    end
    

Use Example
    
    
    @Component
    script TemplateScript_1287_1636 extends Component
        -- [Original MLUA template/example preserved as comment]
        -- method string NewMethod(number num)
        --     return ""
        -- end
    end
    

## Event Handler Definition Statement

You can define event handlers in a script through an event handlerstatement. The following is the syntax for event handler definition statements.
    
    
    handler Handler Name(Event Type Name Parameter Name)
    
    end
    

Use Example
    
    
    @Component
    script TemplateScript_1287_1637 extends Component
        -- [Original MLUA template/example preserved as comment]
        -- handler HandleEvent(KeyDownEvent event)
        -- 
        -- end
    end
    

## Attribute Statement

You can designate various functions to scripts and members using Attribute statements.You can use multiple Attribute statements for a single member. The following is the syntax for the Attribute statement.
    
    
    @Component
    script TemplateScript_1287_1638 extends Component
        -- [Original MLUA template/example preserved as comment]
        -- @AttributeName
    end
    

Use Example
    
    
    @Component
    script NewValueSynchronizer extends Component
        @Sync
        property string NewValue = nil
    end
    

An Attribute can have parameters. The following is the syntax for an Attribute that has a parameter.
    
    
    @Component
    script TemplateScript_1287_1679 extends Component
        -- [Original MLUA template/example preserved as comment]
        -- @AttributeName(Argument)
    end
    

Use Example
    
    
    @Component
    script TemplateScript_1287_1641 extends Component
        -- [Original MLUA template/example preserved as comment]
        -- @ExecSpace("Server")
        -- method void OnBeginPlay()
        -- 
        -- end
    end
    

# Attribute

An Attribute is required to apply functions like synchronization, execution control, min values, and max values to script members in mLua. You can set multiple Attributes to a single member.

#### Sync Attribute

Sets the sync status on a property. The application target is property. The application target is a property with a Property type that can be synced. The aplicable types are Component, Logic, and BTNode.

  * Parameter: None


    
    
    @Component
    script SyncedValue extends Component
        @Sync
        property string NewValue = nil
    end
    

#### TargetUserSync Attribute

Sets the sync status of certain targets. The application targets are properties and components with Property types that can be synced.

  * Parameter: None


    
    
    @TargetUsrSync
    

#### ExecSpace Attribute

Sets the execution control space for methods and event handlers. Only segments set as strings can be used as parameters. The application targets are methods and event handlers. The applicable types are Component and Logic.

  * Parameter: string ("Server", "Client", "ServerOnly", "ClientOnly", "Multicast")


    
    
    @Component
    script TemplateScript_1287_1643 extends Component
        -- [Original MLUA template/example preserved as comment]
        -- @ExecSpace("Client")
        -- @ExecSpace("Server")
        -- @ExecSpace("ServerOnly")
        -- @ExecSpace("ClientOnly")
        -- @ExecSpace("Multicast")
    end
    

#### InspectorButton Attribute

  * Adds a method that executes the target method to the property editor. The application target is the method.

  * Parameter: string



    
    
    @Component
    script TemplateScript_1287_1644 extends Component
       -- [Original MLUA template/example preserved as comment]
       -- @InspectorButton("ButtonName")
       -- method void Function1()
       -- 
       -- end
    end
    

#### EventSender Attribute

Designates the event sending object of an event handler. The application target is event handler, and the EventSender Attribute must be used. Only segments set as strings can be used as parameters.

  * Parameter: string ("Self", "Entity", "Model", "LocalPlayer", "Service", "Logic"), string(optional)



> Tip The value that can be used as the second parameter depends on the string used as the first parameter.
> 
>   * "Entity", "Model": Entity Id must be entered as the second parameter.
>   * "Service", "Logic": Type name must be entered as the second parameter.
>   * "Self", "LocalPlayer": The second parameter isn't used.
> 

    
    
    @Component
    script TemplateScript_1287_1645 extends Component
        -- [Original MLUA template/example preserved as comment]
        -- @EventSender("Service", "InputService")
        -- handler handleEvent(keyDownEvent event)
        -- 
        -- end
    end
    

#### DisplayName Attribute

Sets the name that will be displayed in MSW maker's property editor. The application target is property.

  * Parameter: string


    
    
    @Component
    script StringSetting extends Component
        @DisplayName("Test Value")
        property string NewValue = nil
    end
    

#### Description Attribute

Description that displays as a tooltip in MSW maker's property editor. The application target is property.

  * Parameter: string


    
    
    @Component
    script StringValue extends Component
        @Description("Property Description")
        property string NewValue = nil
    end
    

#### HideFromInspector Attribute

Sets whether it will display in MSW maker's property editor. The application target is property.

  * Parameter: string


    
    
    @Component
    script RuntimeDataContainer extends Component
        @HideFromInspector
        property string NewValue = nil
    end
    

#### MinValue Attribute

Sets the minmum value in MSW maker's property editor. The application targets are number, integer type properties.

  * Parameter: number


    
    
    @Component
    script ConstrainedNumber extends Component
        @MinValue(2)
        property number NewValue = 3
    end
    

#### MaxValue Attribute

Sets the max value in MSW maker's property editor. The application targets are number, integer type properties.

  * Parameter: number


    
    
    @Component
    script ConstrainedNumber extends Component
        @MaxValue(10)
        property number NewValue = 3
    end
    

#### Delta Attribute

Sets the change value when pressing the left/right buttons in MSW Maker's property editor. The application targets are number, integer type properties.

  * Parameter: number


    
    
    @Component
    script DeltaValue extends Component
        @Delta(1)
        property number NewValue = 3
    end
    

#### MaxLength Attribute

Sets the max length in MSW maker's property editor. The application targets are string type properties.

  * Parameter: integer


    
    
    @Component
    script StringLengthLimiter extends Component
        @MaxLength(5)
        property string NewVlaue = "test"
    end
    

#### Component Attribute

Sets a script as a Component. The application targets are scripts.

  * Parameter: **None **


    
    
    @Component
    script TemplateScript_1287_1653 extends Component
        -- [Original MLUA template/example preserved as comment]
        -- @Component
        -- script NewComponent extends Component
    end
    

#### Event Attribute

Sets the script as an EventType. The application targets are scripts.

  * Parameter: None


    
    
    @Event
    script NewEvent extends EventType
    
    end
    

#### Item Attribute

Sets the script as an ItemType. The application targets are scripts.

  * Parameter: None


    
    
    @Item
    script NewItem extends Item
    
    end
    

#### BTNode Attribute

Sets the script as BTNodeType. The application targets are scripts.

  * Parameter: None


    
    
    @BTNode
    script NewBTNode extends BTNode
    
    end
    

#### State Attribute

Sets the script as StateType. The application targets are scripts.

  * Parameter: None


    
    
    @State
    script NewState extends StateType
    
    end
    

#### Struct Attribute

Sets the script as StructType. The application targets are scripts.

  * Parameter: None


    
    
    @Component
    script TemplateScript_1287_1658 extends Component
        -- [Original MLUA template/example preserved as comment]
        -- @Struct
        -- script NewStruct
        -- 
        -- end
    end
    

#### Logic Attribute

Sets the script as Logic. The application targets are scripts.

  * Parameter: None


    
    
    @Logic
    script NewLogic extends Logic
    
    end
    

# Annotation

Annotations are a type of metadata that is added to code. Writing annotations in the '---@' format can offer various assistance when writing actual code.

#### Type Annotation

Type Annotation types can be explicitly set. It can be used in assignment statements and property definition statements.
    
    
    @Component
    script ServerBootstrapper extends Component
        @ExecSpace("ServerOnly")
        method void OnBeginPlay()
            ---@type any
        end
    end
    

Use Example
    
    
    @Component
    script ServerInitializer extends Component
        @ExecSpace("ServerOnly")
        method void OnBeginPlay()
            ---@type string
            local testValue = "test"
        end
    end
    
    
    
    @Component
    script StartupValueInitializer extends Component
        @ExecSpace("ServerOnly")
        method void OnBeginPlay()
            ---@type string, integer
            local strValue, intValue = "str", 5
        end
    end
    

#### Param Annotation

Param Annotation can set the parameter type for targetted functions. Used in function definition statements.
    
    
    @Component
    script ServerInitializer extends Component
        @ExecSpace("ServerOnly")
        method void OnBeginPlay()
            ---@param param_name any
        end
    end
    

Use Example
    
    
    @Component
    script EntityTester extends Component
        @ExecSpace("ServerOnly")
        method void OnBeginPlay()
            ---@param testParamA Entity
            local function TestFunction(testParamA)
            end
        end
    end
    
    
    
    @Component
    script TestFunctionInitializer extends Component
        @ExecSpace("ServerOnly")
        method void OnBeginPlay()
            ---@param testParamA Entity
            ---@param testParamB integer
            local function TestFunction(testParamA, testParamB)
            
            end
        end
    end
    

#### Return Annotation

Return Annotations can set the return type of the targeted function. Used in function definition statements and method definition statements. Return Annotations can be assisted by the script assist by setting the return type of the target function.
    
    
    @Component
    script ServerInitializer extends Component
        @ExecSpace("ServerOnly")
        method void OnBeginPlay()
            ---@return any
        end
    end
    

Use Example
    
    
    @Component
    script TestEntityGetter extends Component
        @ExecSpace("ServerOnly")
        method void OnBeginPlay()
            ---@return Entity
            local function TestFunction(testParamA, testParamB)
                return self.Entity
            end
        end
    end
    
    
    
    @Component
    script EntityStringProvider extends Component
        @ExecSpace("ServerOnly")
        method void OnBeginPlay()
            ---@return Entity, string
            local function TestFunction(testParamA, testParamB)
                return self.Entity, "strValue"
            end
        end
    end
    

#### Deprecated Annotation

Deprecated Annotations mark something as no longer in use. Used in script definition statements, property definition statements, method definition statements, and event handler definition statements.
    
    
    @Component
    script ObsoleteBehavior extends Component
        @ExecSpace("ServerOnly")
        method void OnBeginPlay()
            ---@deprecated
        end
    end
    

Use Example
    
    
    @Component
    script DeprecatedValueContainer extends Component
        ---@deprecated
        property any NewValue = nil
    end
    

#### Sealed Annotation

Sealed Annotations mark members that will no longer be overridden. Used in property definition statements, method definition statements, and event handler definition statements.
    
    
    @Component
    script ServerContext extends Component
        @ExecSpace("ServerOnly")
        method void OnBeginPlay()
            ---@sealed
        end
    end
    

Use Example
    
    
    @Component
    script ValueHolder extends Component
        ---@sealed
        property any NewValue = nil
    end
    

#### Description Annotation

Description Annotations are used for descriptions that are shown in tooltips. Used in script definition statements, property definition statements, method definition statements, and event handler definition statements.
    
    
    @Component
    script PlaytimeTest extends Component
        @ExecSpace("ServerOnly")
        method void OnBeginPlay()
            ---@description "Test"
        end
    end
    

Use Example
    
    
    @Component
    script TestValueHolder extends Component
        ---@description "Test Value"
        property any NewValue = nil
    end