Piorun Posted March 4, 2016 Share Posted March 4, 2016 Hi, Can anyone explain me this: https://wiki.multitheftauto.com/wiki/OO ... _Structure (this metatable structure)? I know OOP in other languages like Java, PHP etc. so i'm not 'oop noob'. Anyway how I can use this knowledge to create my own Lua classes in MTA? Link to comment
vx89 Posted March 5, 2016 Share Posted March 5, 2016 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
Piorun Posted March 5, 2016 Author Share Posted March 5, 2016 I can use something like "CLuaClassDefs::Index" in Lua or only in C++? Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now