[Tutorial] Simple speedometer script
#1

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
Reply
#2

Nice I like.
Reply
#3

gj looks good
Reply
#4

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.
Reply
#5

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
Reply
#6

You forgot something:
Quote:

new Text:SPEEDOS[MAX_PLAERS]

Use:
Quote:

new Text:SPEEDOS[MAX_PLAYERS];

But gr8 job i like it (:
Reply
#7

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

nice. Thanks for tutorial.
Reply
#9

At step 2 I get 2 errors:

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

Never mind, silly mistake.
Reply
#10

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


Forum Jump:


Users browsing this thread: 1 Guest(s)