06.10.2014, 19:32
(
Last edited by lcp9; 07/10/2014 at 06:29 PM.
)
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:
If we done that we have to create the PlayerTextDraw variable:
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:
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:
We want that our Text is in a box so we write:
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:
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:
Everything together:
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:
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:
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.
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:
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);
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.
Now we need the formula to find out the mph.
It's floatsqroot((x*x)+(y*y)+(z*z))*156.666667;
Now we have to use our function in our main function:
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.
Now we have to edit our PlayerTextDraw.
First we create a string.
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:
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:
The last thing we want to do is showing the PlayerTextDraw by using the function: PlayerTextDrawShow(playerid,PlayerTextDraw);
Now we put in out variables:
Now we put in the last things we did into our main function:
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
4. The full code
Here you can see the full code:
If i could help plase evaluate my code and write in the the comments!
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
pawn Code:
new PlayerText:Velocity[MAX_PLAYERS];
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~_");
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);
pawn Code:
PlayerTextDrawUseBox(playerid,Velocity[playerid],1);
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);
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]);
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();
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);
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;
}
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;
}
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);
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)
{
}
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
}
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;
}
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
First we create a string.
pawn Code:
new string[256];
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'
If we want to use it we have to put our variables into it:
pawn Code:
PlayerTextDrawSetString(i,Velocity[i],string);
Now we put in out variables:
pawn Code:
PlayerTextDrawShow(i,Velocity[i]);
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;
}
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;
}
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!