Hibakeresés: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 33: Line 33:




This is of course just an example, there are plenty of other messages and scenarious, but you should get the idea.
This is of course just an example, there are plenty of other messages and scenarios, but you should get the idea.


==Debug strategies==
==Debug strategies==
There are several strategies that support finding errors, apart from going through the code of course. Most of them include outputting debug messages, with differing information depending on the situtation.
There are several strategies that support finding errors, apart from going through the code of course. Most of them include outputting debug messages, with differing information depending on the situtation.
===Useful functions===
First of all some functions that may come in handy for debugging.
* [[outputDebugString]] or [[outputChatBox]] for outputting any information
* [http://www.lua.org/manual/5.1/manual.html#pdf-tostring tostring()] on a variable to turn it into a string, for example when it contains a boolean value
* [[getElementType]] to check an MTA Element for it's type

Revision as of 00:11, 5 August 2007

While scripting you will often come across problems that are not immediately apparant. This page tries to point out some basic strategies to locate the error.

Debug console

MTA features a built-in debug console that shows debug messages created by MTA or the scripter. You can open it with by typing debugscript x in console, while x is the debug level:

  • 1: only errors
  • 2: errors and warnings
  • 3: errors, warnings and infos

Thus, by typing debugscript 3 all messages are visible, that or level 2 are recommended for most occasions. You should have debugscript enabled most of the time you are testing your script, this will help you to detect typos or other simple to solve errors easily.

Example

For example this snippet has two errors:

if (getClientName(player) == "Fedor")
	outputChatbox("DISGRACE")
end

When the script this piece of code is in is tried to be loaded, debugscript will output something similiar to this:

INFO: INFO: Loading script failed: C:\MTAServers\server8\mods\deathmatch\resources\d\script.lua:15: 'then' expected near ´outputChatbox'

This means the script could not be parsed, because there was a syntax error. It shows the path of the script, so you can also see what resource it is in ('d' in this case) and of course the name of the script. After the filename it shows the line number and again after that what was wrong. Easy to solve now, we just forgot the 'then':

if (getClientName(player) == "Fedor") then
	outputChatbox("DISGRACE")
end

Now the script will load fine and also don't output an error, until a player with the name 'Fedor' enters this section of the script. Then debugscript will output:

ERROR: ERROR: ..rvers\server8\mods\deathmatch\resources\d\script.lua:15: attempt to call global 'outputChatbox' (a nil value)

This means the called function does not exist, which can be easily explained since the functions' name is outputChatBox (capital Box):

if (getClientName(player) == "Fedor") then
	outputChatBox("DISGRACE")
end


This is of course just an example, there are plenty of other messages and scenarios, but you should get the idea.

Debug strategies

There are several strategies that support finding errors, apart from going through the code of course. Most of them include outputting debug messages, with differing information depending on the situtation.

Useful functions

First of all some functions that may come in handy for debugging.