Speedometer only works for ID 0
#1

I'm not sure what I'm doing wrong here, but it's probably something stupid that I'm missing. Any hints/tips/suggestions would be greatly appreciated. Thank you.

pawn Код:
new Text:Speedometer[MAX_PLAYERS];

forward Speedo(playerid);
OnGameModeInit:
pawn Код:
SetTimer("Speedo", 500, 1);
OnPlayerConnect:
pawn Код:
Speedometer[playerid] = TextDrawCreate(555, 420, " ");
Function:
pawn Код:
public Speedo(playerid) {

    new vehicleid,
        Float:speed_x,
        Float:speed_y,
        Float:speed_z,
        Float:final_speed,
        speed_string[256],
        final_speed_int,
        odo_string[256];
       
    vehicleid = GetPlayerVehicleID(playerid);
   
    if(vehicleid != 0) {
   
        GetVehicleVelocity(vehicleid, speed_x, speed_y, speed_z);
        final_speed = floatsqroot(((speed_x*speed_x)+(speed_y*speed_y))+(speed_z*speed_z))*125;
        final_speed_int = floatround(final_speed,floatround_round);
        format(speed_string,256,"%i MPH",final_speed_int);
        TextDrawSetString(Speedometer[playerid], speed_string);
       
    } else {
   
        TextDrawSetString(Speedometer[playerid], " ");

    }
   
    return 1;
   
}
OnPlayerStateChange:
pawn Код:
if(oldstate == PLAYER_STATE_ONFOOT && newstate == PLAYER_STATE_DRIVER) {
   
        TextDrawUseBox(Speedometer[playerid], 1);
        TextDrawBoxColor(Speedometer[playerid],0x00000066);
        TextDrawShowForPlayer(playerid, Speedometer[playerid]);
   
    }
   
    if(oldstate == PLAYER_STATE_DRIVER && newstate == PLAYER_STATE_ONFOOT) {
   
        TextDrawHideForPlayer(playerid, Speedometer[playerid]);

   
    }
Reply
#2

I just hate the way this language initializes variables to 0. It's just not user friendly.

Anyways, you used SetTimer instead of SetTimerEx, which ignores func Speedo parameters and initializes them as zero (in other words, sets the timer to work only with id 0). A possible solution for this would be using SetTimerEx, and setting the timer up with the corresponding parameters.

And I think that should solve your problem...
Reply
#3

pawn Код:
foreach(Player, i)
    {
        vehicleid = GetPlayerVehicleID(i);

        if(vehicleid != 0) {

            GetVehicleVelocity(vehicleid, speed_x, speed_y, speed_z);
            final_speed = floatsqroot(((speed_x*speed_x)+(speed_y*speed_y))+(speed_z*speed_z))*125;
            final_speed_int = floatround(final_speed,floatround_round);
            format(speed_string,256,"%i MPH",final_speed_int);
            TextDrawSetString(Speedometer[i], speed_string);

        } else {

            TextDrawSetString(Speedometer[i], " ");

        }


    }
    return 1;
}
If you don't use foreach replace it with
pawn Код:
for(new i = 0; i < MAX_PLAYERS; i++)
Reply
#4

Thanks for the quick response guys. I used waxhunter's method and kept everything the way it was but used the SetTimerEx function to carry over the param and it worked perfectly.

Thanks to both of you for your help!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)