[Tutorial] Velocity, Health system
#1

Velocity Sytem

This will be a velocity system which shows you the current velocity and health.

Structure

1.Creating Public Variables
2.Creating the PlayerTextDraw
3.Creating a timer and updating the vehicle
4.The full code

1. Creating Public Variables

First we have to create the variable for the PlayerTextDraw.
This has to be a PlayerTextDraw and non TextDraw because every player has a different velocity.

Make sure that you have defined MAX_PLAYERS as your true limit of players.
If you don't know how that works I'll show you:
pawn Code:
#undef MAX_PLAYERS // Undefines MAX_PLAYERS
#define MAX_PLAYERS YourTrueLimitOfPlayer // Setting th MAX_PLAYERS to your true limit, but you can't write that you have to put your value for YourTrueLimitOfPlayer in
If we done that we have to create the PlayerTextDraw variable:
pawn Code:
new PlayerText:Velocity[MAX_PLAYERS];
Note that that is not the variable for the Velocity, it is the Variable for the whole TextDraw.

That is the only public variable we have to create.

Lets go to the next thing:

2. Creating the PlayerTextDraw

The first thing we have to do is going to the function: OnPlayerConnect(playerid)
In that we will create the PlayerTextDraw and we'll set some coordinates for it.

First thing we have to do is creating the TextDraw with using the function: CreatePlayerTextDraw(playerid,x,y,Text[]);
The return value is the PlayerTextDraw value.
Lets insert our specific variables:

pawn Code:
Velocity[playerid] = CreatePlayerTextDraw(playerid,520,320,"_~N~Velocity: 0~N~Health: 0~N~_");
We use ~n~ for a new line.
520 and 530 are the x and y coordinates.
The resolution is here always 640 (x) * 480 (y)
At the left top x = 0; y = 0;
So at the bottom right x = 640; y = 480;

Now we have to set the width and height of the box with using the function: PlayerTextDrawTextSize(playerid,PlayerTextDraw,x,y );
In this function x and y are the ends of the box.
We have to set the width of the box to 110 if it has to fit, so we have to set x[2] = x[1] + 110 = 630;
x[2] is the ending of the box and x[1] is the starting of the box.
We don't have to change the y value so we set it to 0;
So the code is:
pawn Code:
PlayerTextDrawTextSize(playerid,Velocity[playerid],630,0);
We want that our Text is in a box so we write:
pawn Code:
PlayerTextDrawUseBox(playerid,Velocity[playerid],1);
The first parameter is just the playerid and the second one is the PlayerTextDraw, the last:
1 if it has to be shown
0 if it has to be hidden

If we want to change the box's color we have to use the function: PlayerTextDrawBoxColor(playerid,PlayerTextDraw,col or);
I use the color 0x00000066, it's a semi transparent black.
So we put in the required values:
pawn Code:
PlayerTextDrawBoxColor(playerid,Velocity[playerid],0x00000066);
The last thing we have do write in OnPlayerConnect(playerid); is that the box is not shown if you connect.
We will use the function: PlayerTextDrawHide(playerid,PlayerTextDraw);
If we put in our PlayerTextDraw variable it seems like that:
pawn Code:
PlayerTextDrawHide(playerid,Velocity[playerid]);
Everything together:
pawn Code:
public OnPlayerConnect(playerid)
{
    Velocity[playerid] = CreatePlayerTextDraw(playerid,520,320,"_~N~Velocity: 0~N~Health: 0~N~_");
    PlayerTextDrawTextSize(playerid,Velocity[playerid],630,0);
    PlayerTextDrawUseBox(playerid,Velocity[playerid],1);
    PlayerTextDrawBoxColor(playerid,Velocity[playerid],0x00000066);
    PlayerTextDrawHide(playerid,Velocity[playerid]);
    return 1;
}


3. Creating a timer and updating the vehicle

We want that the Health and Velocity updates every half second, so we have to use a Timer.
If we want to create a timer we have to create a function.
So first we forward a update function:
pawn Code:
forward VehicleUpdate();
Now we are able to start the timer with using the function: SetTimer(function[],interval,repeating);
If repeating is set to 0 the function starts only once.
If repeating is set to 1 the function starts every interval.

The interval is in milliseconds. (1000 ms = 1s)

Let's put our values into it:
pawn Code:
SetTimer("VehicleUpdate",1*500,1);
Now we go to the main code of the system, the VehicleUpdate() function

Let's start with a simple for loop which has to update the velocity and health from every player.

pawn Code:
public VehicleUpdate()
{
    for(new i = 0;i < MAX_PLAYERS; i++)
    {
       
    }
    return 1;
}
Now we have to create a query if the player is in any vehicle or not.
We use the function IsPlayerInAnyVehicle(playerid);
If it returns 0 nothing has to be shown, if it returns 1 it should show the PlayerTextDraw:
pawn Code:
public VehicleUpdate()
{
    for(new i = 0;i < MAX_PLAYERS; i++) // i is the playerid
    {
        if(IsPlayerInAnyVehicle(i) == 1) // You could also write if(IsPlayerInAnyVehicle(i))
        {
        }
        else if(!IsPlayerInAnyVehicle(i)) // You could also write else if(IsPlayerInAnyVehicle(i) == 0))
        {
            PlayerTextDrawHide(i,Velocity[i]); // The TextDraw has to be hidden
        }
    }
    return 1;
}
Now we have to find out the velocity of the car.
We will use the function: GetVehicleVelocity(vehicleid,Float,Float:y,Float :z);
But first we want to create a variable for the player's vehicle so we use the function: GetPlayerVehicleID(playerid);
pawn Code:
new vehicleid = GetPlayerVehicleID(i);
The function GetPlayerVehicleVelocity(); is not for getting the mph or kph its for getting the speed in the x, y and z direction.
We are going to create a simple GetMph(vehicleid) stock.
A stock is like a public function only that you don't have you forward it.
pawn Code:
stock GetMph(vehicleid)
{
   
}
Now we need the formula to find out the mph.
It's floatsqroot((x*x)+(y*y)+(z*z))*156.666667;
pawn Code:
stock GetMph(vehicleid)
{
    new Float:x,Float:y,Float:z; // Creating Float variables
    GetVehicleVelocity(vehicleid,x,y,z); // Writing in the x,y,z coordinates the speed values.
    return floatround(floatsqroot((x*x)+(y*y)+(z*z))*156.666667); // It returns the mph of the vehicle, floatround converts a float into a integer
}
Now we have to use our function in our main function:
pawn Code:
public VehicleUpdate()
{
    for(new i = 0;i < MAX_PLAYERS; i++) // i is the playerid
    {
        if(IsPlayerInAnyVehicle(i) == 1) // You could also write if(IsPlayerInAnyVehicle(i))
        {
            new vehicleid = GetPlayerVehicleID(i); // Getting the vehicleid, and write it into the vehicleid variable
            new mph = GetMph(vehicleid); // Using our function to get the mph velocity of our vehicle  
        }
        else if(!IsPlayerInAnyVehicle(i)) // You could also write else if(IsPlayerInAnyVehicle(i) == 0))
        {
            PlayerTextDrawHide(i,Velocity[i]); // The TextDraw has to be hidden
        }
    }
    return 1;
}
Now we have to find out the health of the car.
We use the function: GetVehicleHealth(vehicleid,health);
The return value is the health.
It returns 1000 if the car hasn't got any damage.
It returns 250 if the car starts burning.

pawn Code:
new Float:health;
GetVehicleHealth(vehicleid,health); //writing in health the health of the vehicle
Now we have to edit our PlayerTextDraw.
First we create a string.
pawn Code:
new string[256];
The next thing we want to do is writing in the string the health and the velocity with using the function: format(string,len,string(,..,...,...));
Let's do it:
pawn Code:
format(string,sizeof(string),"_~N~Velocity: %i~N~Health: %i%%~N~_",mph,floatround(health / 10)); // %i if you want to write an integer variable into the string, %% if you want to write a %, we want to show health with per cents so we have to to 'health / 10'
Now we have to put that string into the PlayerTextDraw by using the function: PlayerTextDrawSetString(playerid,PlayerTextDraw,st ring);
If we want to use it we have to put our variables into it:
pawn Code:
PlayerTextDrawSetString(i,Velocity[i],string);
The last thing we want to do is showing the PlayerTextDraw by using the function: PlayerTextDrawShow(playerid,PlayerTextDraw);
Now we put in out variables:
pawn Code:
PlayerTextDrawShow(i,Velocity[i]);
Now we put in the last things we did into our main function:
pawn Code:
public VehicleUpdate()
{
    for(new i = 0;i < MAX_PLAYERS; i++) // i is the playerid
    {
        if(IsPlayerInAnyVehicle(i) == 1) // You could also write if(IsPlayerInAnyVehicle(i))
        {
            new vehicleid = GetPlayerVehicleID(i); // Getting the vehicleid, and write it into the vehicleid variable
            new mph = GetMph(vehicleid); // Using our function to get the mph velocity of our vehicle  
            new Float:health;
GetVehicleHealth(vehicleid,health); //Getting the vehicle's health
            new string[256];
            format(string,sizeof(string),"_~N~Velocity: %i~N~Health: %i%%~N~_",mph,floatround(health / 10)); // %i if you want to write an integer variable into the string, %% if you want to write a %, we want to show health with per cents so we have to to 'health / 10'
            PlayerTextDrawSetString(i,Velocity[i],string); // setting the PlayerTextDraw string
            PlayerTextDrawShow(i,Velocity[i]); // Showing the TextDraw to the player i
        }
        else if(!IsPlayerInAnyVehicle(i)) // You could also write else if(IsPlayerInAnyVehicle(i) == 0))
        {
            PlayerTextDrawHide(i,Velocity[i]); // The TextDraw has to be hidden
        }
    }
    return 1;
}
Now we have to make a query if the player is connected.
We use the function: IsPlayerConnected(playerid);
If it's 0 it has to hide the textdraw

pawn Code:
public VehicleUpdate()
{
    for(new i = 0;i < MAX_PLAYERS; i++) // i is the playerid
    {
        if(IsPlayerConnected(i)) // the same as if(IsPlayerConnected(i) == 1)
        {
            if(IsPlayerInAnyVehicle(i) == 1) // You could also write if(IsPlayerInAnyVehicle(i))
            {
                new vehicleid = GetPlayerVehicleID(i); // Getting the vehicleid, and write it into the vehicleid variable
                new mph = GetMph(vehicleid); // Using our function to get the mph velocity of our vehicle  
                new Float:health;
GetVehicleHealth(vehicleid,health); //writing in health the health of the vehicle
                new string[256];
                format(string,sizeof(string),"_~N~Velocity: %i~N~Health: %i%%~N~_",mph,floatround(health / 10)); // %i if you want to write an integer variable into the string, %% if you want to write a %, we want to show health with per cents so we have to to 'health / 10'
                PlayerTextDrawSetString(i,Velocity[i],string); // setting the PlayerTextDraw string
                PlayerTextDrawShow(i,Velocity[i]); // Showing the TextDraw to the player i
            }
            else if(!IsPlayerInAnyVehicle(i)) // You could also write else if(IsPlayerInAnyVehicle(i) == 0))
            {
                PlayerTextDrawHide(i,Velocity[i]); // The TextDraw has to be hidden
            }
        }
        else if(!IsPlayerConnected(i)) //same as if(IsPlayerConnected(i) == 0)
        {
            PlayerTextDrawHide(i,Velocity[i]); // The TextDraw has to be hidden
        }
    }
    return 1;
}
4. The full code
Here you can see the full code:
pawn Code:
#undef MAX_PLAYERS // Undefines MAX_PLAYERS
#define MAX_PLAYERS YourTrueLimitOfPlayer // Setting th MAX_PLAYERS to your true limit, but you can't write that you have to put your value for YourTrueLimitOfPlayer in

new PlayerText:Velocity[MAX_PLAYERS];

forward VehicleUpdate(); // forwarding VehicleUpdate();

public OnGameModeInit()
{
    SetTimer("VehicleUpdate",1*500,1);
    return 1;
}

public OnPlayerConnect(playerid)
{
    Velocity[playerid] = CreatePlayerTextDraw(playerid,520,320,"_~N~Velocity: 0~N~Health: 0~N~_");
    PlayerTextDrawTextSize(playerid,Velocity[playerid],630,0);
    PlayerTextDrawUseBox(playerid,Velocity[playerid],1);
    PlayerTextDrawBoxColor(playerid,Velocity[playerid],0x00000066);
    PlayerTextDrawHide(playerid,Velocity[playerid]);
    return 1;
}
stock GetMph(vehicleid)
{
    new Float:x,Float:y,Float:z; // Creating Float variables
    GetVehicleVelocity(vehicleid,x,y,z); // Writing in the x,y,z coordinates the speed values.
    return floatround(floatsqroot((x*x)+(y*y)+(z*z))*156.666667); // It returns the mph of the vehicle
}
public VehicleUpdate()
{
    for(new i = 0;i < MAX_PLAYERS; i++) // i is the playerid
    {
        if(IsPlayerConnected(i)) // the same as if(IsPlayerConnected(i) == 1)
        {
            if(IsPlayerInAnyVehicle(i) == 1) // You could also write if(IsPlayerInAnyVehicle(i))
            {
                new vehicleid = GetPlayerVehicleID(i); // Getting the vehicleid, and write it into the vehicleid variable
                new mph = GetMph(vehicleid); // Using our function to get the mph velocity of our vehicle  
                new Float:health;
GetVehicleHealth(vehicleid,health); //writing in health the health of the vehicle
                new string[256];
                format(string,sizeof(string),"_~N~Velocity: %i~N~Health: %i%%~N~_",mph,floatround(health / 10)); // %i if you want to write an integer variable into the string, %% if you want to write a %, we want to show health with per cents so we have to to 'health / 10'
                PlayerTextDrawSetString(i,Velocity[i],string); // setting the PlayerTextDraw string
                PlayerTextDrawShow(i,Velocity[i]); // Showing the TextDraw to the player i
            }
            else if(!IsPlayerInAnyVehicle(i)) // You could also write else if(IsPlayerInAnyVehicle(i) == 0))
            {
                PlayerTextDrawHide(i,Velocity[i]); // The TextDraw has to be hidden
            }
        }
        else if(!IsPlayerConnected(i)) //same as if(IsPlayerConnected(i) == 0)
        {
            PlayerTextDrawHide(i,Velocity[i]); // The TextDraw has to be hidden
        }
    }
    return 1;
}


If i could help plase evaluate my code and write in the the comments!
Reply
#2

Now we need the formula to find out the kph.
It's floatsqroot((x*x)+(y*y)+(z*z))*156.666667;

This is incorrect it is
floatsqroot((x*x)+(y*y)+(z*z))*180.0;

I suggest fixing it so the noobs get it right.
Reply
#3

Seems nice..
Reply
#4

Quote:
Originally Posted by Pottus
View Post
Now we need the formula to find out the kph.
It's floatsqroot((x*x)+(y*y)+(z*z))*156.666667;

This is incorrect it is
floatsqroot((x*x)+(y*y)+(z*z))*180.0;

I suggest fixing it so the noobs get it right.
Mhhh, i found that it was mine magic number.
Maybe i use mph ?
Reply
#5

Quote:
Originally Posted by lcp9
View Post
Mhhh, i found that it was mine magic number.
Maybe i use mph ?
180.0 is for KPH trust me
Reply
#6

Quote:
Originally Posted by Pottus
View Post
Now we need the formula to find out the kph.
It's floatsqroot((x*x)+(y*y)+(z*z))*156.666667;

This is incorrect it is
floatsqroot((x*x)+(y*y)+(z*z))*180.0;

I suggest fixing it so the noobs get it right.
Quote:
Originally Posted by Pottus
View Post
180.0 is for KPH trust me
I said, maybe its mph, not that mine is correct.
Reply
#7

Quote:
Originally Posted by Pottus
View Post
180.0 is for KPH trust me
But something it must be, because normaly an Infernus can drive 192 kph/mph or whatever.

Edit: I searched the internet and i found that there is no 'true' formula.
Everyone use another formula.
Often the formulas like 190 or 200 or other things.

Edit: I will edit my Tutorial to mph
Reply
#8

Well if you look at this include.

https://sampforum.blast.hk/showthread.php?tid=486060

It has all the correct top speeds for vehicles.
The reason everyone uses the incorrect formula is because of posts like this that uses the incorrect formula people just copy paste code. If you use 180 it will show approximately the correct values.
Reply
#9

Quote:
Originally Posted by Pottus
View Post
Well if you look at this include.

https://sampforum.blast.hk/showthread.php?tid=486060

It has all the correct top speeds for vehicles.
The reason everyone uses the incorrect formula is because of posts like this that uses the incorrect formula people just copy paste code. If you use 180 it will show approximately the correct values.
Let's stop that discussion, we will never get a result for it...
Reply
#10

What are you talking about dude, several of us have documented and tested the living shit out of this all the results are in lol.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)