Jump to content

[oop] Inheritance in MTA


Piorun

Recommended Posts

Hi,

I want to make class which will be extended with Vehicle class in LUA. I know about OOP in other langs and I know how to create class in MTA. I did anyway inh with 2 classes made by me but I want to create class which will be extended by Vehicle class (have same methods and variables as Vehicle class and also mine vars and methods wrote by me).

Link to comment

I've never actually used OOP in MTA since Lua doesn't support it by default, the MTA team has created OOP theirselfs with meta tables since that was recommended by Lua itself. yet I don't feel the OOP vibe in lua and therefore I'm not using it.

An example can be found on the wiki;

-- Exposed to global environment 
Element = { 
    create = createElement, 
    setPosition = setElementPosition, 
    ... 
} 
  
Vehicle = { 
    create = createVehicle, 
    setColor = setVehicleColor, 
    ... 
} 
  
-- Hidden in lua registry, applied to userdata 
ElementMT = { 
    __index = CLuaClassDefs::Index, 
    __newindex = CLuaClassDefs::NewIndex, 
    __class = Element, 
    __call = __class.create, 
    __set = { 
        type = CLuaClassDefs::ReadOnly, 
        health = setElementHealth, 
        ... 
    }, 
    __get = { 
        type = getElementType, 
        health = getElementHealth, 
        ... 
    }, 
} 
  
VehicleMT = { 
    __index = CLuaClassDefs::Index, 
    __newindex = CLuaClassDefs::NewIndex, 
    __class = Vehicle, 
    __parent = ElementMT, 
    __call = __class.create, 
    __set = { 
        damageProof = setVehicleDamageProof 
        ... 
    }, 
    __get = { 
        damageProof = isVehicleDamageProof 
        ... 
    }, 
} 

source; https://wiki.multitheftauto.com/wiki/OOP

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...