sanyisasha Posted September 1, 2018 Share Posted September 1, 2018 Hi. Somehow possible to make a custom class (like Vehicle) and add custom function to it (e.g. Vehicle:create(...)) I want to create a class based system, where i make base classes (e.g User), then make custom functions (e.g User:setMoney(100)) My main problem is, as i saw in mta it's not possible to export function as variable/element data value. I don't want to make it like: exports.resource:User_setMoney(UserClass, 100), but it should called like before, User:setMoney(100), because that's why i want to make the system: make mta programing easier with a cool framework (Like in PHP -> Yii2, Laravel) Any possibility to make it work? Thanks, SaSha Link to comment
Mr.Loki Posted September 1, 2018 Share Posted September 1, 2018 Simple class template: -- Create Class local Class = {} Class.__index = Class function Class.new() local self = setmetatable({ },Class) return self end setmetatable(Class, {__call = function( _,... ) return Class.new(...) end }) -- Add method to class function Class:doStuff(stuff) self.var = stuff end Link to comment
sanyisasha Posted September 1, 2018 Author Share Posted September 1, 2018 That's okey. But! If i'll send this to another script, i'll get 'error': Class:doStuff value will equal to nill! That's my problem, not the OOP Link to comment
Saml1er Posted September 1, 2018 Share Posted September 1, 2018 (edited) You can use: https://raw.githubusercontent.com/LuaDist/classlib/master/unclasslib.lua The usage is pretty simple. In this example, NamedAccount class inherits methods from Account class, it works because it supports multiple inheritance. If you don't want to inherit from other classes ,you should call class(). Account = class() function Account:__init(initial) -- constructor self.balance = initial or 0 end function Account:deposit(amount) self.balance = self.balance + amount end function Account:getBalance() return self.balance end myAccount = Account(10.00) NamedAccount = class(Account) function NamedAccount:__init(name, initial) self[Account]:__init(initial) self.name = name or 'anonymous' end function NamedAccount:getName() return self.name end myNamedAccount = NamedAccount('John', 10.00) myNamedAccount:deposit(60) outputChatBox ("Balance: "..tostring ( myNamedAccount:getBalance() ).." | Name: "..myNamedAccount:getName()) Edited September 1, 2018 by Saml1er Link to comment
Tails Posted September 1, 2018 Share Posted September 1, 2018 (edited) @sanyisasha, check out bakagaijin: http://anirudh.katoch.me/project/bakagaijin/ I think this is as close you will get to interacting with other scripts in an object oriented way. I've used it before in one of my projects and it worked pretty well. Edited September 1, 2018 by Tails 1 Link to comment
Simple0x47 Posted September 2, 2018 Share Posted September 2, 2018 If you want to create a gamemode full OOP, don't try it due to lack of optimization in the methods used for the creation of the OOP style. Better take C++ and write as much as possible with it for the server side. 1 Link to comment
sanyisasha Posted September 2, 2018 Author Share Posted September 2, 2018 15 hours ago, Saml1er said: You can use: https://raw.githubusercontent.com/LuaDist/classlib/master/unclasslib.lua The usage is pretty simple. In this example, NamedAccount class inherits methods from Account class, it works because it supports multiple inheritance. If you don't want to inherit from other classes ,you should call class(). Account = class() function Account:__init(initial) -- constructor self.balance = initial or 0 end function Account:deposit(amount) self.balance = self.balance + amount end function Account:getBalance() return self.balance end myAccount = Account(10.00) NamedAccount = class(Account) function NamedAccount:__init(name, initial) self[Account]:__init(initial) self.name = name or 'anonymous' end function NamedAccount:getName() return self.name end myNamedAccount = NamedAccount('John', 10.00) myNamedAccount:deposit(60) outputChatBox ("Balance: "..tostring ( myNamedAccount:getBalance() ).." | Name: "..myNamedAccount:getName()) That's okey. But. How i'll separate these in two lua files? Example: models/user.s.lua <-- Create class named User app.s.lua <-- instantiates User class from models/user.s.lua My solution was every model had a function called get<ModelName>Model, what return the needed class. It worked well, if i had a User.name, i was able to print it. But if i tryed to call User.setname(newname), it writed the setname function is nill. Link to comment
sanyisasha Posted September 2, 2018 Author Share Posted September 2, 2018 I found a not best, but looks like working solution. I made a bridge shared file. Created a shared variable: Class_test -- user.s.lua test = class() function test:__init(initial) -- constructor self.balance = initial or 0 end function test:deposit(amount) self.balance = self.balance + amount end function test:getBalance() return self.balance end Class_test = test In user.s.lua, in the last row writed this: Class_test = test In app.s.lua, writed this: local t = Class_test(10) Then: outputDebugString(t:getBalance()) <- in console: 10. Thanks everyone!! Link to comment
Saml1er Posted September 2, 2018 Share Posted September 2, 2018 can you post the PHP equivalent code in OOP? I can show you how you can convert it to Lua. @sanyisasha Link to comment
sanyisasha Posted October 3, 2018 Author Share Posted October 3, 2018 (edited) On 02/09/2018 at 22:25, Saml1er said: can you post the PHP equivalent code in OOP? I can show you how you can convert it to Lua. @sanyisasha Not that's the problem. Simple: //xy.php Class test extends test2 { public $var; public function __construct($varVal) { $this->var = $varVal; } } //yx.php somewhere else require 'xy.php'; $testClass = new test(1); echo $testClass->var; Btw my final solution: Own package manager <!-- meta.raw.xml | Package manager generate meta.xml from this --> <test> <info author="SaSha" /> <package name="utils" /> <package name="user" /> <script src="test.lua" type="client" /> </test> <!-- Generated meta.xml --> <test> <info author="SaSha"></info> <script src="packages/utils/unclasslib.class.lua" version="1.0" type="shared"></script> <file src="packages/user/assets/24.jpg"></file> <script src="packages/user/User.class.lua" version="0.0.1" type="shared"></script> <script type="client" src="test.lua"></script> </test> And i can access int test.lua what for example User.class.lua contains. That's good because when i write a resource, for example a loginpanel, simple write needed packages in the meta.raw.xml and in the server: /package test install, and the package manager generate meta.xml, copy scripts, folders and everytihing or if it's exists, replace it with the new one. (Later with version check) Edited October 3, 2018 by sanyisasha 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