Jump to content

[HELP] OOP - new methods and properties


Kernell

Recommended Posts

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

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

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