Speedo Help again :/
#1

Alright I had made a speedo. When in game is says Speed: 0 m/h. I cant get it to update the speed. Heres the code.
In OnGameModeInit
pawn Код:
for(new i = 0; i < MAX_PLAYERS; i++)
    {
        Speedo = TextDrawCreate(506.000000,351.000000,"_");

        SetTimer("Speedometer",1000,true);
    }
pawn Код:
forward Speedometer(playerid);
public Speedometer(playerid)
{
    new Float:VehVelocity[3];
    new State = GetPlayerState(playerid);
    if(State == PLAYER_STATE_DRIVER && IsPlayerInAnyVehicle(playerid))
    {
        #define MULTIPLY_SPEED  4

        GetPlayerVelocity(playerid, VehVelocity[0], VehVelocity[1], VehVelocity[2]);
        new Float:speed = floatsqroot(VehVelocity[0] * VehVelocity[0] + VehVelocity[1] * VehVelocity[1] + VehVelocity[2] * VehVelocity[2]);

        new Sspeed[15], string[200];
       
        if( speed == 0) {
            format(Sspeed,sizeof(Sspeed),"~G~%dm/h",(floatround(speed)*MULTIPLY_SPEED));
        } else if( speed >= 1 && speed <= 65 ){
            format(Sspeed,sizeof(Sspeed),"~G~%dm/h",(floatround(speed)*MULTIPLY_SPEED));
        } else if( speed >= 66 && speed <= 120 ){
            format(Sspeed,sizeof(Sspeed),"~Y~%dm/h",(floatround(speed)*MULTIPLY_SPEED));
        } else {
            format(Sspeed,sizeof(Sspeed),"~R~%dm/h",(floatround(speed)*MULTIPLY_SPEED));
        }

        format(string,sizeof(string),"Speed: %s", Sspeed);
        TextDrawSetString(Speedo, string);
        TextDrawShowForPlayer(playerid, Speedo);
    }
    else
    {
        //TextDrawHideForPlayer(playerid, Speedo[playerid]);
    }
    return true;
}
Reply
#2

The first thing I notice is that your Speedometer function has one parameter: playerid. However, when you set your timer up to call the function, it does not supply the playerid. The first step would be to modify this:
pawn Код:
SetTimer("Speedometer",1000,true);
...to this:
pawn Код:
SetTimerEx("Speedometer",1000,true,"i",i);
Here is the wiki entry for SetTimerEx - https://sampwiki.blast.hk/wiki/SetTimerEx

I haven't looked too much into the rest, but give my suggestion a go and see what happens. Good luck!
Reply
#3

use settimer by using some cmds
Reply
#4

I put in SetTimerEx but it still didn't work. Same problem. Speed isn't showing up.
Reply
#5

Quote:
Originally Posted by Iphone1234g
Посмотреть сообщение
use settimer by using some cmds
Did you even read the topic?

Ontopic: You can try setting the timer once a player gets in a vehicle (OnPlayerEnterVehicle or OnPlayerStateChange) and then starting the timer, But I am not sure if that will work. Doesn't require a loop either

Also, I'm not sure a loop works in OnGameModeInit, as there are probably no players at OnGameModeInit, you should try it the way above or putting the code under OnPlayerConnect.
Reply
#6

It still didn't work after trying what you said. Just shows 0 mph.
Reply
#7

I've just spotted this: you're getting the players velocity (GetPlayerVelocity), shouldn't you use GetVehicleVelocity?
Reply
#8

Heres the new code. After awhile it said I was going 4 MPH. Idk what is wrong. I also get an error if i uncomment the TextDrawHide line.
Код:
C:\Users\zack\Desktop\SAMP03CSVR_WIN32 (1)\gamemodes\MYSQLgm.pwn(552) : error 028: invalid subscript (not an array or too many subscripts): "Speedo"
C:\Users\zack\Desktop\SAMP03CSVR_WIN32 (1)\gamemodes\MYSQLgm.pwn(552) : warning 215: expression has no effect
C:\Users\zack\Desktop\SAMP03CSVR_WIN32 (1)\gamemodes\MYSQLgm.pwn(552) : error 001: expected token: ";", but found "]"
C:\Users\zack\Desktop\SAMP03CSVR_WIN32 (1)\gamemodes\MYSQLgm.pwn(552) : error 029: invalid expression, assumed zero
C:\Users\zack\Desktop\SAMP03CSVR_WIN32 (1)\gamemodes\MYSQLgm.pwn(552) : fatal error 107: too many error messages on one line
pawn Код:
forward Speedometer(playerid);
public Speedometer(playerid)
{
    new Float:VehVelocity[3];
    new State = GetPlayerState(playerid);
    if(State == PLAYER_STATE_DRIVER && IsPlayerInAnyVehicle(playerid))
    {
        #define MULTIPLY_SPEED  4
        new vID = GetPlayerVehicleID(playerid);
        GetVehicleVelocity(vID, VehVelocity[0], VehVelocity[1], VehVelocity[2]);
        new Float:speed = floatsqroot(VehVelocity[0] * VehVelocity[0] + VehVelocity[1] * VehVelocity[1] + VehVelocity[2] * VehVelocity[2]);
        new Sspeed[15], string[200];
       
        if( speed == 0) {
            format(Sspeed,sizeof(Sspeed),"~G~%dm/h",(floatround(speed)*MULTIPLY_SPEED));
        } else if( speed >= 1 && speed <= 65 ){
            format(Sspeed,sizeof(Sspeed),"~G~%dm/h",(floatround(speed)*MULTIPLY_SPEED));
        } else if( speed >= 66 && speed <= 120 ){
            format(Sspeed,sizeof(Sspeed),"~Y~%dm/h",(floatround(speed)*MULTIPLY_SPEED));
        } else {
            format(Sspeed,sizeof(Sspeed),"~R~%dm/h",(floatround(speed)*MULTIPLY_SPEED));
        }

        format(string,sizeof(string),"Speed: %s", Sspeed);
        TextDrawSetString(Speedo, string);
        TextDrawShowForPlayer(playerid, Speedo);
    }
    else
    {
        //TextDrawHideForPlayer(playerid, Speedo[playerid]);
    }
    return true;
}
Reply
#9

Quote:
Originally Posted by futuretrucker
Посмотреть сообщение
In OnGameModeInit
pawn Код:
for(new i = 0; i < MAX_PLAYERS; i++)
    {
        Speedo = TextDrawCreate(506.000000,351.000000,"_");

        SetTimer("Speedometer",1000,true);
    }
Okay, you do realize that if someone is not connected when the game mode initializes the speedometer text-draw is not going to create? It would be better to perform the functions (excluding the loop) under ...

pawn Код:
public OnPlayerConnect(playerid)
... as this way it will create the text-draw and set the timer for anyone who connects after the server is initialized.
Reply
#10

Yeh what RealCop said ^

Although that is a more efficient way to do it (and is something you should do!), it won't actually fix the current problems you are having. You haven't shown us the creation of the Speedo variable, which is quite important regarding the problem you are having with this line:
pawn Код:
//TextDrawHideForPlayer(playerid, Speedo[playerid]);
You have referenced Speedo as an array here (which I believe should be correct), but in all other cases you do not refer to Speedo as an array. Your initial creation of the Speedo variable should be at the top of your script and look something like this:
pawn Код:
new Text:Speedo[MAX_PLAYERS];
Once that is done, you can uncomment the line that I previously quoted, and then you will need to go back and change all other references to Speedo. So for example:
pawn Код:
TextDrawSetString(Speedo, string);
TextDrawShowForPlayer(playerid, Speedo);
will become:
pawn Код:
TextDrawSetString(Speedo[playerid], string);
TextDrawShowForPlayer(playerid, Speedo[playerid]);
Once you have changed all references to Speedo, your speedo should display to the correct player when in a vehicle driver, and hide itself otherwise.

The next problem you mention is that it only shows 0mph or 4mph. That sounds like a problem with the maths to me, and is unfortunately something I can't assist with! I'd suggest looking around other available speedo scripts to see exactly how the maths works there.

Good luck!
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)