SA-MP Forums Archive
[Tutorial] Simple speedometer script - 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] Simple speedometer script (/showthread.php?tid=161275)



Simple speedometer script - mckennie - 19.07.2010

hi, this is a short tutorial for making a simple text speedometer. it can measure in MPH or KM/H, but it only shows when the player is it a vehicle so, lets get started

step one. define an array that will hold all the speeds for each player

Code:
new Text:SPEEDOS[MAX_PLAYERS]
step two. assign text to each players speedo on connect. in the OnPlayerConnect callback, add this

Code:
SPEEDOS[playerid] = TextDrawCreate(10.0,200.0," ");
TextDrawShowForPlayer(playerid,SPEEDOS[playerid]);
explanation: textdrawcreate creates a textdraw, and the numbers next to it are its position on a 640x480 'canvas' that covers the screen. in the quotes is the default text for the textdraw, and since we dont want anything to show until the player gets into a car, its just a space

step 3. we need some code to read the cars speed and draw it to the screen, so thats up next. add the following code to the OnPlayerUpdate callback

Code:
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))*136.666667;
		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], " ");
	}
basicly, we're creating some variables, getting the id of the car the players driving, checking if the players in a car or walking, getting the speed of the car, and then drawing the speed to the screen. here ive multiplyed the speed by 136.666667 to get KM/H, but if you change it to 85.4166672 you would get MPH


i hope you liked my tutorial, and post with some ideas of stuff i could write if you cant be bothered, im out of ideas


Re: Simple speedometer script - [DK]JaloNik - 19.07.2010

Nice I like.


Re: Simple speedometer script - Kar - 19.07.2010

gj looks good


Re: Simple speedometer script - Joe_ - 19.07.2010

pawn Code:
final_speed = floatsqroot(((speed_x*speed_x)+(speed_y*speed_y))+(speed_z*speed_z))*136.666667;
        final_speed_int = floatround(final_speed,floatround_round);
You could just use GetVehicleVelocity, and do you actually know how the above code WORKS? or is this CnP?

And also, such a calculation is quite hard on a callback called 30 times a second, speed is needed in such cases.

Though, I myself would prefer a seperate timer than OPU, because calling the above code 30 times a second for every player connected is bad.


Re: Simple speedometer script - mckennie - 19.07.2010

i do understand the calculation, its the Pythagorean theorem modified for a 3 dimensional speed. and i found it works fine while i was testing it, if you can come up with somthing better, be by guest. also i did use GetVehicleVelocity


Re: Simple speedometer script - hab2ever - 19.07.2010

You forgot something:
Quote:

new Text:SPEEDOS[MAX_PLAERS]

Use:
Quote:

new Text:SPEEDOS[MAX_PLAYERS];

But gr8 job i like it (:


Re: Simple speedometer script - mckennie - 19.07.2010

oh yeah, pretty nooby mistake, but i was writing this at about 1 am :P


Re: Simple speedometer script - AiVAMAN - 19.07.2010

nice. Thanks for tutorial.


Re: Simple speedometer script - Garc1a - 21.07.2010

At step 2 I get 2 errors:

pawn Code:
error 017: undefined symbol "playerid"
error 017: undefined symbol "playerid"
-EDIT-

Never mind, silly mistake.


Re: Simple speedometer script - Sergei - 21.07.2010

Why don't you use PVars to store textdraw ID?