SA-MP Forums Archive
GetTickCount() it's buggy? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: GetTickCount() it's buggy? (/showthread.php?tid=592002)



GetTickCount() it's buggy? - nGen.SoNNy - 19.10.2015

Hello guys, I'm trying to use GetTickCount() with a dealearship system on my server but i have some problems.

pawn Код:
#define DS_TIME 60000 // 1 Minute in miliseconds

// I'm using this when the player left his vehicle
PVeh[ slot ][ playerid ][ VehicleTimer ] = GetTickCount( ) + DS_TIME;

// All good but the car it's despawned right after the player left his vehicle...

// In this 1 second timer i'm checking if that 1 minute has been passed
public CheckCar( ) // It's runing once per seccond
{
    new TimeNow = GetTickCount( );

    foreach( Player, i )
    {
        if( GetPersonalVehiclesNumber( i ) > 0 )
        {
            for( new slot=0; slot<MAX_SLOTS; slot++ )
            {
                if( PVeh[ slot ][ i ][ VehicleStatus ] == 1 )
                {
                    if( PVeh[ slot ][ i ][ VehicleTimer ] - TimeNow < DS_TIME )
                    {
                        #if defined DEBUG
                            printf( "Vehicle[slot:%d] has been despanwned(C:%d / D:%d)", slot, GetTickCount( ), PVeh[ slot ][ i ][ VehicleTimer ] );
                        #endif
                        DeSpawnPlayerVehicle( slot, i );
                        InfoTD_MSG( i, 7000, "~r~~h~Personal Vehicle~n~~w~~h~Your ~g~~h~%s(%d) ~w~~h~has been despawned~n~~y~~h~Reason: ~w~~h~1 minute out of activity", VehicleNames[ PVeh[ slot ][ i ][ VehicleModelID ] - 400 ], slot );
                    }
                }
            }
        }
    }
}



Re: GetTickCount() it's buggy? - Threshold - 19.10.2015

pawn Код:
if( PVeh[ slot ][ i ][ VehicleTimer ] < TimeNow )
Also, you don't really need to use GetTickCount if you're going to be using a timer in seconds anyway. You should use gettime() or actually use SetTimerEx to perform a despawn directly.

Example:
pawn Код:
// When you want to start the despawn timer.
PVeh[slot][i][VehicleTimer] = SetTimerEx("DespawnVehicle", DS_TIME, false, "ii", slot, i);

// When you want to cancel the despawn timer.
KillTimer(PVeh[slot][i][VehicleTimer]);

forward DespawnVehicle(slot, vehicle);
public DespawnVehicle(slot, vehicle)
{
    if( PVeh[ slot ][ vehicle ][ VehicleStatus ] == 1 )
    {
        #if defined DEBUG
            printf( "Vehicle[slot:%d] has been despanwned(C:%d / D:%d)", slot, GetTickCount( ), PVeh[ slot ][ i ][ VehicleTimer ] );
        #endif
        DeSpawnPlayerVehicle( slot, i );
        InfoTD_MSG( i, 7000, "~r~~h~Personal Vehicle~n~~w~~h~Your ~g~~h~%s(%d) ~w~~h~has been despawned~n~~y~~h~Reason: ~w~~h~1 minute out of activity", VehicleNames[ PVeh[ slot ][ i ][ VehicleModelID ] - 400 ], slot );
        return 1;
    }
    return 0;
}
The timer should probably be called under OnPlayerStateChange when the player leaves the vehicle.


Re: GetTickCount() it's buggy? - nGen.SoNNy - 19.10.2015

I don't want to use a timer for about 5 cars per player in a server with 50+ players online that's why i'm using just one single timer for all using gettickcount. Thanks for your reply!


Re: GetTickCount() it's buggy? - Threshold - 19.10.2015

Looping through every single player, every second, is definitely not a better option.


Re: GetTickCount() it's buggy? - PrO.GameR - 19.10.2015

Well I'm not sure if it's buggy or not, but gettime(); is another way to do it, it gives you current time on seconds (not miliseconds so your DS_TIME would be a simple 60), as far as I know it's easier to work with gettime and btw you didn't even mention your problem


Re: GetTickCount() it's buggy? - nGen.SoNNy - 19.10.2015

Quote:
Originally Posted by PrO.GameR
Посмотреть сообщение
Well I'm not sure if it's buggy or not, but gettime(); is another way to do it, it gives you current time on seconds (not miliseconds so your DS_TIME would be a simple 60), as far as I know it's easier to work with gettime and btw you didn't even mention your problem
// All good but the car it's despawned right after the player left his vehicle...

I'm looping just in connected players and if the have any vehicle spawned...so it's still 1 timer not like 3 * 50 = 150 timers for each vehicle


Re: GetTickCount() it's buggy? - PrO.GameR - 19.10.2015

Edit:
just noticed that was a suggestion not your code
it is because you do this
PHP код:
PVehslot ][ playerid ][ VehicleTimer ] = GetTickCount( ) + DS_TIME;//changed with gettime() 
lets say it's 12:10:10 right now, I exit the veh, your variable saves as 12:11:10 which is current time + DS_TIME (1 min)
you recall it 1 sec later, 12:10:11, you check if 12:11:10-12:10:11 is less than 1 min, which obviously always is !
change that line into
PHP код:
PVehslot ][ playerid ][ VehicleTimer ] = gettime(); 
and change your
PHP код:
                    if( PVehslot ][ ][ VehicleTimer ] - TimeNow DS_TIME 
into
PHP код:

                    
if(TimeNow PVehslot ][ ][ VehicleTimer ]> DS_TIME 



Re: GetTickCount() it's buggy? - nGen.SoNNy - 19.10.2015

I just changed this in the "CheckCar" function and it's done

PHP код:
if( PVehslot ][ ][ VehicleTimer ] < TimeNow 



Re: GetTickCount() it's buggy? - PrO.GameR - 19.10.2015

Well thats another way to do it xD
well done, glad I could help.


Re: GetTickCount() it's buggy? - Threshold - 20.10.2015

That's exactly what I said to do :/