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==
For example this snippet has two errors:
For example this snippet has two errors:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
Line 14: Line 15:
end
end
</syntaxhighlight>
</syntaxhighlight>
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'}}
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':
<syntaxhighlight lang="lua">
if (getClientName(player) == "Fedor") then
outputChatbox("DISGRACE")
end
</syntaxhighlight>
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:
{{Debug 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''):
<syntaxhighlight lang="lua">
if (getClientName(player) == "Fedor") then
outputChatBox("DISGRACE")
end
</syntaxhighlight>
This is of course just an example, there are plenty of other messages and scenarious, 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.

Revision as of 23:54, 4 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 ('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':

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.