OOP: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (Added tuts)
(17 intermediate revisions by 6 users not shown)
Line 1: Line 1:
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.
Object Orientated Programming was 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 ==
== Turning it on ==
You must set the <oop> key in the server configuration file from false to true to enable OOP in your server. However, Vectors and Matrices will always work regardless of OOP being enabled.
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:
<syntaxhighlight lang="xml"><oop>true</oop></syntaxhighlight>


== Vectors and Matrices ==
== Vectors and Matrices ==
[[Vector|Vectors]] and [[Matrix|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.
[[Vector|Vectors]] and [[Matrix|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==
==ADVANCED: OOP Metatable Structure==
You will undersand this if you're an advanced scripter with an understanding in metatables.
You will understand this if you're proficient with Lua and have a decent understanding of metatables. Understanding this section is not necessary to use OOP.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- Exposed to global environment
-- Exposed to global environment
Line 58: Line 59:
</syntaxhighlight>
</syntaxhighlight>


== Things to do ==
== Useful Links ==
* Document all the existing functions in OOP form on the documentation
* '''[[OOP Introduction]]''' - teaches you about the basics of OOP
* '''[[OOP_client|Function list (client)]]''' and '''[[OOP_server|Function list (server)]]''' - a list of functions implemented


== Useful tutorials ==
[[Category:OOP]]
This list may not contain all the OOP related tutorials, for a full list, check [[Tutorials]].
* '''[[OOP Introduction]]''' - teaches you about the basics of OOP

Revision as of 18:40, 7 August 2016

Object Orientated Programming was 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.

ADVANCED: OOP Metatable Structure

You will understand this if you're proficient with Lua and have a decent understanding of metatables. Understanding this section is not necessary to use OOP.

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

Useful Links