[C#] Vehicle.GetAllModelsOfClass and Vehicle.GetAllModels not working?

CitizenFX.Core.Vehicle has these defined:
public static VehicleHash[] GetAllModels();
public static VehicleHash[] GetAllModelsOfClass(VehicleClass vehicleClass);

Yet i can’t use them, i get a Nullreferenceexception when i try to. Does anyone know why this is?

You said on discord that other people had this issue as well.

Could you make a small reproduction for us? To see if it actually is a bug or not :slight_smile:

1 Like

This should do it:

    class Test : BaseScript
    {

        private bool firstTick = true;

        public Test() {
            Tick += OnTick;
        }

        private async Task OnTick() {
            await Delay(0);
            if (firstTick) {
                firstTick = false;
                VehicleHash[] hashArr = Vehicle.GetAllModelsOfClass(VehicleClass.Emergency);
                VehicleHash[] hashArr2 = Vehicle.GetAllModels();
            }
        }
    }

Let me try to repro it!

1 Like

This is clearly not working… seems like MemoryAccess.VehicleModels does not get populated.

I don’t how this is suppose to be assigned, I see multiple pieces of code use it without it actually being assigned :thinking:

1 Like

Seems like these method should not exist at all, should’ve been removed when porting over scripthook. As I was told by an element :slight_smile:

Case closed I guess.

1 Like

Ok thanks. You wouldn’t happen to know if there is some other convenient way to get a list of the available vehicles?

In the future if someone is looking for this, below is a bit of code one can use to get all the vehicle hashes organized by type:

static Dictionary<VehicleClass, List<VehicleHash>> dict = new Dictionary<VehicleClass, List<VehicleHash>>();

        static void BuildDict() {
            foreach (VehicleHash vehicleHash in Enum.GetValues(typeof(VehicleHash)))
            {
                //Debug.WriteLine( vehicleHash +":" + (int)vehicleHash);
                Model model = new Model(vehicleHash);
                VehicleClass vehicleClass = Vehicle.GetModelClass(model);

                List<VehicleHash> list;
                if ( dict.TryGetValue(vehicleClass, out list))
                {
                    list.Add(vehicleHash);
                }
                else {
                    var newList = new List<VehicleHash>();
                    newList.Add(vehicleHash);
                    dict.Add(vehicleClass, newList);
                }
            }
        }

I’m not really sure sure, but I don’t think that hashes list has every vehicle.

That is very possible, i wouldn’t know. Do you know of a better way of doing it?

Not really a better way, but in vMenu ive made a list of all vehicles per vehicle class. It’s in the Vehicles.cs source file if you want to grab it.