Jump to content

[advice]PROPER Coding Guidelines


qaisjp

Recommended Posts

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! :P

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

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:

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 by Guest
Link to comment
  • 3 weeks later...
  • 7 months later...
  • 2 months later...
  • Recently Browsing   0 members

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