Getting last vehicle id player was in.
#1

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

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

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;
}
Reply
#4

sorry for double post, it was accident
Reply
#5

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 !
Reply
#6

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

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?
Reply
#8

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

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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)