Debugging a game can sometimes be difficult. One often cannot use a simple print statement at the location of interest because the screen is overwritten milliseconds later by the graphics stuff. A simple logToFile subroutine has helped me to track what is going on. In combination with a "tail -f logfile.txt" (linux command, don't know the windows equivalent) in a second window, events and error messages can be tracked almost real time (about 1 second delay).
Code with example use:
const as string LOG_FILE_NAME = "logfile.txt"
sub logToFile(text as string)
dim as integer fileNum
fileNum = freefile
open LOG_FILE_NAME for append as fileNum
print #fileNum, time & " " & text
close fileNum
end sub
'------------ test code -------------
sub doSometing()
dim as integer errorCode = int(rnd*10)
logToFile("Something went wrong, error: " & str(errorCode))
end sub
dim as double rndSeed = 1234
randomize 1234
logToFile("Game start")
sleep 100, 1
logToFile("Random seed: " & str(rndSeed))
sleep 100, 1
doSometing()
sleep 100, 1
logToFile("Game end")
print "End. See output in: " & LOG_FILE_NAME
This produces: ...
01:20:52 Game start
01:20:53 Random seed: 1234
01:20:53 Something went wrong, error: 2
01:20:53 Game end
...
Sub DebugFile(FName As String, Note As String)
'if not defined then will do nothing!
#Ifdef __DebugFileOn__
Dim As Integer DFile, FileCnt,KB
Dim As Double LastLog
Dim As String CheckIt, NextCheck
'
KB = 30 'this can be changed any time
'check size of file, if getting large, make new file (infinite loop or A LOT of errors ????)
'skip over older files!!!
Do
FileCnt += 1
If FileCnt > 1 Then
CheckIt = NextCheck
Else
CheckIt = ExePath + "\" + FName + ".txt"
EndIf
'append # to name
NextCheck = ExePath + "\" + FName + "-" + Str(FileCnt+1)+ ".txt"
If FileExists(NextCheck)=0 Then
'check size of last used file
If FileLen(CheckIt) > KB * 1024 Then CheckIt = NextCheck
Exit Do
EndIf
Loop
LastLog = 0
If FileExists(CheckIt) Then
LastLog = FileDateTime(CheckIt)
EndIf
DFile = FreeFile
Open CheckIt For Append As #DFile
If Int(Now) > Int(LastLog) Then
'new date
If Lof(DFile) Then Print #DFile, "" 'blank line -- will not add if a "new" file
Print #DFile, String(10,"-");" ";
Print #DFile, Format(Now,"mmmm d, yyyy");
Print #DFile, " ";String(10,"-")
EndIf
Print #DFile, "At ";
Print #DFile, Format(Now,"h:mm:ssam/pm");
Print #DFile, " ";Note
Close #DFile
#EndIf
End Sub