Trams? - 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: Trams? (
/showthread.php?tid=243124)
Trams? -
Danny - 21.03.2011
Hi,
No, this isn't another noob topic about how i can spawn them ...
I've made a function to return the closest tram and to put the player into it to drive it (seat 0).
But, i noticed it only works on one tram. So if i spawn 2 trams, the code will succeed on Tram 1, but fails at Tram 2. Same thing happens with 3 trams, 4 trams, and so on. Is this a samp bug or something? This is my code:
Код:
stock ReturnClosestTramID(playerid)
{
for(new c=0; c<MAX_VEHICLES; c++)
{
if(GetVehicleModel© == 449)
{
new Float:X,Float:Y,Float:Z;
GetVehiclePos(c, X, Y, Z);
if(IsPlayerInRangeOfPoint(playerid, TRAM_RANGE, X, Y, Z))
{
return c;
}
}
}
return -1;
}
Greetz,
Danny
Re: Trams? -
Mauzen - 21.03.2011
Well, your function does not check for the closest one, but only if a tram is within the given range. So it will always return the id of the tram with the lowest vehicleid.
But you can do this with some extra lines:
pawn Код:
stock ReturnClosestTramID(playerid)
{
new Float:nearest_dist = -1.0, nearest_id = -1;
for(new c=0; c<MAX_VEHICLES; c++)
{
if(GetVehicleModel(c) == 449)
{
new Float:X,Float:Y,Float:Z;
GetVehiclePos(c, X, Y, Z);
if(GetPlayerToPoint(playerid, X, Y, Z) < nearest_dist || nearest_dist == -1.0)
// You have to get any implementation of a GetPlayerToPoint function that returns the distance
{
nearest_dist = GetPlayerToPoint(playerid, X, Y, Z);
nearest_id = c;
}
}
}
return c;
}
As you can see, you basically countercheck the distance with the currently shortest distance to see if the tram is nearer than your current nearest tram.