Jump to content

OOP


Piorun

Recommended Posts

Just make the function part of the class, like this:

function Player:isCool() 
     return true 
end 
  
localPlayer:isCool() 

Alternatively use the following method, which also works with custom elements:

local element = createElement("myElementType", "yolo") 
local myElementTypeClass = getmetatable(element).__class 
function myElementTypeClass:testFunc() 
     outputChatBox("my Element type id: "..getElementID(self)) 
end 
      
element:testFunc() 

If you however dont have an element to use for getmetatable to get the class metatable, like if you are doing it server side then relying on getRandomPlayer() would be stupid, so in that case you can do this:

local registry = debug.getregistry() 
local playerMetatable = registry.mt.Player 
local playerClass = playerMetatable.__class 
function playerClass:isCool() 
     return true 
end 

If you want to store values for elements, and then also get/set them by doing element.key = value and value = element.key, then you can do the following:

local registry = debug.getregistry() 
local playerMetatable = registry.mt.Player 
local playerData = {} 
  
local setFunc = playerMetatable.__set 
local getFunc = playerMetatable.__get 
  
function setFunc:cool(value) 
    if(not playerData[self])then 
        playerData[self] = {} 
    end 
    playerData[self].cool = value 
end 
  
function getFunc:cool() 
    if(not playerData[self])then 
        return nil 
    end 
    return playerData[self].cool 
end 
  
localPlayer.cool = true 
  
function Player:isCool() 
    return self.cool 
end 
      
outputChatBox("localPlayer is cool: "..tostring(localPlayer:isCool())) 

Note: The functions you add to classes will only be available in the resource in which they were defined in.

Link to comment

Ok, fine. I have that code:

test.lua (it's server-side):

local class = debug.getregistry().mt 
local getter = class.Player.__get 
local setter = class.Player.__set 
local data = {} 
  
function setter:logged(val) 
    if not data[self] then 
        data[self] = {} 
    end 
    data[self].logged = val 
end 
  
function getter:logged() 
    if not data[self] then 
        return nil 
    end 
        return data[self].logged 
end 
  
addCommandHandler("oop", 
    function(player) 
        player.logged = "Piorun" 
        outputChatBox(player.logged) 
    end 
) 

It works cause i have command to test so its cool and my meta for that file:

    

Am I doing it good? If not pls correct.

Link to comment

You cannot do what you're trying to. You cannot export functions within tables. You must define the functions in every resource you need them in. No Lua data is ever shared between Lua virtual machines.

Also, I would stop right now and use an intermediate library for classes (like sbx320's classlib) instead of writing that mess in multiple files. This, however (and obviously), does not make it possible to share methods from classes across resources.

Link to comment

The class library can be used in both client and server, logically.

If I remember correctly, adding a member variable to any MTA class is as simple as adding one to a "normal" table:

Player.foo = 3 
  
function Player:bar () 
  
    print ( self, "bar" ) 
  
end 

Link to comment

Doesnt work:

Player.test = "Piorun" 
  
addCommandHandler("oop", 
    function(player) 
        outputChatBox(player.test) 
    end 
) 

Edit.

Anyway why MTA's devs let Lua devs use OOP in MTA if we can't export methods and variables through other resources added to existing Class ? :/

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