Hibakeresés: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 8: Line 8:
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.
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==
===Example===
For example this snippet has two errors:
For example this snippet has two errors:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
Line 17: Line 17:
When the script this piece of code is in is tried to be loaded, debugscript will output something similiar to this:
When the script this piece of code is in is tried to be loaded, debugscript will output something similiar to this:
{{Debug info|INFO: Loading script failed: C:\MTAServers\server8\mods\deathmatch\resources\d\script.lua:15: 'then' expected near ´outputChatbox'}}
{{Debug 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 ('d' in this case) it is in and of course the name of the script. Behind the filename it shows the line number and again after that what was wrong. Easy to solve now, we just forgot the 'then':
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':
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
if (getClientName(player) == "Fedor") then
if (getClientName(player) == "Fedor") then
Line 31: Line 31:
end
end
</syntaxhighlight>
</syntaxhighlight>


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 scenarious, but you should get the idea.

Revision as of 00:01, 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 scenarious, 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.