PDA

View Full Version : Guide to Debugging!



Nava2
05-13-2008, 02:02 AM
-=~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~-


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 = True

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:

if Debugging = true then

or

if Debugging then

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.

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

Nava2
05-13-2008, 02:07 AM
I will edit it more tomorrow, bed time for now :)

Nava2

Awkwardsaw
05-13-2008, 02:19 AM
hahaha, did you helping me with my debugging give you inspiration for this tut? i saw you posted this today soo. =p

oh yeah, your debugging procedure has an error,

procedure NDB(Proc : String);//Line 169: [Error] (12823:14): Type mismatch in script
begin
if Debugging then
Writeln('Debug: '+ Proc + '; Time: ' + IntToStr(GetTimeRunning));
Status(Proc);
end;

just thought i'd let you know =D

Nava2
05-13-2008, 12:17 PM
hahaha, did you helping me with my debugging give you inspiration for this tut? i saw you posted this today soo. =p

oh yeah, your debugging procedure has an error,

procedure NDB(Proc : String);//Line 169: [Error] (12823:14): Type mismatch in script
begin
if Debugging then
Writeln('Debug: '+ Proc + '; Time: ' + IntToStr(GetTimeRunning));
Status(Proc);
end;

just thought i'd let you know =D

I swear I said that most wouldn't compile. NOOB!

Jokes; uhm, honestly I do not see the error. I don't get one when run my version... I had to edit it because I didn't want to include the Time procedure I have to make my time look pretty.

I will look into it real quick.

Nava2

Awkwardsaw
05-13-2008, 01:25 PM
he, iv never actually read it, i just looked over it and noticed its the same thing that you told me earlier

mastaraymond
05-13-2008, 02:54 PM
hahaha, did you helping me with my debugging give you inspiration for this tut? i saw you posted this today soo. =p

oh yeah, your debugging procedure has an error,

procedure NDB(Proc : String);//Line 169: [Error] (12823:14): Type mismatch in script
begin
if Debugging then
Writeln('Debug: '+ Proc + '; Time: ' + IntToStr(GetTimeRunning));
Status(Proc);
end;

just thought i'd let you know =D
program Hoi;
const
Debugging = true;
procedure NDB(Proc : String);//Line 169: [Error] (12823:14): Type mismatch in script
begin
if Debugging then
Writeln('Debug: '+ Proc + '; Time: ' + IntToStr(GetTimeRunning));
Status(Proc);
end;
begin
While true do
begin;
NDB('Mainloop');
Wait(20);
end;
end.
It compiles for me?:rolleyes:

Nava2
05-13-2008, 08:24 PM
Well, I didn't try just compiling that but it SHOULD work haha.

I also edited the procedures to be easier to read, with less bolded words.. They weren't intentional :P

Nava2

skilld u
05-13-2008, 08:32 PM
nice, im in credits?

mixster
05-13-2008, 08:37 PM
I find a debug procedure is better when you use at as a writeln that only appears when a DebugScript - or something similar - const is set to true. In fact, I think I may have even posted a simple version of it in the Public Test Corner along with another procedure I made and used it in...
Saying that a procedure is running when something goes wrong isn't as useful as seeing what is happening when it goes wrong :)

Nava2
05-14-2008, 12:45 AM
I find a debug procedure is better when you use at as a writeln that only appears when a DebugScript - or something similar - const is set to true. In fact, I think I may have even posted a simple version of it in the Public Test Corner along with another procedure I made and used it in...
Saying that a procedure is running when something goes wrong isn't as useful as seeing what is happening when it goes wrong :)

Uhm; that seems really.. repetitive and I'm not sure what you meant. My procedure has an on off switch aka a Const boolean... Can you explain what you mean?

Nava2

Nava2
05-15-2008, 11:40 PM
Ugh; spent an hour typing that out and no one uses it :(

Nava2

livewrong811
08-03-2008, 01:16 PM
Nice job, I learned a lot.

Nava2
08-05-2008, 04:17 PM
Nice job, I learned a lot.

You know, thats all I wanted from writing this! :)

massive630
01-16-2009, 09:33 PM
Thanks alot Nava, i always did this same thing with "WriteLn" but using a debug procedure will make it easier, and add some uniformity. =)