I thought I would write a little tutorial on how to debug your game. This won’t guarantee that you will fix whatever problem you have but it should make it easier to find problems that you have. This is also from experience so if you think something seems wrong, if I should add something, or if there is an actual term for some of these words (yes I am making up words for a lack of a better term) do tell
.
First off there is pretty much three kinds of problems you could have in your project – Run-Time Error, Syntax Error and Logical Error. A Syntax Error is when you write the command wrong, you don't have enough parameters, or it isn't the right data type. Thanks to an IDE though it will not highlight the command so it is easy to pick out. A Run-Time Error is basically an error that stops your code from running. A Logical Error is when your problem does run but it doesn’t do what you want it to do, example – Object AI should move around the waypoints but doesn’t.
Here are some general things you can do to help solve your problems.
1. Indent your code – this will make your code easier to look at and easier to follow. The way I indent my code is indent if/endifs, case/endcase, select/endselect, loops, and functions. Just look at this and tell me what looks better
This-
function get_word(string$ as string,w as byte)
`declare everything
i as byte
_w as byte
`start from the first character in the string
i=1
`first fix the string by removing a space if it is near another space or comma so it wont messup the function
for i=0 to len(string$)
`see if the old char is a space or a comma and the new char is a space or comma
if char$="," or char$=" "
if mid$(string$,i)=" " or mid$(string$,i)="," then string$=left$(string$,i-1)+right$(string$,len(string$)-i)
endif
`store it
char$=mid$(string$,i)
next i
`add an extra space to the begining so it wont chop off the first part of the word
string$=" "+string$
`go to the part of the string they want
for i=1 to len(string$)
`if there is a space or comma tell the function
if mid$(string$,i)=" " or mid$(string$,i)="," then inc _w
`see if that is the word they want if it is make a new string from it and get out of the loop
if w=_w then string$=right$(string$,len(string$)-i) : i=len(string$)
next i
`go through the word until it hits a space or comma or the end of the string
for i=1 to len(string$)
`check the character
if mid$(string$,i)="," or mid$(string$,i)=" " then string$=left$(string$,i-1) : exitfunction string$
next i
endfunction string$
Or this-
function get_word(string$ as string,w as byte)
`declare everything
i as byte
_w as byte
`start from the first character in the string
i=1
`first fix the string by removing a space if it is near another space or comma so it wont messup the function
for i=0 to len(string$)
`see if the old char is a space or a comma and the new char is a space or comma
if char$="," or char$=" "
if mid$(string$,i)=" " or mid$(string$,i)="," then string$=left$(string$,i-1)+right$(string$,len(string$)-i)
endif
`store it
char$=mid$(string$,i)
next i
`add an extra space to the begining so it wont chop off the first part of the word
string$=" "+string$
`go to the part of the string they want
for i=1 to len(string$)
`if there is a space or comma tell the function
if mid$(string$,i)=" " or mid$(string$,i)="," then inc _w
`see if that is the word they want if it is make a new string from it and get out of the loop
if w=_w then string$=right$(string$,len(string$)-i) : i=len(string$)
next i
`go through the word until it hits a space or comma or the end of the string
for i=1 to len(string$)
`check the character
if mid$(string$,i)="," or mid$(string$,i)=" " then string$=left$(string$,i-1) : exitfunction string$
next i
endfunction string$
Hopefully you said the second one if you didn’t never post your code on this forum EVER!
2. Don’t use GOTO. They make your code messy and hard to follow because it jumps everywhere. You should use functions instead because they are more organized and your variables are not global, unless you want them to be, and you may reuse the same variable.
Now you should create a set of debug functions. These functions will store any
information that you need to solve your problem, if done correctly. Basically the functions will get any information that gets sent to it and writes it into a file. The information can come from other functions, variables, arrays, ect. I’m not going to make these functions for you because I go by “No one understands your code more than you”. Although I will give some pseudo-code
function _debug(string)
array insert at bottom debug()
debug(array count(debug()))=string
endfunction
function export_debug()
if file exist(“debug.txt”)=1 then delete file “debug.txt”
open to write 1,”debug.txt”
for x=0 to array count(debug())
write string 1,debug(x)
next x
close file
endfunction
function load_object(filename$,objnum)
if file exist(filename$)=1
load object filename$,objnum
else
_debug(“Load_Object() - ”filename$+“ file does not exist”)
endif
endfunction
As you can see there is an array called debug() and that holds any debug information throughout project. The info will be written to the array using _debug() and it will be exported using export_debug(). There is also one example function that loads an object. If you get an error because the object doesn’t exist you could just check the debug.txt and you will find out right away what happened. This could solve any Logical or Compiler errors, if done right.
If you don’t want any Run-Time errors, or close to any, then never use hard coded values for object numbers, image numbers, sprite numbers, ect. Instead use a function to find the next free number.
Ex –
function free_object()
repeat
inc i
until object exist(i)=0
endfunction i
The best way to solve logical errors is to just think it out. All code does really is to act out the commands so its all logical which is why the rnd() command really stinks. The Debug functions should also help by writing what happens in the function and then see where your logic went wrong although if you know there is no problem with your code then its probably a bug with the compiler but its probably not to likely.
Hope this tutorial helps anyone. I will probably write some more tutorials if this is any good so once I can finish my site I can put a bunch of tutorials on it
. I also hope I didn’t miss anything since it took me about an hour to write this.
Working on Boxed for NVIDIA compo. Check it out