OOP

From Multi Theft Auto: Wiki
Revision as of 22:35, 9 December 2014 by Qaisjp (talk | contribs) (→‎OOP Metatable Structure: fix typo)
Jump to navigation Jump to search

Object Orientated Programming is introduced in MTA:SA 1.4 and comes with special utility classes like Vector and Matrix. This page contains general information about the OOP functions and provides useful links.

Turning it on

By default, OOP is disabled (however, vectors and matrices are always available) - this is mainly because the vast majority of servers will prefer to stick to what they know - procedural programming. In fact, functions are still available even when OOP is enabled. Enabling OOP is as simple as adding the following line to the resource meta file:

<oop>true</oop>

Vectors and Matrices

Vectors and Matrices make it easier to drop the complex maths and go straight ahead with fun part of maths. As mentioned above, OOP does not have to be enabled in the server config for this to be enabled.

OOP Metatable Structure

You will understand this if you're proficient with Lua and have a decent understanding of metatables.

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

Things to do

  • Document all the existing functions in OOP form on the documentation

Useful Links