SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 05/05/2014, 07:30 PM   #1
moof2010
Big Clucker
 
Join Date: Nov 2013
Posts: 66
Reputation: 0
Default Custom function problem

i use this:
Code:
stock GetClosestVehicle(playerid, Float:range)
{
    new     Float:p_X;
    new     Float:p_Y;
    new     Float:p_Z;

    new     Float:Distance;
    new     Float:PretendentDistance = range +1;
    new     Pretendent;

    GetPlayerPos(playerid, p_X, p_Y, p_Z);

    for(new vehicleid=1; vehicleid < MAX_VEHICLES; vehicleid++)
    {
        Distance = GetVehicleDistanceFromPoint(vehicleid, p_X, p_Y, p_Z);

        if(Distance <= range && Distance <= PretendentDistance)
        {
            Pretendent = vehicleid;
            PretendentDistance = Distance;
        }
    }

    return Pretendent;
}
but it returned me 1999 all time. I check at less than 10m from an vehicle end it tell me that nearrest vehicle is 1999 too

Why?
moof2010 is offline   Reply With Quote
Old 05/05/2014, 07:39 PM   #2
Smileys
Huge Clucker
 
Smileys's Avatar
 
Join Date: Dec 2013
Location: Home.
Posts: 384
Reputation: 31
Default Re: Custom function problem

I wonder why you start with vehicleid 1, instead of 0, vehicle ids start with 0, not with one, so what you should be using is:

pawn Code:
for(new vehicleid=0; vehicleid < MAX_VEHICLES; vehicleid++)

unless you have a specific reason to start at 1.

not sure if this will fix it, but the rest of the code seems fine to me; this is the only thing I could find.

tho, I also wonder why you do this:

pawn Code:
new     Float:PretendentDistance = range +1; // creates a variable that has the range + 1;

if(Distance <= range && Distance <= PretendentDistance) // checks if the vehicle is within the normal range, and within the range of the normal range +1;

if it inside the normal range then obviously it's also inside the range + 1; so I don't really understand why you do that.


also, you should change this
pawn Code:
new     Float:p_X;
    new     Float:p_Y;
    new     Float:p_Z;

    new     Float:Distance;
    new     Float:PretendentDistance = range +1;
    new     Pretendent;
to this.
pawn Code:
new    
    Float:p_X,
    Float:p_Y,
    Float:p_Z,

    Float:Distance,
    Float:PretendentDistance = range +1,
    Pretendent
;
Smileys is offline   Reply With Quote
Old 05/05/2014, 07:48 PM   #3
Vince
Spam Machine
 
Vince's Avatar
 
Join Date: Sep 2007
Location: Belgium
Posts: 10,067
Reputation: 2660
Default Re: Custom function problem

Quote:
Originally Posted by Smileys View Post
I wonder why you start with vehicleid 1, instead of 0, vehicle ids start with 0, not with one, so what you should be using is:
False. Vehicleids do start at 1. This is probably done to give GetPlayerVehicleID a proper return value.
__________________
Vince is offline   Reply With Quote
Old 05/05/2014, 07:50 PM   #4
Teemo
Big Clucker
 
Teemo's Avatar
 
Join Date: Jan 2014
Location: SA-MP
Posts: 89
Reputation: 4
Default Re: Custom function problem

Quote:
Originally Posted by Vince View Post
False. Vehicleids do start at 1. This is probably done to give GetPlayerVehicleID a proper return value.
Right
__________________
Teemo is offline   Reply With Quote
Old 05/05/2014, 07:51 PM   #5
Konstantinos
Spam Machine
 
Konstantinos's Avatar
 
Join Date: Dec 2011
Posts: 11,828
Reputation: 1401
Default Re: Custom function problem

http://pastebin.com/4p3c0wba
__________________
Life is like riding a bicycle. To keep your balance, you must keep moving.

[Tutorial] How to use SQLite
[FilterScript] Tune System
Konstantinos is offline   Reply With Quote
Old 05/05/2014, 07:51 PM   #6
Smileys
Huge Clucker
 
Smileys's Avatar
 
Join Date: Dec 2013
Location: Home.
Posts: 384
Reputation: 31
Default Re: Custom function problem

Quote:
Originally Posted by Vince View Post
False. Vehicleids do start at 1. This is probably done to give GetPlayerVehicleID a proper return value.
hm, I didn't know that; probably because other stuff does start at 0.

well, thanks for telling anyway
Smileys is offline   Reply With Quote
Old 05/05/2014, 07:54 PM   #7
moof2010
Big Clucker
 
Join Date: Nov 2013
Posts: 66
Reputation: 0
Default Re: Custom function problem

Quote:
Originally Posted by Smileys View Post
I wonder why you start with vehicleid 1, instead of 0, vehicle ids start with 0, not with one, so what you should be using is:

pawn Code:
for(new vehicleid=0; vehicleid < MAX_VEHICLES; vehicleid++)

unless you have a specific reason to start at 1.

not sure if this will fix it, but the rest of the code seems fine to me; this is the only thing I could find.

tho, I also wonder why you do this:

pawn Code:
new     Float:PretendentDistance = range +1; // creates a variable that has the range + 1;

if(Distance <= range && Distance <= PretendentDistance) // checks if the vehicle is within the normal range, and within the range of the normal range +1;

if it inside the normal range then obviously it's also inside the range + 1; so I don't really understand why you do that.


also, you should change this
pawn Code:
new     Float:p_X;
    new     Float:p_Y;
    new     Float:p_Z;

    new     Float:Distance;
    new     Float:PretendentDistance = range +1;
    new     Pretendent;
to this.
pawn Code:
new    
    Float:p_X,
    Float:p_Y,
    Float:p_Z,

    Float:Distance,
    Float:PretendentDistance = range +1,
    Pretendent
;


i don't see what is the diference?

and about pretendentdistance i change it inside for
moof2010 is offline   Reply With Quote
Old 05/05/2014, 08:08 PM   #8
moof2010
Big Clucker
 
Join Date: Nov 2013
Posts: 66
Reputation: 0
Default Re: Custom function problem

Quote:
Originally Posted by Konstantinos View Post
0 all times

for
Code:
new testrsadas[128];
							format(testrsadas,sizeof(testrsadas),"masina cea mai apropriata %i",GetClosestVehicle(playerid, 10));
					        SendClientMessage(playerid,0xFF0000FF, testrsadas);
moof2010 is offline   Reply With Quote
Old 05/05/2014, 08:18 PM   #9
Konstantinos
Spam Machine
 
Konstantinos's Avatar
 
Join Date: Dec 2011
Posts: 11,828
Reputation: 1401
Default Re: Custom function problem

I guess GetVehicleDistanceFromPoint returns 0 if a vehicle doesn't exist so check if it does.

pawn Code:
GetClosestVehicle(playerid, Float: range)
{
    new
        vehicleid = INVALID_VEHICLE_ID,
        Float: pX,
        Float: pY,
        Float: pZ,
        Float: distance = 9999.0,
        Float: tmp_distance;
   
    GetPlayerPos(playerid, pX, pY, pZ);
 
    for (new v = 1; v != MAX_VEHICLES; ++v)
    {
        if (!GetVehicleModel(v)) continue;
        tmp_distance = GetVehicleDistanceFromPoint(v, pX, pY, pZ);
        if (tmp_distance <= range && tmp_distance < distance)
        {
            distance = tmp_distance;
            vehicleid = v;
        }
    }
    return vehicleid;
}
__________________
Life is like riding a bicycle. To keep your balance, you must keep moving.

[Tutorial] How to use SQLite
[FilterScript] Tune System
Konstantinos is offline   Reply With Quote
Old 05/05/2014, 08:37 PM   #10
moof2010
Big Clucker
 
Join Date: Nov 2013
Posts: 66
Reputation: 0
Default Re: Custom function problem

thank you very much, it works. sorry for late, i ate

http://imgur.com/RMdzKIx,DaQQIyg,AoSCcvu#0
moof2010 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom function Lz Scripting Help 6 15/12/2012 02:38 PM
warning 213: tag mismatch on custom function Chriham3 Scripting Help 7 24/06/2012 07:57 PM
IsPlayerNearATM custom function mrcoolballs Scripting Help 5 18/06/2012 01:17 PM
[SOLVED] Problem with custom property function dcmd_crash Help Archive 0 08/05/2010 06:14 AM
Custom function isn't working biltong Help Archive 7 08/04/2010 06:05 PM


All times are GMT. The time now is 11:04 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.