[Tutorial] Speedo with Timer
#1

Hello Guys, There for my first tutorial, I Will Edit mckennie's speedo tutorial, to make it call with a timer and I'll show you how to create a Vehicle Health system(Health'o'Meter).

Here is his tutorial:

The Purpose of this tutorial?
-no (or less) lag when there are alot of players on your server;
-will teach you a bit on how to use timers;
-will get you better in pawno!


First of all, create a text draw at the top, under defines, just like in mckennie's script.
Code:
new Text:SPEEDOS[MAX_PLAYERS];
Secondly, in public OnPlayerConnectCallback, add this anywhere:
Code:
SPEEDOS[playerid] = TextDrawCreate(10.0,200.0," ");
TextDrawShowForPlayer(playerid,SPEEDOS[playerid]);
Now that we have the textdraw ready, we must add the speedo script.

at the top, again under defines, add:
Code:
forward Speedometer(playerid);
this will allow our custom callback that we'll do in the next steps to work.

We will now create a timer, to call the speedo at each 10 milliseconds.
Add this somewhere under ongamemodeinit (or onfilterscriptinit if it's a filterscript)
Code:
SetTimer("Speedometer", 100, true); //change 100 to what pleases you,but i'd say it's better to keep it like that.
And Finally, we'll create the public speedometer callback, so that all we've done yet can work!
Code:
public Speedometer(playerid)
{
	new vehicleid,Float:speed_x,Float:speed_y,Float:speed_z,Float:final_speed,speed_string[256],final_speed_int;
	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))*250.666667; // 250.666667 = kmph  // 199,4166672 = mph
		final_speed_int = floatround(final_speed,floatround_round);
		format(speed_string,256,"Speed: %i",final_speed_int);
		TextDrawSetString(SPEEDOS[playerid], speed_string);
	}
	else
	{
		TextDrawSetString(SPEEDOS[playerid], " ");
	}
    return 1;
}
GetVehicleVelocity will get the speed of the player in the vehicle
final_speed = floatsqroot will then show the speed in KMH or MPH (depending on which you choose)

that's it for the speedo! Now go try it in-game to see if it works :P

Now we're on for the second part; how to creater a vehicle health meter with a timer.
Just like for the Speedo, add this at the top:
Code:
new Text:HEALTH[MAX_PLAYERS];
Now, under public OnPlayerConnect callback, add this:
Code:
HEALTH[playerid] = TextDrawCreate(10.0,200.0," ");
TextDrawShowForPlayer(playerid,HEALTH[playerid]);
The Textdraw is now ready for use! Lets go for the next step.

In order to make the health system work, we have to add this to our public Speedometer callback:
Code:
	new Float:vehicle_health,final_vehicle_health,health_string[256];
This will be the variables to get vehicle's health for the next steps.

Again under public speedometer callback, find:
Code:
	if(vehicleid != 0)
	{
under it, add:
Code:
		GetVehicleHealth(vehicleid,vehicle_health);
		final_vehicle_health = floatround(floatround(vehicle_health - 250)/ 7.5); //This will make the health show at 100 when the vehicle is not damaged and at 0 when it is in fire.
		format(health_string,256,"Health %i", final_vehicle_health);
		TextDrawSetString(HEALTH[playerid], health_string);
Now, again under public speedometer callback search for
Code:
	else
	{
		TextDrawSetString(SPEEDOS[playerid], " ");
Right under it, add
Code:
		TextDrawSetString(HEALTH[playerid], " ");
This will make in sort that the health meter doesnt show when on foot.


So now, the Health'o'Meter should work!


Confused? here's the whole script:
Code:
/*-------------------------------------------------------
Basic Speed'o'Meter & Health'o'Meter Script By Pmk1
-------------------------------------------------------*/
#define FILTERSCRIPT
//-------------------------------------------------------
#include <a_samp>
//-------------------------------------------------------
new Text:SPEEDOS[MAX_PLAYERS];
new Text:HEALTH[MAX_PLAYERS];
//-------------------------------------------------------
forward Speedometer(playerid);
//-------------------------------------------------------
public OnFilterScriptInit()
{
	SetTimer("Speedometer", 100, true); //change 100 to what pleases you,but i'd say it's better to keep it like that.
	return 1;
}
//-------------------------------------------------------
public OnPlayerConnect(playerid)
{
	SPEEDOS[playerid] = TextDrawCreate(10.0,200.0," ");
	TextDrawShowForPlayer(playerid,SPEEDOS[playerid]);
	HEALTH[playerid] = TextDrawCreate(10.0,180.0," ");
	TextDrawShowForPlayer(playerid,HEALTH[playerid]);
	return 1;
}
//-------------------------------------------------------
public Speedometer(playerid)
{
	new vehicleid,Float:speed_x,Float:speed_y,Float:speed_z,Float:final_speed,speed_string[256],final_speed_int;
	vehicleid = GetPlayerVehicleID(playerid);
	new Float:vehicle_health,final_vehicle_health,health_string[256];
	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))*250.666667; // 250.666667 = kmph  // 199,4166672= mph
		final_speed_int = floatround(final_speed,floatround_round);
		format(speed_string,256,"Speed: %i",final_speed_int);
		TextDrawSetString(SPEEDOS[playerid], speed_string);
		GetVehicleHealth(vehicleid,vehicle_health);
		final_vehicle_health = floatround(floatround(vehicle_health - 250)/ 7.5); //This will make the health show at 100 when the vehicle is not damaged and at 0 when it is in fire.
		format(health_string,256,"Health %i", final_vehicle_health);
		TextDrawSetString(HEALTH[playerid], health_string);
	}
	else
	{
		TextDrawSetString(SPEEDOS[playerid], " ");
		TextDrawSetString(HEALTH[playerid], " ");
	}
    return 1;
}
//-------------------------------------------------------
Thanks for reading! I hope it can help you and +rep me if it did :P
(Please tell me if i made any mistakes)
Reply
#2

Nice tutorial, very usful for people who need speedos
Reply
#3

Thanks
Reply
#4

nice work you can use [pawn ] [/pawn ] for your code :P
Reply
#5

Thanks but i prefer using code, because with pawn, when you copy/paste, it only paste in 1 line... :\
Reply
#6

Nice. Really helped me out.
Reply
#7

Where is a timer in a final script?
Reply
#8

Nice Work...
Reply
#9

Quote:
Originally Posted by AiVAMAN
View Post
Where is a timer in a final script?
what do you mean? :\ EDIT: oh yea lol i forgot it, thanks for telling me

and thanks for the comments people :P
Reply
#10

where is the timer called again in the speedo? im using timer for other stuff, and not sure how i would make work for others
Reply
#11

well, just add the SetTimer("Speedometer", 100, true); in ongamemodeinit, it will call the speedo callback, but you have to create OTHER timers for other Callbacks. It won't work if you use the same...
Reply
#12

nice, the timer is a good idea. i didnt notice lag as i am the only who uses my server lol. i just code for the fun of coding
Reply
#13

haha :P thanks
Reply
#14

Nice, but what does GetVehicleVeloCity does, and floatsqroot( ? because if i follow an tutorial i also wana know what it does/how it works
Reply
#15

getvehiclevelocity will get player's speed in the vehicle and floatsqroot will make in sort that the speed show in KM or MPH
Reply
#16

Quote:
Originally Posted by pmk1
View Post
getvehiclevelocity will get player's speed in the vehicle and floatsqroot will make in sort that the speed show in KM or MPH
Ok nice thanks, i already made an fuel system so il go on and try to make an engine system with speedo meter too
Reply
#17

good :P i'm glad it helps people out
Reply
#18

K, released my speedo meter/fuel system
Reply
#19

yea i took a look at it, it looks nice, good work :P
Reply
#20

so top speed on this is only 85 mph?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)