Kernell Posted August 20, 2014 Share Posted August 20, 2014 I'm trying to define custom methods and properties of existing classes. I do not like to use the variable names with a lowercase letter (I like CamelCase style). In this article (https://wiki.multitheftauto.com/wiki/OOP) described what properties MTA creates for metatables. I tried to define their own properties, but this do not work: local ElementMT = debug.getmetatable( Element ); Element.__get = -- It's funny that __get table does not exist. { Health = getElementHealth; }; debug.setmetatable( Element, ElementMT ); local pElement = Element( "test" ); pElement.health = 13; -- for testing outputDebugString( tostring( pElement.Health ) ); -- nil (do not work) How to make custom properties and methods? Link to comment
ixjf Posted August 20, 2014 Share Posted August 20, 2014 Take a look at sbx320's classlib. Link to comment
Kernell Posted August 20, 2014 Author Share Posted August 20, 2014 No, thanks. I have my own library, with the ability to define custom properties like as C#. I'm interested in the native way of the MTA, without workarounds. Link to comment
ixjf Posted August 20, 2014 Share Posted August 20, 2014 I'm not telling you to use that library, I mentioned it so you can see how to do it the native way. That's just the way it has to go due to the MTA OOP design. Link to comment
Kernell Posted August 20, 2014 Author Share Posted August 20, 2014 I do not see that you use the native OOP from the MTA. I did not even see that you use the override methods and properties as required in the first message. Link to comment
Jusonex Posted August 20, 2014 Share Posted August 20, 2014 Class information are stored in the Lua registry internally. There are two ways to access __get/__set: 1.) Use the userdata (element) instead of the class: debug.getmetatable(element) 2.) Use the registry local classInfo = debug.getregistry().mt local getFunctions = classInfo.Account.__get Link to comment
Kernell Posted August 20, 2014 Author Share Posted August 20, 2014 Did not know about Lua registry. Thank you very much, it really helped me! Issue has been resolved 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