Jump to content

[HELP] OOP Introduction


DriFtyZ

Recommended Posts

Hello MTA SA Community i started this thread so someone who knows how the OOP works can explain it to me aswell because i have some questions and couldn't understand some of the wiki.
I've seen some people using OOP in their resources instead of calling exported functions if i saw correctly. Can i use OOP to replace some exported functions? for example if i have to call a notification resource to show notifications can i use OOP to call that function who will post notifications on the player screen? And second what is the main point to use OOP at all?

Link to comment
  • Discord Moderators

So,
OOP is the shortand of object orientated programming.
Actually, every variable in Lua is an object, because variables are just referencing to a value, and the variables do not actually hold these values, this is why for example, this works:

local = {test = "test", [10] = 10}
print(tbl.test, tbl[10])--> test, 10
local tbl2 = tbl
print(tbl2.test, tbl2[10])--> This will output the same as the another print, but watch this:
--// As i said, variables DO NOT hold values, they are just a reference to an object:
tbl.test = "nottest"
tbl[10] = 12
print(tbl.test, tbl[10])--> nottest, 12
print(tbl2.test, tbl2[10])--> nottest, 12

Run it on repl.it if you want to try it out.
 

So, in OOP every object has its own class, and has its methods, variables.
What is a method?
 

player:getPosition()

Here the player is the object, and getPosition() is the method. (the ':' indicates that its a method)

And to answer on the question: 

6 hours ago, DriFtyZ said:

what is the main point to use OOP at all?

Its more readable than procedural programming 

And its much faster to write(you dont need to write out getElementPosition() and then enter the localPlayer too, but just type localPlayer:getPosition())


Now rethink your question, because it makes no sense at all(the resource OOP part of it)

Edited by Pirulax
Link to comment

okay makes sense it is faster to write, but for people like me who don't know the OOP functions will have to learn them, because i remember it took me months to learn most of the procedural mta functions. Guess it is more advanced even tho it sounds more simple

Link to comment
2 hours ago, DriFtyZ said:

okay makes sense it is faster to write, but for people like me who don't know the OOP functions will have to learn them, because i remember it took me months to learn most of the procedural mta functions. Guess it is more advanced even tho it sounds more simple

The OOP function are very easy to learn.

instead of setElementHealth(element,#)

it would just be element:setHealth(#)

setElementPosition(element,pos) >> element:setPosition(pos)

setVehicleColor(vehicle,color) >> vehicle:setColor(color)

do you see the pattern?

instead of set"Element"Position we just move the element to the front of the function and add a colon Element:setPosition(...)

Since we did this we no longer need to define the element within the function because the function is now directly attacked to that element.

Also most function on the wiki shows their OOP method right under the function.

Another way to look at OOP is every element in the server is a table.

Every element has their own set of function and variables.

Some elements even have the same function like setPosition, setRotation etc...

The pest thing about using OOP for me is that each element has variables

Lets say you want to set the health of a player to 50 the normal way of doing it would be setElementHealth(player,50)

Now in OOP you can use the setHealth method: player:setHealth(50)

OR you can use variables like ".health". While some variables are read only most of them you can change.

so setting the player's health to 50 would be as easy as doing this: player.health = 50

Using player.health would return the player's current health. player.position wourld return rotation etc...

Link to comment
10 hours ago, Mr.Loki said:

Now in OOP you can use the setHealth method: player:setHealth(50)

Fair enough you answered almost all my questions, but there is one more. if you player:setHealth(50) that "player" needs to be defined right?
So if the player is defined somehow either from source or from a variable like local driftyz = getLocalPlayer() can i use driftyz:setHealth(50)? Thanks in advance :)

Link to comment
  • Discord Moderators
16 hours ago, Mr.Loki said:

player.position wourld return rotation etc...

You mean position.
Yeah, so he said the same.
Its just much faster to write something in OOP than in PP, and its even easier to read. (In case you dont use spaces everywhere, because that makes no sense, and you follow the Lua style guide.)
An own set of functions, variables, and such are called a class.

But to answer your question:
As I said, variables are just memory references, they are basically pointers, that point to a memory block, whatever.

So basically, its the same like using

 getLocalPlayer():getHealth()

So it really doesnt matter if the pointer to the mem. block is in the 'source' variable, or 'driftyz' variable, it means really nothing to the Lua VM.
Yes, the player needs to be defined, and it needs to be an element that has the METHOD you want to call.
Otherwise, if its nil Lua would throw an error like:
"Attempt to index a nil value" 
Or if the method in that class doesnt exists(for ex.: you want to call setPosition() on a texture element)
"Attempt to call a method 'setPosition' (something goes here, but idk what)"
 And so on.

Edited by Pirulax
  • Thanks 1
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...