Jump to content

Wiki OOP - mt structure


Piorun

Recommended Posts

It describes how *they* have implemented OOP for MTA builtin functions.

Element = { 
    create = createElement, 
    setPosition = setElementPosition, 
    ... 
} 
  
-- Hidden in lua registry, applied to userdata 
ElementMT = { 
    __index = CLuaClassDefs::Index, -- accessing missing key with element[key] or element.key will call CLuaClassDefs::Index. This will call __class or __get 
    __newindex = CLuaClassDefs::NewIndex, -- setting element[key] = value or element.key = value will call CLuaClassDefs::NewIndex. This will call __set. 
    __class = Element, -- private field, so __call can use it and maybe something else as well 
    __call = __class.create, -- calling element(foo) will call __class.create(element, foo), which is Element.create(element, foo) 
  
    __set = { -- not in Lua specs, MTA handles it somehow - creates setters for contained names 
        type = CLuaClassDefs::ReadOnly, -- setting Element type is not allowed (but getting is) 
        health = setElementHealth, 
        ... 
    }, 
    __get = { -- same as __set, only for getters 
        type = getElementType, 
        health = getElementHealth, 
        ... 
    }, 
} 
  

It's important to look at https://github.com/multitheftauto/mtasa ... ssDefs.cpp to see what's going on.

Also see http://lua-users.org/wiki/MetatableEvents

I am not sure though why they have in that wiki example setPosition in Element instead of ElementMT.__set.position

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...