Jump to content

[HELP] OOP - new methods and properties


Kernell

Recommended Posts

Posted

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?

Posted

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.

Posted

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.

Posted

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.

Posted

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 

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...