Scripting Tips

From Multi Theft Auto: Wiki
Revision as of 21:23, 31 July 2014 by Arran Fortuna (talk | contribs)
Jump to navigation Jump to search

This page contains a variety of things that knowing, make life easier for MTA scripters.

General Lua

  • The infinite loop / too long execution error which aborts execution can be disabled with debug.sethook(nil)
  • Be careful when looping a table where you intend to delete multiple rows, if 2 of them are in a row the 2nd one will get skipped! You must loop the table backwards (reverse ipairs). For example: for i = #table, 1, -1 do
  • Rather than having if checks inside if checks inside if checks, consider using return for example if (not ready) then return false end

MTA Scripting

  • If your script is covered in isTimer and isElement checks to hide debug warnings from deleted elements not being dereferenced (making the variable nil) you will regret it when that element ID or timer pointer has to be re-used by MTA in a weeks time and your script starts acting strangely and you won't have a clue why. Dereference destroyed elements and disconnected players!
  • Remember that 99% of the time it's a bug in your script, not an MTA bug! Don't report something to the mantis until you're absolutely certain the bug can be reproduced with a small piece of script.
  • As MTA already has so many functions and events virtually everything you want to do is already possible, as long as you're willing to do the work! You'll find better solutions to problems as there are many ways to achieve the same thing as long as you know all the functions and events.
  • If a script is getting too complicated, try putting back-end stuff in another file so the main script calls the functions in the 2nd one. For example rather than having meaningless things like vehicleTable[vehicle][7] in the main file, put that in a function in the 2nd file and have the main file call a meaningfully named function so rather than seeing a useless 7 you'd see something like getFuel and although this might take longer to set-up you'll save time in the long run as you'll spend less time being confused when you come back to it in a weeks time to debug a problem.
  • Your client side scripts can cause desync if you're not careful, try to keep the psychical world equal with every player. For example if your script creates a client side object make sure your script will create it for everyone nearby, else people will be wondering why a player appears to be constantly floating and falling.

Server Performance

  • Server lagging? Check this page of debugging performance issues
  • Using resourceRoot in event handlers for events from clients is much more efficient than using root.
  • Try to be efficient, but if what you're doing is too time consuming or complex, is it really efficient?
  • It's much more efficient to set player health client side, consider a client event all your server scripts can call to set a players health.
  • Unless you have hundreds of players, don't worry about making little optimizations, check performancebrowser or ipb (ingame performancebrowser) and make sure no resource is using significantly more than the others.

Client Performance

  • The biggest cause of client script CPU usage is anything done in onClient/Pre/Hud/Render because it is called so often. For example if you have a script which calls dxDrawLine3D 20 times, 60 times a second, if those lines are only in 1 part of the map, consider adding a getDistanceBetweenPoints3D check between the local player and the general area that those lines are in and if they're no where near the player, don't draw the lines.