qaisjp

{wikitut} OOP Introduction

Recommended Posts

Hey, I've created a sweet introduction into using the object orientated features of MTA coming soon in MTA 1.4. I hope you enjoy it :)

If you've contributed by editing and tweaking the wiki page, if you've benefited from this tutorial, or if you have anything to say; please give me feedback on here :)

Enjoy.

Edited by Guest

Share this post


Link to post

It would be better if you could link to the OOP Wikipedia page, it describes OOP in a more detailed way. Nice work!

Share this post


Link to post

Oh oh.. OOP in MTA..

Finally, I can keep practicing what I learnt 3 semesters ago :D

After boring structurated programming, OOP is comming. It is a really great improvement for programmers a.k.a scripters

Share this post


Link to post
Oh oh.. OOP in MTA..

Finally, I can keep practicing what I learnt 3 semesters ago :D

After boring structurated programming, OOP is comming. It is a really great improvement for programmers a.k.a scripters

Yes, but unfortunately it won't be here for a while. It's scheduled to be here in 1.4.

Share this post


Link to post

Wasn't 1.4 coming soon?

Then I read something wrong somewhere, lol.

Share this post


Link to post
Wasn't 1.4 coming soon?

Then I read something wrong somewhere, lol.

Well of course adventively it'll be here, but it will take time.

Edit:

The MTA 1.4 beta release was released on June 21st

Edited by Guest

Share this post


Link to post

I dont know if I used the OOP as it is right now properly, but the class Connection doesn't seem to work as it should :)

Share this post


Link to post

I believe there's a small typo in that page:

local vehicle = createVehicle(411, 0, 0, 3) 
vehicle:setDamageProof(vehicle) -- Here 
vehicle:setFrozen(vehicle) -- Here 
vehicle:setHealth(1000) 
vehicle:setVelocity(0.2, 0.2, 0.2) 
vehicle:destroy() 

Share this post


Link to post
what the different between Lua and OOP ?

OOP is Lua, it's just introducing classes and that kind of stuff.

Here's an example of the differences:

Non-OOP Lua

function giveRandMoney ( ) 
    local p = getRandomPlayer ( ) 
    givePlayerMoney ( p, 100 ) 
end 

OOP Lua (I'm guessing giveMoney is in the player class, but im not sure)

function giveRandMoney ( ) 
    local p = Player.getRandom ( ) 
    p:giveMoney ( 1000 ) 
end 

It's somewhat just how the scripter wants to write, but if they program in any other languages then they're going to want to do OOP because most programming languages such as C, C++, PHP, etc... are all OOP.

Share this post


Link to post
@Made, Player remplace getElementsByType("player") in OOP Lua ?

No, it's a class of functions (or a table as known in Lua)

For example, you could write this:

for i, v in pairs ( Player ) do 
    outputChatBox ( tostring ( i ).." -> "..tostring ( v ) ) 
end 

To output all of the functions in the Player class/table.

Share this post


Link to post
@Made, Player remplace getElementsByType("player") in OOP Lua ?

No, it's a class of functions (or a table as known in Lua)

For example, you could write this:

for i, v in pairs ( Player ) do 
    outputChatBox ( tostring ( i ).." -> "..tostring ( v ) ) 
end 

To output all of the functions in the Player class/table.

thanks :)

Share this post


Link to post

Is there any reason why I can't save data into any element directly (without setData), e.g. player.myvariable = true ?

Share this post


Link to post

Thanks guys!

Guys, is there a Wiki page for MTA OOP?

I'm not quite sure what to put on the page, but I'll link some extra useful links on there.

Is there any reason why I can't save data into any element directly (without setData), e.g. player.myvariable = true ?

You may want to try this, but I'm going to try to convince the devs to make this possible. However, it wouldn't be applied to element data - something like player.data["myvar"]=value may be done instead.

I believe there's a small typo in that page:
local vehicle = createVehicle(411, 0, 0, 3) 
vehicle:setDamageProof(vehicle) -- Here 
vehicle:setFrozen(vehicle) -- Here 
vehicle:setHealth(1000) 
vehicle:setVelocity(0.2, 0.2, 0.2) 
vehicle:destroy() 

Ah! Thanks. The procedural code was wrong too (I missed out the boolean values for the functions, so this is probably why the OOP counterpart was incorrect too!)

Share this post


Link to post

A small typo :

There are children of classes, for example, with players, the system goes like: Element -> Ped -> Player". All Players are Peds and all Peds are Players. Not all Peds are Players, and certainly not all Elements are Players.

all Peds are Players. Not all Peds are Players :shock:

I believe it should have been : all Peds are Elements :D

Thank you for the tutorial gentleman. Have a nice day! :mrgreen:

Share this post


Link to post

Is it possible to create a bean?

- Like a class having some attributes; Example:

class Player{ 
    private nick; 
    private kills; 
    private cash; 
} 

and then being able to do something like aPlayer.setNick(hisNick);

:mrgreen:

Share this post


Link to post

Is it possible to create a bean?

- Like a class having some attributes; Example:

class Player{ 
    private nick; 
    private kills; 
    private cash; 
} 

and then being able to do something like aPlayer.setNick(hisNick);

:mrgreen:

If you have a player variable, you can do something like this:

function command(player) 
    player.name = "something" 
    outputChatBox(player.name) 
     
    player.money = 100 
end 
addCommandHandler("blah", command) 

But they are predefined by MTA, you can not create your own, unless you make your own implementation of OOP in lua using tables and metatables.

We are slowly documenting the OOP syntax in wiki. i've already documented almost every server-side player functions by myself.

https://wiki.multitheftauto.com/wiki/Server_Scripting_Functions#Player_functions

There, you will see something like this:

[b]OOP Syntax[/b] 
[b]Method[/b]: player:getName() 
[b]Variable:[/b] .name 
[b]Pair:[/b] setPlayerName 

It means you can do things like this:

function some_random_function(player) 
    local name = player:getName() -- element -> "player"; method -> ":getName()" 
    local name = player.name -- ".name" is just short for getPlayerName() 
    -- And the pair function is used when you have a variable like the one above and you set a value to it. 
    player.name = "something" -- in this case you are not calling getPlayerName(), but setPlayerName() is used instead.  
end 

There is also methods and variables that are declared under some object Class.

Like this:

function some_other_random_function() 
    Player.getRandom() -- "Player", is a class that contains all player methods and variables, and ".getRandom()" is one of those methods. 
    Player.random -- This is just a short form of the above. Both valid. 
end 

I hope you understand what i said, English is not my native language :)

Share this post


Link to post

Yes I understood.

So it's not possible to make what I asked, alright.

Thanks for the information.

Share this post


Link to post

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.