Trams?
#1

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

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


Forum Jump:


Users browsing this thread: 1 Guest(s)