PDA

View Full Version : Saving race time not working.


finelaq
19/01/2015, 06:54 PM
Hello samp forum!
So i have this code:

if(RaceMinutes[playerid] >= 1)
{
if(RaceSeconds[playerid] < pInfo[playerid][SultanSeconds])
{
pInfo[playerid][SultanMiliSeconds] = RaceMiliSeconds[playerid];
pInfo[playerid][SultanSeconds] = RaceSeconds[playerid];
pInfo[playerid][SultanMinutes] = RaceMinutes[playerid];
SavePlayer(playerid);
format(string, sizeof(string),"[Mission]: This is your new record: %d:%d:%d.",RaceMinutes[playerid], RaceSeconds[playerid], RaceMiliSeconds[playerid]);
SendClientMessage(playerid, -1, string);
RaceMinutes[playerid] = 0;
RaceSeconds[playerid] = 0;
RaceMiliSeconds[playerid] = 0;
}
else
{
format(string, sizeof(string),"[Mission]: Your best time is: %d:%d:%d.",pInfo[playerid][SultanMinutes], pInfo[playerid][SultanSeconds], pInfo[playerid][SultanMiliSeconds]);
SendClientMessage(playerid, -1, string);
RaceMinutes[playerid] = 0;
RaceSeconds[playerid] = 0;
RaceMiliSeconds[playerid] = 0;
}
}
else
{
if(RaceSeconds[playerid] < pInfo[playerid][SultanSeconds])
{
pInfo[playerid][SultanMiliSeconds] = RaceMiliSeconds[playerid];
pInfo[playerid][SultanSeconds] = RaceSeconds[playerid];
pInfo[playerid][SultanMinutes] = RaceMinutes[playerid];
SavePlayer(playerid);
format(string, sizeof(string),"[Mission]: This is your new record: %d:%d:%d.",RaceMinutes[playerid], RaceSeconds[playerid], RaceMiliSeconds[playerid]);
SendClientMessage(playerid, -1, string);
RaceMinutes[playerid] = 0;
RaceSeconds[playerid] = 0;
RaceMiliSeconds[playerid] = 0;
}
else
{
format(string, sizeof(string),"[Mission]: Your best time is: %d:%d:%d.",pInfo[playerid][SultanMinutes], pInfo[playerid][SultanSeconds], pInfo[playerid][SultanMiliSeconds]);
SendClientMessage(playerid, -1, string);
RaceMinutes[playerid] = 0;
RaceSeconds[playerid] = 0;
RaceMiliSeconds[playerid] = 0;
}
}


I don't know is this the best code :D
But the problem is, when i end the race it tells me: "Your best time is: 0:0:0" And sometimes tell me "This is your new record: 0:0:0."
I don't understand where is the problem?

Schneider
19/01/2015, 07:00 PM
Are the right values stored in the RaceMinutes/RaceSeconds/RaceMiliSeconds variables?

finelaq
19/01/2015, 07:03 PM
Are the right values stored in the RaceMinutes/RaceSeconds/RaceMiliSeconds variables?

Yes.

Schneider
19/01/2015, 07:05 PM
Well, if they are correctly saved, they shouldn't appear as zero's in the messages.. So somehow they are still '0'.

Are you also sure these variables are not reset in the SavePlayer(playerid); function?

finelaq
19/01/2015, 07:12 PM
Well, if they are correctly saved, they shouldn't appear as zero's in the messages.. So somehow they are still '0'.

Are you also sure these variables are not reset in the SavePlayer(playerid); function?

This is my saving:

stock SavePlayer(playerid)
{
new INI:file = INI_Open(Path(playerid));
INI_SetTag(file,"Player's Data");
INI_WriteInt(file,"AdminLevel",pInfo[playerid][Adminlevel]);
INI_WriteInt(file,"VIPLevel",pInfo[playerid][VIPlevel]);
INI_WriteInt(file,"Money",GetPlayerMoney(playerid));
INI_WriteInt(file,"Scores",GetPlayerScore(playerid));
INI_WriteInt(file,"Kills",pInfo[playerid][Kills]);
INI_WriteInt(file,"Deaths",pInfo[playerid][Deaths]);
INI_WriteInt(file,"SultanMiliSeconds",pInfo[playerid][SultanMiliSeconds]);
INI_WriteInt(file,"SultanSeconds",pInfo[playerid][SultanSeconds]);
INI_WriteInt(file,"SultanMinutes",pInfo[playerid][SultanMinutes]);
INI_Close(file);
}


I don't get it, where is the problem -.-

Schneider
19/01/2015, 07:22 PM
Ok,
can you show us the piece of code where RaceMinutes/RaceSeconds/RaceMiliSeconds are set when the race is finished?

finelaq
19/01/2015, 07:27 PM
Ok,
can you show us the piece of code where RaceMinutes/RaceSeconds/RaceMiliSeconds are set when the race is finished?

This is the timer:

public RaceTimer(playerid)
{
RaceMiliSeconds[playerid] += 100;
if(RaceMiliSeconds[playerid] == 1000)
{
RaceSeconds[playerid]++;
RaceMiliSeconds[playerid] = 0;
}
if(RaceSeconds[playerid] == 60)
{
RaceMinutes[playerid]++;
RaceSeconds[playerid] = 0;
}
new string[16];
format(string, 16, "%d:%d:%d", RaceMinutes[playerid], RaceSeconds[playerid], RaceMiliSeconds[playerid]);
TextDrawSetString(Race[playerid], string);
}


And whole code when race ends:

else if (CP[playerid] == 11)
{
new string[70];
DisablePlayerCheckpoint(playerid);
SendClientMessage(playerid, -1, "[Mission]: Sultan mission is completed! You get +$1200 ja +2 score!");
GivePlayerCash(playerid, 1200);
SetPlayerScore(playerid, GetPlayerScore(playerid) + 2);
Missioonil[playerid] = 0;
KillTimer(RaceT[playerid]);
format(string, sizeof(string),"[Mission]: You ended Sultan missioni. Time: %d:%d:%d.",RaceMinutes[playerid], RaceSeconds[playerid], RaceMiliSeconds[playerid]);
SendClientMessage(playerid, -1, string);
TextDrawHideForPlayer(playerid, Race[playerid]);
if(RaceMinutes[playerid] >= 1)
{
if(RaceSeconds[playerid] < pInfo[playerid][SultanSeconds])
{
pInfo[playerid][SultanMiliSeconds] = RaceMiliSeconds[playerid];
pInfo[playerid][SultanSeconds] = RaceSeconds[playerid];
pInfo[playerid][SultanMinutes] = RaceMinutes[playerid];
SavePlayer(playerid);
format(string, sizeof(string),"[Mission]: This is your new record: %d:%d:%d.",RaceMinutes[playerid], RaceSeconds[playerid], RaceMiliSeconds[playerid]);
SendClientMessage(playerid, -1, string);
RaceMinutes[playerid] = 0;
RaceSeconds[playerid] = 0;
RaceMiliSeconds[playerid] = 0;
}
else
{
format(string, sizeof(string),"[Mission]: Your best time is: %d:%d:%d.",pInfo[playerid][SultanMinutes], pInfo[playerid][SultanSeconds], pInfo[playerid][SultanMiliSeconds]);
SendClientMessage(playerid, -1, string);
RaceMinutes[playerid] = 0;
RaceSeconds[playerid] = 0;
RaceMiliSeconds[playerid] = 0;
}
}
else
{
if(RaceSeconds[playerid] < pInfo[playerid][SultanSeconds])
{
pInfo[playerid][SultanMiliSeconds] = RaceMiliSeconds[playerid];
pInfo[playerid][SultanSeconds] = RaceSeconds[playerid];
pInfo[playerid][SultanMinutes] = RaceMinutes[playerid];
SavePlayer(playerid);
format(string, sizeof(string),"[Mission]: This is your new record: %d:%d:%d.",RaceMinutes[playerid], RaceSeconds[playerid], RaceMiliSeconds[playerid]);
SendClientMessage(playerid, -1, string);
RaceMinutes[playerid] = 0;
RaceSeconds[playerid] = 0;
RaceMiliSeconds[playerid] = 0;
}
else
{
format(string, sizeof(string),"[Mission]: Your best time is: %d:%d:%d.",pInfo[playerid][SultanMinutes], pInfo[playerid][SultanSeconds], pInfo[playerid][SultanMiliSeconds]);
SendClientMessage(playerid, -1, string);
RaceMinutes[playerid] = 0;
RaceSeconds[playerid] = 0;
RaceMiliSeconds[playerid] = 0;
}
}
}

Schneider
19/01/2015, 07:34 PM
Does the textdraw show you the correct time at each timer-update?

finelaq
19/01/2015, 07:37 PM
Does the textdraw show you the correct time at each timer-update?

Yes. And this also works:

format(string, sizeof(string),"[Mission]: You ended Sultan missioni. Time: %d:%d:%d.",RaceMinutes[playerid], RaceSeconds[playerid], RaceMiliSeconds[playerid]);
SendClientMessage(playerid, -1, string);

Schneider
19/01/2015, 07:39 PM
-delete-

Schneider
19/01/2015, 08:12 PM
For me the whole minutes, seconds & milliseconds is pretty confusing, you might want to consider using just 1 variable (milliseconds) to keep the time of the current race and the record. And use a converter-function to convert milliseconds to minutes, seconds and millisecond. You don't even need a timer.
If you want to try this out, i would suggest to first make a copy backup of your current script in case it does not work out how you want it.

//top of script, this will replace RaceMinutes[MAX_PLAYERS], RaceSeconds[MAX_PLAYERS], RaceMiliSeconds[MAX_PLAYERS]:


new RaceStartTime[MAX_PLAYERS];


//In you playerinfo-enum you replace the SultanMinutes/Seconds/Miliseconds with this:
SultanRecord;

Like i said, you don't need a timer anymore, we'll just store the current tickcount when the race starts, get the tickcount again when the race ends and the difference between those 2 numbers is the amount of milliseconds:

So, when the race starts, instead of starting a timer you do this:
RaceStartTime[playerid] = tickcount();

Then, when the player enters the last checkpoint you save the racingtime in a new variable:

else if (CP[playerid] == 11)
{
new racetime = (tickcount() - RaceStartTime[playerid]); //now we have the amount of milliseconds the player raced.
new minutes, seconds, milliseconds, string[128];
ConvertRacetime(racetime, minutes, seconds, milliseconds); //The racetime is now converted to minutes, seconds and milliseconds.
format(string, sizeof(string),"[Mission]: You ended Sultan missioni. Time: %02d:%02d:%03d.", minutes, seconds, milliseconds);
SendClientMessage(playerid, -1, string);
DisablePlayerCheckpoint(playerid);
SendClientMessage(playerid, -1, "[Mission]: Sultan mission is completed! You get +$1200 ja +2 score!");
GivePlayerCash(playerid, 1200);
SetPlayerScore(playerid, GetPlayerScore(playerid) + 2);
Missioonil[playerid] = 0;
TextDrawHideForPlayer(playerid, Race[playerid]);

if((racetime < pInfo[playerid][SultanRecord]) && (pInfo[playerid][SultanRecord] != 0))
{
pInfo[playerid][SultanRecord] = racetime;
SavePlayer(playerid);
format(string, sizeof(string),"[Mission]: This is your new record: %02d:%02d:%03d.", minutes, seconds, milliseconds);
SendClientMessage(playerid, -1, string);
}
else
{
ConvertRacetime(pInfo[playerid][SultanRecord], minutes, seconds, milliseconds);
format(string, sizeof(string),"[Mission]: Your best time is: %02d:%02d:%03d.", minutes, seconds, milliseconds);
SendClientMessage(playerid, -1, string);
}
}

And last you have to add the ConvertRacetime to your gamemode (@ bottom of script for example):


stock ConvertRacetime(racetime,&minutes,&seconds,&milliseconds)
{
if (racetime<0) return false;
seconds = racetime / 1000; milliseconds = racetime % 1000;
minutes = seconds / 60; seconds = seconds % 60;
return 1;
}

I hope this works for you!

finelaq
20/01/2015, 11:40 AM
For me the whole minutes, seconds & milliseconds is pretty confusing, you might want to consider using just 1 variable (milliseconds) to keep the time of the current race and the record. And use a converter-function to convert milliseconds to minutes, seconds and millisecond. You don't even need a timer.
If you want to try this out, i would suggest to first make a copy backup of your current script in case it does not work out how you want it.

//top of script, this will replace RaceMinutes[MAX_PLAYERS], RaceSeconds[MAX_PLAYERS], RaceMiliSeconds[MAX_PLAYERS]:


new RaceStartTime[MAX_PLAYERS];


//In you playerinfo-enum you replace the SultanMinutes/Seconds/Miliseconds with this:
SultanRecord;

Like i said, you don't need a timer anymore, we'll just store the current tickcount when the race starts, get the tickcount again when the race ends and the difference between those 2 numbers is the amount of milliseconds:

So, when the race starts, instead of starting a timer you do this:
RaceStartTime[playerid] = tickcount();

Then, when the player enters the last checkpoint you save the racingtime in a new variable:

else if (CP[playerid] == 11)
{
new racetime = (tickcount() - RaceStartTime[playerid]); //now we have the amount of milliseconds the player raced.
new minutes, seconds, milliseconds, string[128];
ConvertRacetime(racetime, minutes, seconds, milliseconds); //The racetime is now converted to minutes, seconds and milliseconds.
format(string, sizeof(string),"[Mission]: You ended Sultan missioni. Time: %02d:%02d:%03d.", minutes, seconds, milliseconds);
SendClientMessage(playerid, -1, string);
DisablePlayerCheckpoint(playerid);
SendClientMessage(playerid, -1, "[Mission]: Sultan mission is completed! You get +$1200 ja +2 score!");
GivePlayerCash(playerid, 1200);
SetPlayerScore(playerid, GetPlayerScore(playerid) + 2);
Missioonil[playerid] = 0;
TextDrawHideForPlayer(playerid, Race[playerid]);

if(racetime < pInfo[playerid][SultanRecord]) // Check if the player broke the record:
{
pInfo[playerid][SultanRecord] = racetime;
SavePlayer(playerid);
format(string, sizeof(string),"[Mission]: This is your new record: %02d:%02d:%03d.", minutes, seconds, milliseconds);
SendClientMessage(playerid, -1, string);
}
else
{
ConvertRacetime(pInfo[playerid][SultanRecord], minutes, seconds, milliseconds);
format(string, sizeof(string),"[Mission]: Your best time is: %02d:%02d:%03d.", minutes, seconds, milliseconds);
SendClientMessage(playerid, -1, string);
}
}

And last you have to add the ConvertRacetime to your gamemode (@ bottom of script for example):


stock ConvertRacetime(racetime,&minutes,&seconds,&milliseconds)
{
if (racetime<0) return false;
seconds = racetime / 1000; milliseconds = racetime % 1000;
minutes = seconds / 60; seconds = seconds % 60;
return 1;
}

I hope this works for you!

Hmm... I cant get this work :(
When i finish the race it tell me:
"You ended Sultan missioni. Time: 0:0:540
and "Your best time is: 0:0:0"

And what i have to type here, when player register account:

INI_WriteInt(file,"SultanRecord",0);
INI_WriteInt(file,"SabreRecord",0);

Schneider
20/01/2015, 02:22 PM
Are you sure you aren't running the old backup?
Because even if it isn't showing the correct time, it should show the minutes and seconds with 2 digits:
00:00:540 instead of 0:0:540

Edit: I noticed it is causing problems when there is no record set yet, I'm working on a fix right now.

Edit2: Here, replace if(racetime < SultanRecord[playerid]) with:
if((racetime < SultanRecord[playerid]) || (SultanRecord[playerid] == 0))

finelaq
21/01/2015, 11:42 AM
Okay so now it's working but not correctly.
When i end the race it tell me:
"You ended race bla bla: 00:00:540"
And "Your best time is: 00:00:540"
Now if i race aigan and then i get time 420
then it tell me "This is your new record: 00:00:420"
But i race 40 seconds not 420 milliseconds.

I think im not going to get this work...

So this is the code:


//When finish
else if (CP[playerid] == 11)
{
new racetime = (tickcount() - RaceStartTime[playerid]); //now we have the amount of milliseconds the player raced.
new minutes, seconds, milliseconds, string[128];
ConvertRacetime(racetime, minutes, seconds, milliseconds); //The racetime is now converted to minutes, seconds and milliseconds.
format(string, sizeof(string),"[Mission]: You ended Sultan missioni. Time: %02d:%02d:%03d.", minutes, seconds, milliseconds);
SendClientMessage(playerid, -1, string);
DisablePlayerCheckpoint(playerid);
SendClientMessage(playerid, -1, "[Mission]: Sultan mission is completed! You get +$1200 ja +2 score!");
GivePlayerCash(playerid, 1200);
SetPlayerScore(playerid, GetPlayerScore(playerid) + 2);
Missioonil[playerid] = 0;
TextDrawHideForPlayer(playerid, Race[playerid]);

if(racetime < pInfo[playerid][SultanRecord]) // Check if the player broke the record:
{
pInfo[playerid][SultanRecord] = racetime;
SavePlayer(playerid);
format(string, sizeof(string),"[Mission]: This is your new record: %02d:%02d:%03d.", minutes, seconds, milliseconds);
SendClientMessage(playerid, -1, string);
}
else
{
ConvertRacetime(pInfo[playerid][SultanRecord], minutes, seconds, milliseconds);
format(string, sizeof(string),"[Mission]: Your best time is: %02d:%02d:%03d.", minutes, seconds, milliseconds);
SendClientMessage(playerid, -1, string);
}
}

//When start
SetPlayerCheckpoint(playerid, -2365.8149,1082.3146,55.2833, 7.0);
SendClientMessage(playerid, -1,"[Mission]: Sultan mission has started!");
SetVehiclePos(GetPlayerVehicleID(playerid), -2363.4282,1088.5339,55.2364);
SetVehicleZAngle(GetPlayerVehicleID(playerid), 158.3996);
TextDrawShowForPlayer(playerid, Race[playerid]);
RaceStartTime[playerid] = tickcount();

//And these
stock SavePlayer(playerid)
{
new INI:file = INI_Open(Path(playerid));
INI_SetTag(file,"Player's Data");
INI_WriteInt(file,"AdminLevel",pInfo[playerid][Adminlevel]);
INI_WriteInt(file,"VIPLevel",pInfo[playerid][VIPlevel]);
INI_WriteInt(file,"Money",GetPlayerMoney(playerid));
INI_WriteInt(file,"Scores",GetPlayerScore(playerid));
INI_WriteInt(file,"Kills",pInfo[playerid][Kills]);
INI_WriteInt(file,"Deaths",pInfo[playerid][Deaths]);
INI_WriteInt(file,"Meetrid",pInfo[playerid][Meetrid]);
INI_WriteInt(file,"Kilomeetrid",pInfo[playerid][Kilomeetrid]);
INI_WriteInt(file,"SultanRecord",pInfo[playerid][SultanRecord]);
INI_Close(file);
}
stock ConvertRacetime(racetime,&minutes,&seconds,&milliseconds)
{
if (racetime<0) return false;
seconds = racetime / 1000; milliseconds = racetime % 1000;
minutes = seconds / 60; seconds = seconds % 60;
return 1;
}


I don't know.. im not going to do this system. But Thanks for helping me! I appreciate it!

Schneider
21/01/2015, 01:34 PM
I see you haven't applied the fix yet I posted in the post above you (although that's only for cases where the records hasn't been set yet and it shouldn't affect the display of the racetime.

I'm baffled about this, i added the same code to my test server. (Because I don't have your race-system I just create 1 random checkpoint and drive to it) but it displays the race-time, as well as the record as it should... (see screenshot below).

Are you sure the value of RaceStartTime[playerid] is not getting changed somewhere else in your script?

http://i61.tinypic.com/34gooly.jpg

Here is the full script I used to test this:
http://pastebin.com/jFjrsVTE

finelaq
21/01/2015, 03:47 PM
So now i tryed on localhost and its working!
But if i upload this in linux server then it's not working -.-
Thanks for your help!

Schneider
21/01/2015, 04:19 PM
Ok, I don't know, maybe the tickcount();-function works differently on Linux?