Jump to content

MTA Lua and OOP


NegativeIQ

Recommended Posts

Hello everyone, 2-3 days ago i started playing with MTA and Lua, and i like it (after samp its not hard :P)

Anyway i noticed that OOP here is a bit strange (at least to me). Usually if some language is OOP it can handle everything in both ways (procedural and oop). And if im right here it is not the case (we cant mix up two in same resource)

So some good examples would be nice, or resources i dont mind as i mostly need to learn syntax (dont send me to wiki, that i already checked).

Also if someone can clarify something about OOP docs.

bool forcePlayerMap ( player thePlayer, bool forceOn )

OOP Syntax

Method: player:forceMap(...)

Variable: .mapForced

Pair: isPlayerMapForced

From above, as i can understand i can use:

player:forceMap(true/false) (as static method call in most oop languages)

player.mapForced (Just directly access variable (set and get) instead of calling forMap set method)

player.isPlayerMapForce (As get method)

Im right or completely wrong? If im wrong please give some examples...

And if someone could provide some examples on how different resources can communicate with each other, if possible in OOP style.

EDIT: Oh and if possible some examples on custom classes and class inheritance in Lua.

Thx in advance

Link to comment

Lua is not an object oriented language, but you can make it so using tables and metamethods.

You can mix up both ways in the same resource, since, again, Lua is not really an OO language.

In Lua, calling a function with class:function is simply syntatic sugar for class.function(self). For example:

myInstance:func(arg1, arg2) 
-- is the same as 
myInstance.func(myInstance, arg1, arg2) 

It's usually the opposite in Lua, you'll mostly use Class.func for static calls and instance:func for instance calls.

Here's a few tutorials about OOP and metamethods:

http://www.lua.org/pil/16.html

http://lua-users.org/wiki/ObjectOrientationTutorial

http://www.tutorialspoint.com/lua/lua_o ... iented.htm

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