Jump to content

Object Oriented Programming paradigm in MTA


Pi.R

Recommended Posts

Hello, 

I want to design my gamemode in an Object-Oriented way. To do so, I have created a resources called Classes where each file maps a Class definition. 

To give you an insight of my code, here's the code of my Database.lua file which defines a set of rules and properties for SQL management. 

This guide has inspired my code : https://www.lua.org/pil/16.html

Spoiler

-- A Database class use to interact with the sql file or the mysql server

-- Properties
Database = {

    db_file_path = nil

}

-- Constructor
function Database:new( obj )

    obj = obj or {}
    setmetatable( obj, self )
    self.__index = self
    return obj

end

-- Create a handler
-- @Params : None
-- @Results : A flag and a message
function Database:connect()

    self.handler = dbConnect( "sqlite", self.db_file_path )

    if ( self.handler == nil ) then

        return 1, "Couldn't connect to the database <" .. self.db_file_path ..">"

    else

        return 0, "Connected to the database <" .. self.db_file_path ..">"

    end

end

-- Execute a sql query
-- @Params : A sql query and its binding
-- @Results : A flag and a message
function Database:execute( query, ... )

    local query = dbQuery( self.handler, query, unpack( arg ) )

    if ( query == false ) then

        return 1, "Database handler is not correctly defined."

    else

        local result, num_affected_rows, last_insert_id

        repeat

            result, num_affected_rows, last_insert_id = dbPoll( query, -1 )

        until ( result ~= nil )

        if ( result == false ) then

            local error_code, error_msg = num_affected_rows, last_insert_id
            return 1, "Failure. Code : " .. to_string( error_code ) .." <" .. to_string( error_msg ) .. ">"

        else

            return 0, "Success."

        end

    end

end

local db = Database:new{ ":db/dev.sql" }

-- Returns the database Object
-- @Params: None
-- @Return : a database object
function _db()

    return db

end

 

In my meta.xml, I have exported the _db function. 

Now, in another resource, here's how I use my db object.

Spoiler

addEventHandler( "onResourceStart", getRootElement(),

	function()

		local db = exports.Classes:_db()
		outputServerLog( db.db_file_path )

	end

)

 

However, I have an error telling me that db_file_path is not defined (equal to nil). 

Therefore, I wonder how I can call an object instantiated in a particular resource FROM another resource.

Thanks for you help!

Link to comment

Don't, because if oop is enabled in meta.xml file in Class resource, export won't work. However I've noticed this part in Class resource's script:

 

-- Properties
Database = {

    db_file_path = nil -- <- This

}

I suppose this is the reason why it's nil. Try updating it at function/place where you're creating the database at.

Link to comment

Try checking if db even exists.

I believe that there's an error in Database:new function

You've coded Database:new function in a way that it expects 'obj' to be a table, but instead, you're sending filepath of database which is probably why it doesn't even create the db at all.

local db = Database:new{ ":db/dev.sql" } -- This is part where you're sending a filepath instead of a table to Database:new function

Link to comment
  • MTA Team

Metatables cannot be shared across resources right now. If you pass a table with a metatable across a resource boundary, the metatable is removed. There are basically two options to solve this.

A) Move the database class into your resource

B) Construct a wrapper class around exports which forwards all metatable calls to the database resource.

Link to comment

I guess another option is to use getElementData and setElementData

I have created a dummy object and assigned it as an ElementData my db object (not the class, but the object), then I can call whatever function on it from another resources.

However it's not really elegant.

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