Pi.R Posted September 21, 2016 Share Posted September 21, 2016 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
RizoN Posted September 21, 2016 Share Posted September 21, 2016 (edited) In case you've enabled oop in meta.xml file, exports won't work (unless the resource in which export function is in doesn't have oop enabled in it's meta.xml file). Edited September 21, 2016 by RizoN Link to comment
Pi.R Posted September 21, 2016 Author Share Posted September 21, 2016 I haven't enabled anything, should I? Link to comment
RizoN Posted September 21, 2016 Share Posted September 21, 2016 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
Pi.R Posted September 21, 2016 Author Share Posted September 21, 2016 I'm pretty sure that's the issue because I've tried setting db_file_path to "Hello" or to 1 and the result is the same : it seems that there are no db_file_path as an index. Link to comment
RizoN Posted September 21, 2016 Share Posted September 21, 2016 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 sbx320 Posted September 22, 2016 MTA Team Share Posted September 22, 2016 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
Pi.R Posted September 22, 2016 Author Share Posted September 22, 2016 Thank your for your enlightment. I think I'm going to go with option B. Woud you mind giving me deeper explications on how to do it? Cheers! Link to comment
Pi.R Posted September 22, 2016 Author Share Posted September 22, 2016 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
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