Quantcast

Jump to content

» «
Photo

[C#]check which car models are around the player

3 replies to this topic
nordi
  • nordi

    Crackhead

  • Members
  • Joined: 17 Mar 2014
  • France

#1

Posted 27 April 2014 - 12:17 PM Edited by nordi, 16 August 2014 - 05:36 PM.

Hey

 

 

I want to check which vehicles models are around the player, and if there are  specific models, do something.

So, I was thinking about using World.GetClosestVehicle(Player.Character.Position, 20.0f) but the problem is, that it will only select ONE vehicle, not all the vehicle (or at least I think this is how it will work).

 

 

Thanks.


pedro2555
  • pedro2555

    Open Sourcer

  • Members
  • Joined: 02 Sep 2012
  • Portugal

#2

Posted 27 April 2014 - 12:33 PM Edited by pedro2555, 27 April 2014 - 12:41 PM.

Did you know that right clicking a function and selecting the option 'Go To Definition' can show you all overloads of a function (not only that, but all member of the underlying class) ?

 

GetClosestVehicle() implements 2 overloads, they are:

public static Vehicle GetClosestVehicle(Vector3 Position, float MaxRadius);
public static Vehicle GetClosestVehicle(Vector3 Position, float MaxRadius, Model ofModel);

You can use the second to get the closest vehicle from a specific position, around a certain radius of a said model, I think that is exactly what you want.

 

Here is a quick mock up:

Vehicle theUsualSuspect = World.GetClosestVehicle(Player.Character.Position, 20f, Model.FromString("YourModel"));

Not really sure what will return if no match is found, but I guess 'null', so you may want to check for that before using the theUsualSuspect.

 

If you need to match a model from multiple models, something on these lines is the best approach:

Model[] theUsualSuspects; // Fill the array with the desired models
Vehicle theUsualSuspect;
foreach (Model suspect in theUsualSuspects)
{
	theUsualSuspect = World.GetClosestVehicle(Player.Character.Position, 20f, suspect);
	if (theUsualSuspect != null)
		break;
}

Again, check for nulls.

  • nordi likes this

nordi
  • nordi

    Crackhead

  • Members
  • Joined: 17 Mar 2014
  • France

#3

Posted 27 April 2014 - 01:24 PM

Did you know that right clicking a function and selecting the option 'Go To Definition' can show you all overloads of a function (not only that, but all member of the underlying class) ?

 

GetClosestVehicle() implements 2 overloads, they are:

public static Vehicle GetClosestVehicle(Vector3 Position, float MaxRadius);
public static Vehicle GetClosestVehicle(Vector3 Position, float MaxRadius, Model ofModel);

You can use the second to get the closest vehicle from a specific position, around a certain radius of a said model, I think that is exactly what you want.

 

Here is a quick mock up:

Vehicle theUsualSuspect = World.GetClosestVehicle(Player.Character.Position, 20f, Model.FromString("YourModel"));

Not really sure what will return if no match is found, but I guess 'null', so you may want to check for that before using the theUsualSuspect.

 

If you need to match a model from multiple models, something on these lines is the best approach:

Model[] theUsualSuspects; // Fill the array with the desired models
Vehicle theUsualSuspect;
foreach (Model suspect in theUsualSuspects)
{
	theUsualSuspect = World.GetClosestVehicle(Player.Character.Position, 20f, suspect);
	if (theUsualSuspect != null)
		break;
}

Again, check for nulls.

 

Thanks, this should work ! I actually spent 10 minutes, yesterday, playing around with "public static Vehicle GetClosestVehicle(Vector3 Position, float MaxRadius, Model ofModel);" trying to figure out which value give to Model (as I only had "taxicar, copcar, etc." I didn't think FromString would work !)

Anyway, thanks !


julionib
  • julionib

    Coder

  • Feroci
  • Joined: 13 Sep 2012
  • Brazil

#4

Posted 28 April 2014 - 08:31 AM

use GetVehicles and in a For see the Hash of each car.model and see if its what you need, case its only one model you can apply model filter in the params of GetVehicles





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users