Jump to content

{wikitut} OOP Introduction


qaisjp

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
Link to comment
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.

Link to comment

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() 

Link to comment
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.

Link to comment
@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.

Link to comment
@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 :)

Link to comment

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!)

Link to comment

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:

Link to comment
  • 1 month later...

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 :)

Link to comment
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...