Piorun Posted January 21, 2016 Share Posted January 21, 2016 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
tosfera Posted January 22, 2016 Share Posted January 22, 2016 set the __parent towards your other class in the metatable. Link to comment
Piorun Posted January 22, 2016 Author Share Posted January 22, 2016 Can i ask you about some example? And did you test it before? Link to comment
tosfera Posted January 23, 2016 Share Posted January 23, 2016 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
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