this post is really unnecessary(You can delete this post if needed), but want to show an example how i debug my code.
Last week some random errors occur on my timeattack script, when it tries to save the local saved stagetimes to the textfiles. The Timeattack server is down due to this bug. Testing during development went fine BTW.
Looking into the ERR log file it tells me this, and sometimes other errors related to this.
2/13/2024 7:30:00 PM -> Syntax error: in file ".\default\.\includes\.\RallySystem\Scripts\NewTimes.LPR" at line #212
ERROR on Function: 'EditFile', ERROR on Function: 'EditFile', File is empty: [0] lines detected, you cant edit any lines.
Function 'writetofile' script aborted
These error came on very random moments and i couldn't pinpoint the problem. So that means i have to debug my code.
Let me show you my example of debugging. Warning, its a large piece of code
Do not copy the code, it simply doesnt work!!
As you can see i've created alot of debug messages, even on lines that do not need a debug message.
Sub WriteToFile()
$SaveStageCount = arrayCount($SaveStageTimesStage);
Globalmsg("^4DEBUG: ^8Start WriteToFile() With ^3".$SaveStageCount." ^8Veh/Stage Combo's");
IF($SaveStageCount > 0) THEN
FOR ( $x = 0; $x < ToNum($SaveStageCount) ; $x = $x + 1)
$SaveTimeStage = $SaveStageTimesStage[ToNum($x)];
$SaveTimeVeh = $SaveStageTimesVeh[ToNum($x)];
Globalmsg("^2DEBUG: ^8[".$x."] ^8Vehicle: ".$SaveTimeVeh." / Stage: ". $SaveTimeStage);
IF(($SaveTimeStage != "") && ($SaveTimeVeh != "")) THEN
#Globalmsg("^3DEBUG: ^8WriteToFile() Loop ".$SaveTimeStage." / ".$SaveTimeVeh." || ^3".$CountTimesOfCar[ToNum($SaveTimeStage),$SaveTimeVeh]);
$StageDir = $StageTimes_Dir . "/Stage_" . $SaveTimeStage; #RallySystem\StageData\TrackConfig\NameOfTimeTable\Stage_X
#Verify if vehiclefile for current stage exist
IF (FileExist($StageDir,$SaveTimeVeh,".txt") == 1) THEN
#Get information about the vehicle file.
$TimeInfoFromFile = ReadFile($SaveTimeVeh,$StageDir,".txt");
$MaxFileRecords = $TimeInfoFromFile["NumberOfLines"];
$ArrayCount = ToNum($CountTimesOfCar[ToNum($SaveTimeStage),$SaveTimeVeh]);
globalmsg("^3DEBUG: Records in File [Stage ".$SaveTimeStage."/".$SaveTimeVeh."]: ^3".$MaxFileRecords." ^0/ ^6".$ArrayCount);
$RecordsWritten = 0;
$Deletedlines = 0;
FOR ( $Pos = 0; $Pos < ToNum($ArrayCount); $Pos = $Pos + 1)
$GetRecordInfo = $TimeOfCar[ToNum($Pos),ToNum($SaveTimeStage),$SaveTimeVeh,"Info"]."";
#Ignore if $getRecordInfo is empty. This line wont be saved to the timefile
IF($GetRecordInfo != "") THEN
$RecordsWritten = $RecordsWritten + 1;
globalmsg("^3DEBUG: WriteToFile() Write/Array ".$RecordsWritten." <> ".$MaxFileRecords);
IF(ToNum($RecordsWritten) > ToNum($MaxFileRecords))THEN
#If current list of records is larger than the list of records from current timefile, create a new line.
EditFile($SaveTimeVeh,$StageDir,$GetRecordInfo,-1,".txt"); #NewLine
globalmsg("^3DEBUG: ^8New Record ");
ELSE
#Overwrite every record.
EditFile($SaveTimeVeh,$StageDir,$GetRecordInfo,$RecordsWritten,".txt"); #NewLine
globalmsg("^3DEBUG: ^8Record Overwritten");
ENDIF
ELSE
globalmsg("^3DEBUG: Warning >> ^8NO DATA: ".$SaveTimeVeh." / ".$SaveTimeStage." / ".$Pos);
ENDIF
ENDFOR
IF( $RecordsWritten > 0) THEN
#Delete Records that exceed the maximum records for the current vehicle/stage combo (Records can be deleted by player in !times)
IF(ToNum($RecordsWritten) < ToNum($ArrayCount)) THEN
FOR ( $Line = $ArrayCount-1; $Line >= ToNum($TotalRecordsWritten) ; $Line = $Line - 1)
EditFile($SaveTimeVeh,$StageDir,-1,$Line,".txt"); #NewLine
$Deletedlines = $Deletedlines + 1;
ENDFOR
ENDIF
ENDIF
Globalmsg("^3DEBUG: ^8Total Lines Written/Deleted [Stage ".$SaveTimeStage."/".$SaveTimeVeh."]: ^2".$RecordsWritten."^0/^1".$Deletedlines);
ELSE
Globalmsg("^3ERROR: ^8Cannot save stagetime: ^3[Stage ".$SaveTimeStage."/".$SaveTimeVeh."], ^7Contact admin!");
ENDIF
ENDIF
ENDFOR
Globalmsg("^3DEBUG: ^8Clear/Reset $SaveStageTimesStage & $SaveStageTimesVeh Array + Counter");
UnSet($SaveStageTimesStage);
UnSet($SaveStageTimesVeh);
$SaveStageTimesCounter = 0;
#LogSystem
$ConvertShortDate = Replace(GetLapperVar( "ShortDate" ),"/","-");
$NewLine = StripLFSColor("".GetLapperVar( "ShortDate" )."-".GetLapperVar( "ShortTime" ) ."|TIMES| Scheduled task to save stagetimes to file");
EditFile($ConvertShortDate,$Insim_Log_Dir,$NewLine,-1,".txt"); #NewLine
#Globalmsg("^3DEBUG: ^8WriteToFile() Finished");
ENDIF
EndSub