Author Topic: Tip: Debugging Script  (Read 3271 times)


  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2733
    • ICQ Messenger - 2678251
    • MSN Messenger -
    • View Profile
    • Email
Tip: Debugging Script
« on: 2005-09-06, 05:43:21 PM »
Add the following code to your script so that you can easily log text to the display to see what's going on in your script
Code: [Select]
Dim MsgLog(10)
Dim LogPos
Dim DebugFont
Sub LogMsg(M)
   MsgLog(LogPos) = Now & ": " & M & vbCrLf
   LogPos = (LogPos + 1) Mod (UBound(MsgLog) + 1)
End Sub
Sub DrawLog()
   Dim Idx
   Dim DrawStr
   Dim X, Y
   If IsEmpty(DebugFont) Then
      Set DebugFont = CreateObject("StdFont")
      DebugFont.Name = "Tahoma"
      DebugFont.Size = 8
   End If
   For Idx = 0 to UBound(MsgLog)
      DrawStr = MsgLog((Idx+LogPos) Mod (UBound(MsgLog)+1))
      X = 0
      Y = 465 - (UBound(MsgLog) - Idx)*10
      CurrentDisplay.DrawText DrawStr, X+1, Y
      CurrentDisplay.DrawText DrawStr, X+1, Y+2
      CurrentDisplay.DrawText DrawStr, X, Y+1
      CurrentDisplay.DrawText DrawStr, X+2, Y+1
      CurrentDisplay.DrawText DrawStr, X+1, Y+1
End Sub

Once you have added that code, so the following to make use of it:
1. Add "DrawLog" to the beginning of your Player_OnAfterMoveSprites subroutine.  (Add a Player_OnAfterMoveSprites subroutine if you don't already have one.)  This will make sure that all your debug info is visible during every frame.

2. Call LogMsg whenever you want to add an item to the log.  For example:
LogMsg "Position " & ProjectObj.GamePlayer.PlayerSprite.X

If you want more than 11 lines of log history, increase the "10" on the first line of the script to 20 or even 40.  Note that increasing this value can significantly affect the performance of your game.  If you want to improve the performance while debugging, you can reduce the number, or remove the code between "BEGIN DRAW OUTLINE" and "END DRAW OUTLINE", but be aware that drawing the debug text without an outline might result in text that is hard to read.  You can change the color by using a constant other than vbWhite for the ForeColor.

Remember to remove this from your script (or at least comment out the calls to DrawLog) before releasing your game.