SA-MP Forums Archive
[Tutorial] How to create a speedometer - 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] How to create a speedometer (/showthread.php?tid=315844)



How to create a speedometer - thimo - 04.02.2012

How to create a speedometer

Hello, this is my tutorial to make a simple textdraw speedometer. It measures in MPH. I suggest you read the tutorial and dont just copy paste but read it and retype it yourself since you learn the most from it.

Step 1:
Create all new's for the floats and the textdraw.
pawn Code:
new Text:Speedometer[MAX_PLAYERS] //This will create a textdraw for every player
new Float:svx[MAX_PLAYERS];//This is for getting the speed of the car
new Float:svy[MAX_PLAYERS];//This is for getting the speed of the car
new Float:svz[MAX_PLAYERS];//This is for getting the speed of the car
new Float:s1[MAX_PLAYERS];
new s2[MAX_PLAYERS];
new s3[MAX_PLAYERS];
new stimer[MAX_PLAYERS];
Step 2:
Forward the speedometer timer:
pawn Code:
forward speedometer(playerid);
Step 3:
Creating the textdraw by placing this under onplayerconnect:
pawn Code:
Speedometer[playerid] = TextDrawCreate(10.0, 200.0, " "); //Create a empty textdraw since the textdraw only shows a value when driving in a car
TextDrawFont(Speedometer[playerid], 2);//Set the text font.
TextDrawShowForPlayer(playerid, Speedometer[playerid]); //This gotta be in there otherwise speedometer wont show up.
Step 4:
Now lets create the public where we created the forward for.
pawn Code:
public speedometer(playerid)
{
    GetVehicleVelocity(GetPlayerVehicleID(playerid), svx[playerid], svy[playerid], svz[playerid]); // This Saves Our Velocitys To Our Varibles
    s1[playerid] = floatsqroot(((svx[playerid]*svx[playerid])+(svy[playerid]*svy[playerid]))+(svz[playerid]*svz[playerid]))*100; // This Is Our Forumula ( I Don't Know How It Works but i found it on internet )
    s2[playerid] = floatround(s1[playerid],floatround_round); // Round the output off to a whole number
    format(s3[playerid],32,"%i MPH", s2[playerid]); // The textdraw string
    TextDrawSetString(sdisplay[playerid], s3[playerid]); // The actual changing of the textdraw
    return 1;
}
Step 5:
Detect if a player is in a car and if he is show the textdraw
pawn Code:
public OnPlayerStateChange(playerid, newstate, oldstate)
{ // This Is The Callback That Is Called When A Person Changes State
    KillTimer(stimer[playerid]); // This Stops Our Timer For When You Get Out Of Your Vehicle Your Speed Doesn't Keep Going
    TextDrawSetString(Speedometer[playerid], " "); // This Sets Our Textdraw To Blank And Freezes Because We Stop The Timer ^
    if(newstate == 2) stimer[playerid] = SetTimerEx("speedometer", 255, true, "i", playerid); // This Starts The Timer When The Player Changes His/Her State To Being The Driver
    else if(newstate == 3) stimer[playerid] = SetTimerEx("speedometer", 250, true, "i", playerid); // This Start The Timer When The Player Changes His/Her Start To Being The Passenger
    return 1;
}
This is actually all there is to it.


Greetings Thimo


Re: How to create a speedometer - System64 - 04.02.2012

not good, why don't you take a look on my speedometer? Less variables, getting speed is easier and code is better


Re: How to create a speedometer - thimo - 04.02.2012

Well this is a BASIC speedometer


Re: How to create a speedometer - System64 - 04.02.2012

Quote:
Originally Posted by thimo
View Post
Well this is a BASIC speedometer
I know but I mean on speed in my speedometer not whole speedometer (Fuel, gears, speed...)


Re: How to create a speedometer - thimo - 04.02.2012

You are using a stock for getting a player's speed. Is this going to be faster in performance?


Re: How to create a speedometer - Konstantinos - 04.02.2012

I will agree with System64 because it has many variables and I have already a lot.
You have a undefined symbol
pawn Code:
new sdisplay[MAX_PLAYERS]
Also, why do you use the variable like this?
pawn Code:
s3[MAX_PLAYERS];
// ---
format(s3[playerid],256,"%i MPH", s2[playerid]);
1024 bytes for this text only? Waste of memory.
An array of 32 is enough.
Although, you explained it good.


Re: How to create a speedometer - thimo - 04.02.2012

Quote:
Originally Posted by Dwane
View Post
I will agree with System64 because it has many variables and I have already a lot.
You have a undefined symbol
pawn Code:
new sdisplay[MAX_PLAYERS]
Also, why do you use the variable like this?
pawn Code:
s3[MAX_PLAYERS];
// ---
format(s3[playerid],256,"%i MPH", s2[playerid]);
1024 bytes for this text only? Waste of memory.
An array of 32 is enough.
Although, you explained it good.
The undefined is fixed... i got this out of my GM so it was different. And about the 256, i will change it to 32
And btw Thanks for your comment


Re: How to create a speedometer - thimo - 11.02.2012

Bumper sticker



Re: How to create a speedometer - Littlehelper - 11.02.2012

Hello,
I'm afraid that the speedo meter you're tryin to teach us in this tutorial dosent works, test the code yourself first.
Thank You.


Re: How to create a speedometer - parames3010 - 24.02.2012

ขอบคุณครับ
ThankYou