MTA Team qaisjp Posted June 10, 2014 MTA Team Posted June 10, 2014 (edited) 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 September 27, 2014 by Guest
JR10 Posted June 17, 2014 Posted June 17, 2014 It would be better if you could link to the OOP Wikipedia page, it describes OOP in a more detailed way. Nice work!
'LinKin Posted June 19, 2014 Posted June 19, 2014 Oh oh.. OOP in MTA.. Finally, I can keep practicing what I learnt 3 semesters ago After boring structurated programming, OOP is comming. It is a really great improvement for programmers a.k.a scripters
xXMADEXx Posted June 19, 2014 Posted June 19, 2014 Oh oh.. OOP in MTA.. Finally, I can keep practicing what I learnt 3 semesters ago 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.
'LinKin Posted June 19, 2014 Posted June 19, 2014 Wasn't 1.4 coming soon? Then I read something wrong somewhere, lol.
xXMADEXx Posted June 21, 2014 Posted June 21, 2014 (edited) 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 June 23, 2014 by Guest
MTA Team botder Posted June 22, 2014 MTA Team Posted June 22, 2014 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
Castillo Posted June 23, 2014 Posted June 23, 2014 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()
xXMADEXx Posted June 24, 2014 Posted June 24, 2014 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.
Snow-Man Posted June 24, 2014 Posted June 24, 2014 @Made, Player remplace getElementsByType("player") in OOP Lua ?
xXMADEXx Posted June 24, 2014 Posted June 24, 2014 @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.
Snow-Man Posted June 24, 2014 Posted June 24, 2014 @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
MTA Team botder Posted June 24, 2014 MTA Team Posted June 24, 2014 Is there any reason why I can't save data into any element directly (without setData), e.g. player.myvariable = true ?
Arnold-1 Posted June 25, 2014 Posted June 25, 2014 Guys, is there a Wiki page for MTA OOP? https://wiki.multitheftauto.com/wiki/OOP
MTA Team qaisjp Posted June 26, 2014 Author MTA Team Posted June 26, 2014 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!)
diesel974 Posted June 27, 2014 Posted June 27, 2014 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 I believe it should have been : all Peds are Elements Thank you for the tutorial gentleman. Have a nice day!
'LinKin Posted August 9, 2014 Posted August 9, 2014 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);
RottenFlesh Posted August 10, 2014 Posted August 10, 2014 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); 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
'LinKin Posted August 10, 2014 Posted August 10, 2014 Yes I understood. So it's not possible to make what I asked, alright. Thanks for the information.
RottenFlesh Posted August 10, 2014 Posted August 10, 2014 Nonono! It is totally possible, you just have to implement it yourself. Read this: http://lua-users.org/wiki/ObjectOrientationTutorial This is how you can implement OOP by yourself, with classes and inheritance and everything.
Recommended Posts