Nava2
2008-05-12 22:02:47
-=~Nava2's Guide to Debugging~=-
-~Introduction~-
Well, this is a tutorial on adding a debugging procedure to your script, as well as how to use it!
By reading this, you acknowledge that you know some minor scripting knowledge of Scar and SRL. All examples are in SRL Rev 16. Most Code will not compile please do not complain about it.
I will use some of MY codes, if you would like to use them; make sure to CREDIT.
-~Table of Contents~-
-~Debugging: Why use it?~-
Every run your script and you get those annoying bugs? The mouse just does not move? The script crashes? Or maybe it just gets lost unexpectedly? Well debugging can help you get to the root of your problem.
It shows you the procedures that you are calling as well as it shows when they are called and the time that they were called. It can show you how to speed up your procedures and reduce lag on your script.
How to write a procedure
For starters, we will use a basic procedure:
A procedure allows us to run the debug when ever it is called.
Now, how do we have scar tell us what the debug is doing? Writeln.
So, so far our procedure reads:
Well that would be useful if every time we used it, we were in our debugging procedure.
The easiest and BEST way that I know of to show what procedure is using a string variable defined when it is called. We will call ours Proc (Short for Procedure). This looks like:
Now, we need to use this newly defined string in our procedure! We can simply do that by calling it in the Writeln.
Now that makes our debugging procedure Dynamic and it can change as we call it.
Our procedure thus far:
That procedure allows us to see what procedure is called when it is called. It will be displayed in the debug window! (Ironic its called the Debug window :rolleyes:).
Now, we can also include the running time. Its very simple.
We merely change the Writeln.
Now, I just introduced two new commands. The IntToStr command which makes any integer into a string, easy enough, and the GetTimeRunning Command. The GetTimeRunning command is an SRL function which gives us the current running time in MS. Although most people do not tell time in MS it gives us a time. Now it has come to my attention that the time doesn't work try using this if the previous doesn't work:
I only added a local variable (z) and told the script to define it as the number of ms the script has been running for. Simply put, z = the running time of the script.
Procedure now:
Hm, so this is great and dandy, but what if we do not want everyone who uses the script to get these handy, but obnoxious warnings? Simple an On/Off switch. This switch can be easily defined by a boolean. True being On and False being Off. We define the switch in the constants of the script.
So we have this magnificent switch, how do we use it? We use an If else statement. We say If Debugging is true then I want the script to tell me my debugging information. We can write this like so:
There are two ways, one just shaves a few keystrokes! Now, that would mean that the script just checks then runs the debug if its true. Lets add that to the procedure.
Now, we have a switch on and off, and we have a debugging procedure. There are some extra features you can use. Such as:
The Status command which displays the strings in the status bar at the bottom of the Scar window.
Any other useful ones? Tell me and I will add them.
This is MY debug procedure:
My debug, if true tells me the Procedure its running, and the time running. It also, regardless if on or off, displays the procedure in the status bar. Also, a hint, making the name short makes it easier to call it later on.
-~How to use it~-
Using debugging is the easiest part. Using it involves calling it in the procedures you run. Simply place it at the top of your script; and call it later on.
An example taken from my Fally West Miner:
Notice how I called the debugging at the beginning? Because the switch is declared in the procedure of debug, all I have to do is call it and it will work if its on and not if its off.
When the procedure FindPickaxe is called, the Debug procedure will write: Debug: FindPickaxe, Time: [The time since the script start.].
If my script stopped running and froze up, and the last line written was the latter, then I would know that that's where my error lies! I could then add more debug procedures to narrow it down until I found the error. The following is excessive but an idea:
Thats basically how you use Debugging, but I'm sure others can find more uses! :D
-~Credits and Post Script~-
Credits to:
Hermpie: Taught me my first things about scripting.
SRL Community: Well, why else would I write this?
Bullzeye95: Great helper and good person.
Skilld U: Great helper as well, good person as well :)
If you think this helped you, please feel free to rep me as well as rate the thread!
Please post any corrections or suggestions!
Nava2
-~Introduction~-
Well, this is a tutorial on adding a debugging procedure to your script, as well as how to use it!
By reading this, you acknowledge that you know some minor scripting knowledge of Scar and SRL. All examples are in SRL Rev 16. Most Code will not compile please do not complain about it.
I will use some of MY codes, if you would like to use them; make sure to CREDIT.
-~Table of Contents~-
- Introduction
- Table of Contents
- Debugging: Why use it?
- How to write a procedure.
- How to use it.
- Credits and Post Script
-~Debugging: Why use it?~-
Every run your script and you get those annoying bugs? The mouse just does not move? The script crashes? Or maybe it just gets lost unexpectedly? Well debugging can help you get to the root of your problem.
It shows you the procedures that you are calling as well as it shows when they are called and the time that they were called. It can show you how to speed up your procedures and reduce lag on your script.
How to write a procedure
For starters, we will use a basic procedure:
procedure Debug;A procedure allows us to run the debug when ever it is called.
Now, how do we have scar tell us what the debug is doing? Writeln.
begin
Writeln('Debug Procedure');
end;So, so far our procedure reads:
procedure Debug;
begin
Writeln('Debug Procedure');
end;Well that would be useful if every time we used it, we were in our debugging procedure.
The easiest and BEST way that I know of to show what procedure is using a string variable defined when it is called. We will call ours Proc (Short for Procedure). This looks like:
procedure Debug(Proc: string);Now, we need to use this newly defined string in our procedure! We can simply do that by calling it in the Writeln.
begin
Writeln('Procedure: ' + Proc);
end;Now that makes our debugging procedure Dynamic and it can change as we call it.
Our procedure thus far:
procedure Debug(Proc: string);
begin
Writeln('Procedure: ' + Proc);
end;That procedure allows us to see what procedure is called when it is called. It will be displayed in the debug window! (Ironic its called the Debug window :rolleyes:).
Now, we can also include the running time. Its very simple.
We merely change the Writeln.
begin
Writeln('Procedure: ' + Proc + ', Time: ' + IntToStr(GetTimeRunning));
end;Now, I just introduced two new commands. The IntToStr command which makes any integer into a string, easy enough, and the GetTimeRunning Command. The GetTimeRunning command is an SRL function which gives us the current running time in MS. Although most people do not tell time in MS it gives us a time. Now it has come to my attention that the time doesn't work try using this if the previous doesn't work:
begin
z:= GetTimeRunning;
Writeln('Procedure: ' + Proc + ', Time: ' + IntToStr(z));
end;I only added a local variable (z) and told the script to define it as the number of ms the script has been running for. Simply put, z = the running time of the script.
Procedure now:
procedure Debug(Procedure: string);
var z: integer;
begin
z := GetTimeRunning;
Writeln('Procedure: ' + Procedure + ', Time: ' + IntToStr(z));
end;Hm, so this is great and dandy, but what if we do not want everyone who uses the script to get these handy, but obnoxious warnings? Simple an On/Off switch. This switch can be easily defined by a boolean. True being On and False being Off. We define the switch in the constants of the script.
const Debugging = TrueSo we have this magnificent switch, how do we use it? We use an If else statement. We say If Debugging is true then I want the script to tell me my debugging information. We can write this like so:
if Debugging = true then
or
if Debugging thenThere are two ways, one just shaves a few keystrokes! Now, that would mean that the script just checks then runs the debug if its true. Lets add that to the procedure.
const Debugging = true;
procedure Debug(Procedure: string);
var z: integer;
begin
if Debugging then
begin
z := GetTimeRunning;
Writeln('Procedure: ' + Procedure + ', Time: ' + IntToStr(z));
end;
end;Now, we have a switch on and off, and we have a debugging procedure. There are some extra features you can use. Such as:
The Status command which displays the strings in the status bar at the bottom of the Scar window.
Status(String);Useful if you want to see the procedures activating but not see the Debug Box filled. Also useful if you want to run your script as normal and still see the debugging.Any other useful ones? Tell me and I will add them.
This is MY debug procedure:
{-NDB aka Nava2's Debug!-}
procedure NDB(Proc : String);
begin
if Debugging then
Writeln('Debug: '+ Proc + '; Time: ' + Time2('total', 0));
Status(Proc);
end;Time2 is a useful function I edited to write the time in Hrs, mins, and seconds.My debug, if true tells me the Procedure its running, and the time running. It also, regardless if on or off, displays the procedure in the status bar. Also, a hint, making the name short makes it easier to call it later on.
-~How to use it~-
Using debugging is the easiest part. Using it involves calling it in the procedures you run. Simply place it at the top of your script; and call it later on.
An example taken from my Fally West Miner:
{*******************************************************************************
function PickaxeFind: integer;
By: Nava2
Description: Finds PickAxe in inventory, or wielded. Returns the integer
of the pickaxe found.
*******************************************************************************}
function PickaxeFind: integer;
var i: integer;
begin
NDB('PickaxeFind'); // If you didn't read before, this is my debug procedure.
result:= 6;
GameTab(4);
DeclareDTM('PickDTM');
for i := 0 to 5 do
if FindDTM(PickDTM[i], x, y, 547, 202, 737, 466) then
begin
if CoordsToItem(x, y) <> 1 then
begin
DragItem(CoordsToItem(x, y), 1);
Spc('Silly Billy,');
Spc('I told you to put your pick in the first.');
end;
Wait(500+random(250));
result := i;
k := 2;
end;
if result = 6 then
begin
GameTab(5);
for i := 0 to 5 do
if FindDTM(PickDTM[i], x, y, 547, 202, 737, 466) then
begin
Wait(500+random(250));
result := i;
k := 1;
end;
GameTab(4);
end;
for i:= 0 to 5 do
FreeDTM(PickDTM[i]);
end;Notice how I called the debugging at the beginning? Because the switch is declared in the procedure of debug, all I have to do is call it and it will work if its on and not if its off.
When the procedure FindPickaxe is called, the Debug procedure will write: Debug: FindPickaxe, Time: [The time since the script start.].
If my script stopped running and froze up, and the last line written was the latter, then I would know that that's where my error lies! I could then add more debug procedures to narrow it down until I found the error. The following is excessive but an idea:
{*******************************************************************************
function PickaxeFind: integer;
By: Nava2
Description: Finds PickAxe in inventory, or wielded. Returns the integer
of the pickaxe found.
*******************************************************************************}
function PickaxeFind: integer;
var i: integer;
begin
NDB('PickaxeFind'); // If you didn't read before, this is my debug procedure.
result:= 6;
GameTab(4);
DeclareDTM('PickDTM');
NDB('PickaxeFind2');
for i := 0 to 5 do
if FindDTM(PickDTM[i], x, y, 547, 202, 737, 466) then
begin
if CoordsToItem(x, y) <> 1 then
begin
NDB('PickaxeFind3');
DragItem(CoordsToItem(x, y), 1);
Spc('Silly Billy,');
Spc('I told you to put your pick in the first.');
end;
NDB('PickaxeFind4');
Wait(500+random(250));
result := i;
k := 2;
end;
if result = 6 then
begin
GameTab(5);
NDB('PickaxeFind5');
for i := 0 to 5 do
if FindDTM(PickDTM[i], x, y, 547, 202, 737, 466) then
begin
NDB('PickaxeFind6');
Wait(500+random(250));
result := i;
k := 1;
end;
GameTab(4);
NDB('PickaxeFind7');
end;
for I:= 0 to 5 do
FreeDTM(PickDTM[I]);
NDB('PickaxeFind8');
end;Although excessive, it would narrow down my error easily telling me what the problem line is.Thats basically how you use Debugging, but I'm sure others can find more uses! :D
-~Credits and Post Script~-
Credits to:
Hermpie: Taught me my first things about scripting.
SRL Community: Well, why else would I write this?
Bullzeye95: Great helper and good person.
Skilld U: Great helper as well, good person as well :)
If you think this helped you, please feel free to rep me as well as rate the thread!
Please post any corrections or suggestions!
Nava2