Pi.R Posted June 5, 2016 Share Posted June 5, 2016 Hello, I'm currently working on "designing" my future server, and I would like to get some tips / advices on how to organise my resources? Since I have began coding on MTA and in LUA, my code was always a mess up and I was struggling to make it readable and understandable. What are you coding habits? How to you separate your functions / resources? Cheers Link to comment
Simple0x47 Posted June 6, 2016 Share Posted June 6, 2016 One of the most important things of the scripting is the organiation. I would recommend you to use metatables and tables for a more readable script, it's just a way of OOP. The separation of resources depends on you, Personally I like to separe the resources by their functions. Link to comment
Anubhav Posted June 9, 2016 Share Posted June 9, 2016 One of the most important things of the scripting is the organiation. I would recommend you to use metatables and tables for a more readable script, it's just a way of OOP. The separation of resources depends on you, Personally I like to separe the resources by their functions. organisation* seperate* Indentation (4 spaces is best) is how I really make my code look clean. Link to comment
Noki Posted June 14, 2016 Share Posted June 14, 2016 Anubhav, 'organisation' is how it's spelled in Australia, Britain and New Zealand. And Simple01 was correct - it is 'separate' (separation), not 'seperate'. Anyway, Dealman made a very nice guide on keeping your code clean. Although I don't agree with 100% of it (everyone has different coding preferences, which is perfectly okay), it is a very good guide on the principles of setting out code nicely. For resources, I would recommend using carpets, which are folders denoted by square brackets, like "[private]" or "[admin]" to organise resources. MTA does this in their default resources and it works quite nicely. Maps are separated from the admin panel and runcode. Which makes perfect sense. Put all resources that fall within the scope of the carpet name in the carpet. So don't put the resource with all your mods in "[admin]" for example. Anubhav brought up indentation, which is a great point. Indentation helps you organise your code, especially if it is long and has lots of nests. I use tabs to indent my code. . At the end of the day, that's personal preference. Indents should be done on a new line after a control statement (if, for, do, while). Spacing is also relatively important in regards to the readability of code. Some like to put spaces between 'if' and the bracket that follows, some don't. Some like to put spaces between every bracket, some don't. Some space their arguments, some don't. Personal preference yet again. Although I think you should space properly like you do in the English language (eg: 1, 2, 3). Callum, an MTA contributor, doesn't put any spaces between his arguments (eg: 1,2,3). mabako's spacing scheme in mta-paradise: As for how you organise files within a resource, that's tricky. It's good to put your images in a sub folder (:resourceName/images/), similar to how web developers do so with '/src/' and others of the like. I like to have two files in small scripts - 'server.lua' and 'client.lua'. I write all of my server-sided code in one, and all of my client-sided code in the other. For bigger resources, I tend to prefix my files with their type ('c_' and 's_') followed by their purpose or what they contain (eg: 'c_utils.lua' and 's_sync.lua'). And as Simple01 said, OOP is the way to go. I personally think 'player.name' is much easier to read than 'getPlayerName(player)'. Less typing and more elegant code for the same result? Yes please! Metatables are another thing and can make things overly complicated at times, but also very simple at other times. Try them out for yourself. Link to comment
Anubhav Posted June 14, 2016 Share Posted June 14, 2016 That helped me a lot. Thanks for the effort. What about metatables? What's the use of them? Link to comment
Discord Moderators AlexTMjugador Posted June 15, 2016 Discord Moderators Share Posted June 15, 2016 Metatables are just tables with special fields. When they are assigned to another variable by using the setmetatable function, those special key-value pairs modify how does Lua interact with the original variable. In pure Lua it is only possible to modify metatables of tables, however. If you look at it without paying too much attention, metatables seem like advanced stuff which is there to complicate things and not provide any new possibilities. But you should look again at it: metatables are the only way we have in Lua to achieve proper Object Oriented Programming, and only that alone should be an important enough motivation to learn what metatables are. Besides OOP, metatables are also useful for intercepting table reads and writes (the setTableProtected useful function is possible thanks to them, and the exports.resource:function(...) syntax too), making things clear for big scripts, when hiding away details of some implementation is preferred, debugging... Sure, metatables are something you won't need to use everyday, but in the long term they become a necessity because you will want to tidy and clean up your code. There is more information about metatables available here, in addition to a list of special fields a metatable can have. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now