Player's distance from point textdraw - 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: Help Archive (
https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Player's distance from point textdraw (
/showthread.php?tid=192759)
Player's distance from point textdraw -
samsicles - 23.11.2010
I'm trying to make a simple command that measures the distance between a player's current position, and a previously defined point. The command /defpoint stores their starting position, and starts a looping timer to work out how far from the point they've moved. I'll try and include only the necessary parts;
/defpoint command
pawn Код:
CMD:defpoint(playerid, params[])
{
DistanceTravelled[playerid] = TextDrawCreate(320.000000, 361.650000,"~g~~h~Distance: ~w~~h~ ");
TextDrawSetShadow(DistanceTravelled[playerid],0);
TextDrawSetOutline(DistanceTravelled[playerid],1);
TextDrawLetterSize(DistanceTravelled[playerid],0.3,1.05);
TextDrawFont(DistanceTravelled[playerid],1);
TextDrawAlignment(DistanceTravelled[playerid],2);
TextDrawShowForPlayer(playerid, DistanceTravelled[playerid]);
GetPlayerPos(playerid, DEFx, DEFy, DEFz);
UpdateTimer[playerid] = SetTimerEx("distance_timer", 750, true, "i", playerid);
SendClientMessage(playerid, COLOR_LIGHTBLUE, "Point defined! Type /close to close the calculator.");
return 1;
}
Timer
pawn Код:
public distance_timer(playerid)
{
GetPlayerPos(playerid, NEWx, NEWy, NEWz);
new Float:distance = GetPointDistanceToPoint(DEFx,DEFy,NEWx,NEWy);
new Float:distancernd = floatround(distance,floatround_round);
format(DistanceUpdateString[playerid],24,"~g~~h~Distance: ~w~~h~%d",distancernd);
TextDrawSetString(DistanceTravelled[playerid], DistanceUpdateString[playerid]);
TextDrawShowForPlayer(playerid, DistanceTravelled[playerid]);
return 1;
}
Stock function to calculate distance
pawn Код:
Float:GetPointDistanceToPoint(Float:x1,Float:y1,Float:x2,Float:y2)
{
new Float:x, Float:y;
x = x1-x2;
y = y1-y2;
return floatsqroot(x*x+y*y);
}
The issue is that it doesn't seem to be calculating accurate results. The textdraw only reads "Distance: 1". I added a little debug that would send a client message saying what the 'distance' value was, and it was coming up with 10-figure long numbers. (e.g - 1132197622)
I'm hoping it's just some stupid mistake that some smarty-pants will pick up. Any replies are appreciated.
(Don't bother criticising the fact that it's probably a terrible script - I'm only going to run it alone here and there to work out distances, the looping timer doesn't worry me.)
Re: Player's distance from point textdraw -
Finn - 23.11.2010
%d is for integers,
%f is for floats.
Re: Player's distance from point textdraw -
samsicles - 24.11.2010
Cheers Finn, it works well now.
You're awesome.