Jump to content

Make class with methods in it?


sanyisasha

Recommended Posts

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

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

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 by Saml1er
Link to comment
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

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
  • 1 month later...
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 :D

<!-- 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 by sanyisasha
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...