Jump to content

[Help] Question about Entity(Vehicle)


Recommended Posts

Posted (edited)

Hello Dear RageMP Community.

I want to find Closest Vehicle distance from Player with this code:

public static Vehicle GetClosestVehicle(Player player, float distance = 1000.0f)
    {
        Vehicle vehResult = new Vehicle();
        foreach (var veh in NAPI.Pools.GetAllVehicles())
        {
            Vector3 vehPos = NAPI.Entity.GetEntityPosition(veh);
            float distanceVehicleToPlayer = player.Position.DistanceTo(vehPos);
            if (distanceVehicleToPlayer < distance)
            {
                distance = distanceVehicleToPlayer;
                vehResult = veh;

            }
        }
        return vehResult;
    }

There is an error making me mad and cannot find out and understand what's going on, should I convert NetHandle to Entity(Vehicle) ??

ERROR: CS7036    There is no argument given that corresponds to the required formal parameter 'handle' of 'Vehicle.Vehicle(NetHandle)'

 

all of my input type from other functions are 'Vehicle' (not the NetHandle) and NAPI.Entity.GetEntityPosition(veh); want's me NetHandle type, How can I convert veh to NetHandle?


 

I should add this too,

 

When I'm going to change my input types to NetHandle, some errors come and say's this:

CS1503    Argument 1: cannot convert from 'GTANetworkAPI.NetHandle' to 'GTANetworkAPI.Entity'

 

I need to know How to Convert from NetHandle to Entity or Entity to NetHandle

Edited by LT.Steiner
Posted

You can use System.Linq to simplify this check:

public static Vehicle GetClosestVehicle(Player player, float distance = 15f)
{
    return NAPI.Pools.GetAllVehicles().Where(X => X.Position.DistanceToSquared(player.Position) <= distance).OrderBy(v => v.Position.DistanceToSquared(player.Position)).FirstOrDefault();
}

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...