qaisjp Posted March 8, 2012 Share Posted March 8, 2012 (edited) To make your code easier to understand and modify, I recommend to take a skim through the following guidelines: Recommended coding design When you are coding, it is recommend to indent (by using TAB or four spaces) lines as per the scope, see the example below. local scope1 = math.random(3) local debug = 1 if scope1 == 1 then-- new scope if debug == 1 then outputDebugString("Testing feature 'balls'") if balls then outputDebugString('Balls were ripped') else outputDebugString('Balls survived') end end end Commenting your code If you are releasing code to the community, then commenting the code is important in order for other users to understand your code. This is not particularly necessary on EVERY line, but lines that do not have an obvious outcome should be commented. Another reason of commenting your code is for organizing and separating code into categories/sections. See the example below for how to comment. -------------------------- -- RANDOM COMMANDS -- -------------------------- local trainText = '%s likes trains.' function trainCommand() if type(text) == 'string' then -- check if text var is a string -- Format text to include player name outputChatBox( trainText:format( getPlayerName(source) ), root, 255, 0, 0 ) --[[ outputServerLog( "CMD: "..getPlayerName(source) .. " likes trains." ) ]] end end addCommandHandler('trains', trainCommand) Commenting one line is more suited to putting the double dashes at the start of the sentence, however, if you would like to commenting a chunk of code, you only require to put a --[[ and ]] Organizing your code A shopping market has all that beer in the alcohol section, right? In coding MTA resources, to ensure the code is legible you should put certain components in the right order. Event adding - using addEvent to allow calling of custom event handlers Variables - defining PRESET variables, for example a table of language ID's or options Functions - creating functions Handlers - using addEventHandler, this should be right after the function See the example below: CLIENT: -- EVENTS addEvent('loadSpecialVehicle', true) -- VARIABLES local specialModel = 411 function SpecialVehicle( ) txd = engineLoadTXD ( "data/infernus.txd" ) engineImportTXD ( txd, specialModel ) dff = engineLoadDFF ( "data/infernus.dff", specialModel ) engineReplaceModel ( dff, specialModel ) end addEventHandler ( 'loadSpecialVehicle', root, SpecialVehicle ) DONT GO CRAZY! Don't make variables like reallycoolfeatureenablebooleanvalue for enabling a really cool feature that just is a boolean value. Instead you should use something short and to the point, for example instead of the aforementioned variable name, you could use enableCoolFeature. Scripting logs All debug lines (i.e INFO, WARNING, ERROR) are logged, saved into a specific file depending where the debug line was executed on the client or on the server Server log: \deathmatch\logs\server.log (this may not be the case if you disable or modify this in the conf) Client log: \MTA\clientscript.log Periodically you should clear your log if you are interested in saving disk space FINGER, MY CO E DOESNT WORK If your code isn't working the way you wanted it to, use the /debugscript 3 command, some servers enable this for all users, however on most servers and default servers you are required to be an administrator(*?) There are three types of errors: ERROR WARNING INFO Below are how debug messages are generally structured: ERROR: devil/666.lua:666 attempt to index a nil value Errors and warnings are structed as per above, info messages are just structured with the 'INFO: ' prefix. devil/666.lua explains that the error is in the resource named devil and the file named 666.lua :666 explains that the error is on line 666 after that, the actual error is output. If the event you do NOT get any errors, you should check your code. For example, if the thing you want to do starts with a command. Go to the line containing the command, then go to its function and check each line for errors If your resource still fails to work, you should check your meta.xml file, it is a possibility that your meta.xml has syntax errors or you didn't add your script to the file. If you are tired or fed up of going into the game just to find another error, then a few 3rd party software are available, please note that these links may not parse and show errors in the functions, but only syntax code (missing parenthesis etc.) Here is the list of software: Notepad++ (use this if you are interested in something light, resourceful and supporting of multiple language)(recommended) LuaEdit (use this if you are only interested in editing lua files) MTA Script editor (this was built for mta, has an IDE (integrated development environment) with autocompletion and other fancy features, its slightly outdated and buggy) Where to go from here The best ingame script editor (syntax checking, file editing, resource management and color coding): resedit Multi Theft Auto Wiki: Main Page Scripting Introduction Introduction to Scripting the GUI Debugging If you have any other guidelines for coding or tips don't hesitate to PM me and I will add your advice to this post! (*?) = is this advice correct? Edited March 29, 2012 by Guest Link to comment
qaisjp Posted March 29, 2012 Author Share Posted March 29, 2012 bumpedy bumpedy bumpedy bump. someone please move to tuts section Link to comment
Alpha Posted March 29, 2012 Share Posted March 29, 2012 There should be also advice on which script editor to use, with reasons, etc.. Link to comment
qaisjp Posted March 29, 2012 Author Share Posted March 29, 2012 Appended the above requested to the end of the main post and added a few more links to the aforementioned location. english ftw. Link to comment
Alpha Posted March 29, 2012 Share Posted March 29, 2012 I meant normal scripting editors, IDE, not mta resources. Link to comment
Aibo Posted March 29, 2012 Share Posted March 29, 2012 why in the world its 3 spaces? Link to comment
qaisjp Posted March 29, 2012 Author Share Posted March 29, 2012 why in the world its 3 spaces? *4 -- Alpha, sorry, my mistake. I will update that immediately. Link to comment
TwiX! Posted March 30, 2012 Share Posted March 30, 2012 stolen ideam from russian forum Link to comment
Alpha Posted March 30, 2012 Share Posted March 30, 2012 Not true, anyways, it should contain more information, like advantages and disadvantages. Link to comment
X-SHADOW Posted March 31, 2012 Share Posted March 31, 2012 stolen ideam from russian forum even if it stolen here in english not russian ------------------------------- Thx Man Any Way Keep it Up ! Link to comment
Renkon Posted November 21, 2012 Share Posted November 21, 2012 This is so good. I will bump it so people can read this. Link to comment
Callum Posted November 28, 2012 Share Posted November 28, 2012 Your third example doesn't use local variables, which isn't optimal. Link to comment
Recommended Posts