Strange bug - Speedometer & fuel update too fast.
#1

When I spawn a vehicle (any) the speedometer updates really fast and the fuel - which is set to update every 20 seconds updates in around 5. it's very strange, yet when I restart the server; (the vehicles load from mysql) and it works perfectly, can anyone see anything that might suggest why this happens?

pawn Код:
public SpeedoUpdate()
{
    for(new i = 0;i<MAX_PLAYERS;i++)
    {
        if(IsPlayerConnected(i) && IsPlayerInAnyVehicle(i))
        {
            new Float:x,Float:y,Float:z,string[24],vehicleid = GetPlayerVehicleID(i);
            if(GetPlayerState(i) == PLAYER_STATE_DRIVER)
            {
                TextDrawShowForPlayer(i,speed[i]);
                TextDrawShowForPlayer(i,fuel[i]);
            }
            GetVehicleVelocity(vehicleid,x,y,z);
            format(string,sizeof(string),"~g~Speed: ~w~%d mph",floatround(floatsqroot(((x*x)+(y*y))+(z*z))*156.666667*0.641371192));
            TextDrawSetString(speed[i],string);
            format(string,sizeof(string),"~g~Fuel: ~w~%d%%",VehFuel[vehicleid]);
            TextDrawSetString(fuel[i],string);
        }
        if(!IsPlayerInAnyVehicle(i))
        {
            TextDrawHideForPlayer(i,speed[i]);
            TextDrawHideForPlayer(i,fuel[i]);
        }
    }
}
public FuelUpdate()
{
    foreach(Player, p)
    {
    for(new i = 1;i<MAX_VEHICLES;i++)
    {
        if(GetVehicleModel(i))
        {
            GetVehicleParamsEx(i,engine,lights,alarm,doors,bonnet,boot,objective);
            if(engine == 1)
            {
                if(VehFuel[i] > 0)
                {
                    VehFuel[i]--;
                    Fuel = VehFuel[i];
                }
                else if(VehFuel[pvehicle] == 0)
                {
                    EngineStartStatus[i] = 0;
                    SetVehicleParamsEx(i,0,lights,alarm,doors,bonnet,boot,objective);
                    GameTextForPlayer(p, "~r~out of fuel", 3000, 5);
                    SendClientMessage(p, COLOUR_REALRED, "Your vehicle has run out of fuel.");
                        }
                   
                    }
                }
        }
    }
}
Reply
#2

Wheres the timer?
Reply
#3

Here:

pawn Код:
CMD:eon(playerid, params[])
{
    if(LoggedIn[playerid] == 0)return SendClientMessage(playerid, COLOUR_GREY, "You must be logged in to use this command.");
    new string[128];
    new vehicleid = GetPlayerVehicleID(playerid);
    new name = GetVehicleModel(vehicleid) - 400;
    if(GetPlayerState(playerid) != PLAYER_STATE_DRIVER)return SendClientMessage(playerid, COLOUR_GREY, "You are not the driver.");
    if(EngineStartStatus[vehicleid] == 1)return SendClientMessage(playerid, COLOUR_GREY, "The engine is already on.");
    if(!IsPlayerInAnyVehicle(playerid))return SendClientMessage(playerid, COLOUR_GREY, "You are not in a vehicle.");

    SetTimer("SpeedoUpdate",300,1);
    SetTimer("FuelUpdate",20000,1);
    SetTimer("FuelSave", 4000, 1);
   
    if(VehFuel[vehicleid] > 0)
    {
        EngineStartStatus[vehicleid] = 1;
        GameTextForPlayer(playerid, "~g~Engine starting...", 2500, 5);
        SetTimerEx("EngineOn", 3000, false, "i", playerid);
        format(string, sizeof(string), "* %s turns the %s's ignition key...", GetNameEx(playerid), VehicleNames[name]);
        ProxDetector(30.0, playerid, string, COLOUR_PURPLE, COLOUR_PURPLE, COLOUR_PURPLE, COLOUR_PURPLE, COLOUR_PURPLE);
    }
    else if(VehFuel[vehicleid] == 0)
    {
        EngineStartStatus[vehicleid] = 0;
        SendClientMessage(playerid, COLOUR_REALRED, "Your vehicle is out of fuel..");
        format(string, sizeof(string), "* The %s has run out of fuel and failed to start.", VehicleNames[name]);
        ProxDetector(30.0, playerid, string, COLOUR_PURPLE, COLOUR_PURPLE, COLOUR_PURPLE, COLOUR_PURPLE, COLOUR_PURPLE);
    }
    return 1;
}
Reply
#4

Each time someone does /eon, it starts the timer(s) again. Therefore, if you have a timer being called every 300ms, then you start the timer AGAIN, it's going to be called twice every 300ms- which could get salty.

Just make a per-player timer using SetTimerEx.
Reply
#5

AHH! I kind of understand could you show me what you mean?
I have two more issues that I need your professional help with- It's things that could help me in the future also (if you would please!): https://sampforum.blast.hk/showthread.php?tid=312785
https://sampforum.blast.hk/showthread.php?tid=312960
Reply
#6

pawn Код:
SetTimer("SpeedoUpdate",300,1);
    SetTimer("FuelUpdate",20000,1);
    SetTimer("FuelSave", 4000, 1);
Make those timers like this one:

pawn Код:
SetTimerEx("EngineOn", 3000, false, "i", playerid);
Which is basically what person above me said.
Reply
#7

Ahh thank you very much!! I understand now!
Reply
#8

Your extremely welcome!
Reply
#9

When I /eon in one vehicle and then go to another, the timers still count down quicker than they should (Speed and Fuel) although I've added the SetTimerEx.

Any advice?
Reply
#10

There's your problem:

pawn Код:
SetTimer("SpeedoUpdate",300,1);
You make public functions for fuel and speed, but you make another function, that updates those two functions faster, then your these two:

pawn Код:
SetTimer("FuelUpdate",20000,1);
    SetTimer("FuelSave", 4000, 1);
pawn Код:
SetTimer("SpeedoUpdate", 10000,1); // A 10 sec. timer update for fuel and speed.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)