AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
|
10-31-2014
, 12:38
[HOW TO] Debug your plugins
|
#1
|
In my opinion this tutorial should not be written, but I am tired of "coders" who ask stupid questions, which may find the answer through some simple debug messages, and when ou tell them to do that they ignore you or say that they have no clue what that means. I have faced such situations when someone asked me why his code does not work. I found the mistake, it was obvious, but he may find the problem on his account if he would examine the code, of course the situation above described above happened, so I decided to write this tutorial.
What mean to debug your plugin ?
By debugging you find the probable errors, you find the cause, or lower the number of possible cases where the problem might occur. This way you know exactly what to fix and what does not work. Also, you will see the evolution of your own code and you can detect its weaknesses, possible mistakes that you did or places where your logic did not work and the code may fail.
This sounds nice, I know, but how you do that ? It's a very complicated thing, you need a lot of knowledge to be able to do that, and advanced coders have problems too when it comes to debugging. Just kidding, it's a very simple process.
The main idea is to put messages( server console, chat, hud, it does not matter, you put what is more convenient to you ) in your code, you'll see exactly what is running and what is not. Messages should be placed in key positions of your code, will help you understand how your plugin thinks.
Ok, let's take a simple example. Basically, it will swap the values of variables, only if someVar is bigger than 0. If a > b the server will close, otherwise it won't do anything. This is a stupid example, but I can't think of something better for now.
Code:
#include < amxmodx >
new someVar
new a = 5, b = 6
public plugin_init( )
{
register_srvcmd("test", "ServerCommand_Test")
}
public ServerCommand_Test( )
{
new otherVar
if(someVar)
{
otherVar = a
a = b
b = otherVar
}
if( a > b )
{
server_cmd("quit")
}
}
But the plugin above doesn't work, for some reason you need it to work everytime, imagine this scenario. And you can't figure out why it's not working by yourself, so you start adding debug messages.
So, you may do:
Code:
#include < amxmodx >
new someVar
new a = 5, b = 6
public plugin_init( )
{
register_srvcmd("test", "ServerCommand_Test")
}
public plugin_end( )
{
//You make your someVar variable value 1 only here, you think it's ok
someVar = 1
server_print("someVar value was changed")
}
public ServerCommand_Test( )
{
server_print("Command was typed into server console")
new otherVar
if(someVar)
{
server_print("The values of the variables will be changed")
otherVar = a
a = b
b = otherVar
}
else
{
server_print("The values where not swapped")
}
if( a > b )
{
server_print("QUIT. WORKING")
server_cmd("quit")
}
else
{
server_print("Doesn't work" )
}
}
Running the above plugin will give you
Code:
Command was typed into server console
The values where not swapped
Doesn't work
What this tells you ?- Your command is successfully registered.
- Your values were not swapped.
Now you start thinking, why the values were not changed ? Well, we change them in an if, and the condition in the if is false, if the block of code from else is called. Now you think more. Why the condition is false ? Because the value of someVar is 0. Why the value of someVar is 0, I'm setting it to 1. After some more thinking, you ask yourself: "But what if I'm setting it to 1 to late ?". You find out when plugin_end forward is executed and you find your problem. You are setting someVar to 1 too late.
And you fix the problem, make it 1 when the command is used.
Now, you get this:
Code:
#include < amxmodx >
new someVar
new a = 5, b = 6
public plugin_init( )
{
register_srvcmd("test", "ServerCommand_Test")
}
public ServerCommand_Test( )
{
server_print("Command was typed into server console")
someVar = 1
new otherVar
if(someVar)
{
server_print("The values of the variables will be changed")
otherVar = a
a = b
b = otherVar
}
else
{
server_print("The values where not swapped")
}
if( a > b )
{
server_print("QUIT. WORKING")
server_cmd("quit")
}
else
{
server_print("Doesn't work" )
}
}
And after testing, you get:
Code:
Command was typed into server console
The values of the variables will be changed
QUIT. WORKING
I have commented the quit line, so I will be able to copy paste the messages. But you will see, that by running the above plugin your server will close.
Now, your plugin is running perfectly and you can remove the debug messages.
And now you will have a clean code, which works:
Code:
#include < amxmodx >
new someVar
new a = 5, b = 6
public plugin_init( )
{
register_srvcmd("test", "ServerCommand_Test")
}
public ServerCommand_Test( )
{
someVar = 1
new otherVar
if(someVar)
{
otherVar = a
a = b
b = otherVar
}
if( a > b )
{
server_cmd("quit")
}
}
Why I have used server_print ? Because this is a command that will be send from server console, no point in using client_print or whatever
But, if you are testing some things in-game you can do with client_print.
Don't blame me for writting this and for the dumb example above, I'm too tired to find something better, I may edit later and add a real case where debug helps you.
In most cases this will only help you to find the concept problems, if you are using some natives wrong, it doesn't help at all.
__________________
Last edited by HamletEagle; 10-31-2014 at 13:24.
|
|