SA-MP Forums Archive
[Tutorial] Speedo with Timer - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] Speedo with Timer (/showthread.php?tid=167814)

Pages: 1 2 3


Making a Speed'o'meter & an Health'o'Meter with a Timer - pmk1 - 13.08.2010

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)


Re: Speedo with Timer - Lorenc_ - 13.08.2010

Nice tutorial, very usful for people who need speedos


Re: Speedo with Timer - pmk1 - 13.08.2010

Thanks


Re: Speedo with Timer - Flake. - 13.08.2010

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


Re: Speedo with Timer - pmk1 - 14.08.2010

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


Re: Speedo with Timer - scripter1 - 15.08.2010

Nice. Really helped me out.


Re: Speedo with Timer - AiVAMAN - 15.08.2010

Where is a timer in a final script?


Re: Speedo with Timer - HyperZ - 15.08.2010

Nice Work...


Re: Speedo with Timer - pmk1 - 16.08.2010

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


Re: Speedo with Timer - Anthonyx3' - 16.08.2010

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


Re: Speedo with Timer - pmk1 - 16.08.2010

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...


Re: Speedo with Timer - mckennie - 17.08.2010

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


Re: Speedo with Timer - pmk1 - 17.08.2010

haha :P thanks


Re: Speedo with Timer - gamer931215 - 19.08.2010

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


Re: Speedo with Timer - pmk1 - 19.08.2010

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


Re: Speedo with Timer - gamer931215 - 19.08.2010

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


Re: Speedo with Timer - pmk1 - 19.08.2010

good :P i'm glad it helps people out


Re: Speedo with Timer - gamer931215 - 19.08.2010

K, released my speedo meter/fuel system


Re: Speedo with Timer - pmk1 - 03.09.2010

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


Re: Speedo with Timer - WillyP - 04.09.2010

so top speed on this is only 85 mph?