SA-MP Forums Archive
Getting last vehicle id player was in. - 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)
+--- Thread: Getting last vehicle id player was in. (/showthread.php?tid=325350)



Getting last vehicle id player was in. - Dokins - 12.03.2012

Alright, I'm making a trunk system, but they must be out of the vehicle to use it. But I need to check that they are in range of the vehicle.. But to do this, i'd need to store the vehicle id and get it's position, right?

How could I go about doing that?


Re: Getting last vehicle id player was in. - ddnbb - 12.03.2012

Just use GetNearestVehicleID (there is a stock somewhere, just use search), then GetVehiclePos. Then just use IsPlayerInRange. And of course you need to use variables etc.

Hope it helped.


Re: Getting last vehicle id player was in. - eesh - 13.03.2012

pawn Код:
public OnPlayerStateChange(playerid,oldstate,newstate)
{
    if(newstate == PLAYER_STATE_DRIVER || newstate == PLAYER_STATE_PASSENGER)
    {
        lastvehicleid[playerid] = GetPlayerVehicleID(playerid);
    }
}
stock IsVehicleNear(vehicleid)
{
    new Float:p[3];
    GetVehiclePos(vehicleid, p[0],p[1],p[2])
    if(IsPlayerInRangeOfPoint(playerid, 4.0, p[0],p[1],p[2])) return 1;
    return 0;
}



Re: Getting last vehicle id player was in. - ddnbb - 13.03.2012

sorry for double post, it was accident


Re: Getting last vehicle id player was in. - ddnbb - 13.03.2012

Quote:
Originally Posted by eesh
Посмотреть сообщение
pawn Код:
public OnPlayerStateChange(playerid,oldstate,newstate)
{
    if(newstate == PLAYER_STATE_DRIVER || newstate == PLAYER_STATE_PASSENGER)
    {
        lastvehicleid[playerid] = GetPlayerVehicleID(playerid);
    }
}
stock IsVehicleNear(vehicleid)
{
    new Float:p[3];
    GetVehiclePos(vehicleid, p[0],p[1],p[2])
    if(IsPlayerInRangeOfPoint(playerid, 4.0, p[0],p[1],p[2])) return 1;
    return 0;
}
So you can use a vehicles trunk, AFTER you enter & exit it? Whats the point in that? O.O

Anyway i searched ****** for you, and found this:
pawn Код:
GetClosestVehicleID(playerid)
{
    new Float:min_dist = 100.0;
    new Float:dist;
    new Float:vehx,Float:vehy,Float:vehz;
    new Float:px,Float:py,Float:pz;
    new Float:x,Float:y,Float:z;
    GetPlayerPos(playerid,px,py,pz);
    new close_vehid;
    for(new i = 1; i < MAX_VEHICLES;i++)
    {
        if(GetVehicleModel(i) > 0)
        {
            GetVehiclePos(i,vehx,vehy,vehz);
            x = px - vehx;
            y = py - vehy;
            z = pz - vehz;
            dist = floatsqroot((x * x) + (y * y) + (z * z));
            if(dist < min_dist)
            {
                min_dist = dist;
                close_vehid = i;
            }
        }
    }
    return close_vehid;
}
Then just use a check, if(player is near that vehicle id), for example, PlayerToPoint or IsPlayerInRange.


Tell me if this works, and +rep !


Re: Getting last vehicle id player was in. - Babul - 13.03.2012

i bet he wont need a distance check looped for all vehicles. who will exit his car in angel pine, walk up to KACC fuels, and try to open the trunk 5 hours later, after walking 10 miles?
setting the last entered vehicle is the way to go with.
if the vehicleid got stored in LastVehicle[playerid], then this variable can be used with GetVehiclePos(LastVehicle[playerid],X,Y,Z), and then checked for being IsPlayerInRangeOfPoint(playerid,5,X,Y,Z). no need for any loops.


Re: Getting last vehicle id player was in. - ddnbb - 13.03.2012

Quote:
Originally Posted by Babul
Посмотреть сообщение
i bet he wont need a distance check looped for all vehicles. who will exit his car in angel pine, walk up to KACC fuels, and try to open the trunk 5 hours later, after walking 10 miles?
setting the last entered vehicle is the way to go with.
if the vehicleid got stored in LastVehicle[playerid], then this variable can be used with GetVehiclePos(LastVehicle[playerid],X,Y,Z), and then checked for being IsPlayerInRangeOfPoint(playerid,5,X,Y,Z). no need for any loops.
Well it works in that way too. But its stupid imo. Imagine youre selling weapons RP:ly and you tell your customer to pick the weapons from the trunk, he needs to get IN the vehicle to get its ID, then back out, then open the trunk. Lol.

I dont think looping all cars isnt that bad, or is it?


Re: Getting last vehicle id player was in. - Babul - 13.03.2012

yes, looping is the worst way to script. why not set the customers "allowance" vehicleid to the vehicleid the dealer (you) is sitting in?
/offer could contain
pawn Код:
SetPVarInt(customerID,"MayOpenTrunkOfVehicleID",GetVehicleID(playerid),1);
then, the customer, entering a /opentrunk command, should be checked for being in range indeed
in case you want to offer items to more than just 1 customer at a time (like a big sell-out), then a loop through all vehicles is sometimes the easier way. is not that 10 players typing /opentrunk will cause a server crash.. each dealer system can be optimized - my idea was a 2-way connection between the seller and customer, it would be a pain to evolve it to work with 2 or more dealers/customers at the same time. yourr right there. i guess you got some ideas on what to think about with your script, and find the best suitable way to do it hehe


Re: Getting last vehicle id player was in. - ddnbb - 13.03.2012

Quote:
Originally Posted by Babul
Посмотреть сообщение
yes, looping is the worst way to script. why not set the customers "allowance" vehicleid to the vehicleid the dealer (you) is sitting in?
/offer could contain
pawn Код:
SetPVarInt(customerID,"MayOpenTrunkOfVehicleID",GetVehicleID(playerid),1);
then, the customer, entering a /opentrunk command, should be checked for being in range indeed
in case you want to offer items to more than just 1 customer at a time (like a big sell-out), then a loop through all vehicles is sometimes the easier way. is not that 10 players typing /opentrunk will cause a server crash.. each dealer system can be optimized - my idea was a 2-way connection between the seller and customer, it would be a pain to evolve it to work with 2 or more dealers/customers at the same time. yourr right there. i guess you got some ideas on what to think about with your script, and find the best suitable way to do it hehe
Hehe. There should be a callback like OnPlayerOnVariable and you can put a variable anywhere on the map, and then you could check if the player is on the variable, if you get what i mean. But i guess that would use loop too, i dont know. Ps. Like your avatar