Log in to reply
 

Vehiclehash[ ] is of VehicleClass.ClassType



  • So what I am trying to do as explained by the title is I can get the list of all vehicle hashes easily. But I am having trouble with checking if vehicle by hash is of model type.

    Ex. VehicleHash is of model type Muscle

    I was following the tutorial series for a mod menu by GTAVModder4Life and have asked them for help but have not received any response. The reason for doing this is to make a sub section where you can choose by model of vehicle aka Muscle, Sports, Utility and so on. Muscle is just the first I want to do.



  • @xhiyikfkox Use a for loop to go through the VehicleHash array, spawn a vehicle using the current VehicleHash (vehHashes[i]), then check the class using ClassType.

    Edit: Instead just use GET_VEHICLE_CLASS_FROM_NAME to get the class from the model hash itself.



  • Ok the best way to show what im trying to do is with the code setup:

        void VehicleMuscle()
        {
            UIMenu submenu = modMenuPool.AddSubMenu(vehicleMenu, "Muscle Vehicle");
    
            List<dynamic> listOfMuscleVehicles = new List<dynamic>();
            VehicleHash[] allMuscleVehicleHashes = (VehicleHash[])Enum.GetValues(typeof(VehicleHash));
    
            for(int i = 0; i < allMuscleVehicleHashes.Length; i++)
            {
    
            }
        }
    

    In this for loop i need to take allMuscleVehicleHashes[i] and check if it has VehicleClass.Muscle if so then I will do

    If (AllMuscleVehicleHashes.GetValue(VehicleClass.ClassType) == ClassType.Muslce)
    {
    listOfMuscleVehicles.Add(allMuscleVehicleHashes[i]);
    }

    but this code does not work.



  • @xhiyikfkox
    Use GET_VEHICLE_CLASS_FROM_NAME to get the class from the VehicleHash.

    foreach (VehicleHash hash in allVehicleHashes)
    {
        if (Function.Call<VehicleClass>(Hash.GET_VEHICLE_CLASS_FROM_NAME, (uint)hash) == VehicleClass.Muscle)
        {
    	listOfMuscleVehicles.Add(hash);
        }
    }


  • This post is deleted!


  • @Jitnaught I am not sure why, but with having added this function into the code the mod menu I am working on breaks, it refuses to pull up at all. Had this happen a few different times trying to use different methods of comparing the vehicleclass.



  • @xhiyikfkox Are you sure it's not just taking a long time? Is the script crashing?



  • @Jitnaught no idea, it literally just never responds to the key press to open the menu. Game keeps running but the menu will not display.



  • @xhiyikfkox Open the latest ScriptHookVDotNet log file in your GTA V directory and see if there's a message saying your script crashed.



  • @Jitnaught Does the hash in that function call need to be an (int)hash?



  • @LeeC2202 You're right; I should have checked my code in Visual Studio. You have to cast it to uint. @xhiyikfkox I edited my code. Sorry about that.



  • @LeeC2202 I can say the hash portion did not work, reported error, so I did hash.ToString and that worked. But to try and see if that part of the ui was interfearing I setup a new ui with only that setup... still not loading up.



  • @xhiyikfkox Whilst adding ToString() might make the error go away, it will result in the wrong data type being passed to the function call, which in turn will probably cause a crash.

    If I drop that code with the (uint)hash into my current project, I don't get an error from it.

    Edit: Here's an image of that code in VS, as you can see, no red lines under any part of that code, which means no error.

    0_1487200793932_hash.png



  • Somehow I am still messing this up, cant get the menu to pull up. Thanks for the help guys.



  • @xhiyikfkox Change the function call line to this:

    if (Function.Call<int>(Hash.GET_VEHICLE_CLASS_FROM_NAME, (int)hash) == (int)VehicleClass.Muscle)



  • My entire section as I have it now...

    void VehicleMuscle()
    {
    UIMenu submenu = modMenuPool.AddSubMenu(VehicleMenu, "Muscle Vehicle");

            List<dynamic> listOfMuscleVehicles = new List<dynamic>();
            VehicleHash[] allMuscleHashes = (VehicleHash[])Enum.GetValues(typeof(VehicleHash));
    
            foreach (VehicleHash hash in allMuscleHashes)
            {
                if (Function.Call<int>(Hash.GET_VEHICLE_CLASS_FROM_NAME, (int)hash) == (int)VehicleClass.Muscle)
                {
                    listOfMuscleVehicles.Add(hash);
                }
            }
    
            UIMenuListItem list = new UIMenuListItem("Vehicle:", listOfMuscleVehicles, 0);
            submenu.AddItem(list);
    
            UIMenuItem getVehicle = new UIMenuItem("Get Vehicle");
            submenu.AddItem(getVehicle);
    
            submenu.OnItemSelect += (sender, item, index) =>
            {
                if (item == getVehicle)
                {
                    int listIndex = list.Index;
                    VehicleHash hash = allMuscleHashes[listIndex];
    
                    Ped gamePed = Game.Player.Character;
    
                    Vehicle v = World.CreateVehicle(hash, gamePed.Position, gamePed.Heading);
                    v.PlaceOnGround();
                    gamePed.Task.WarpIntoVehicle(v, VehicleSeat.Driver);
                }
            };
        }


  • @xhiyikfkox Does it work?



  • @Jitnaught said in Vehiclehash[ ] is of VehicleClass.ClassType:

    @xhiyikfkox Open the latest ScriptHookVDotNet log file in your GTA V directory and see if there's a message saying your script crashed.

    Just saw this post as I was rereading what was said, and guess what...

    [ERROR] Failed to instantiate script 'MMTest.Class1' because constructor threw an exception:
    System.InvalidCastException: Unable to cast native value to object of type 'GTA.VehicleClass'

    it was crashing lol



  • @xhiyikfkox Another point of advice, I would use an explicitly typed list i.e. 'List<VehicleHash>' and avoid dynamic unless absolutely necessary. It can actually be more performance heavy since it will have to compute the type at runtime..



  • @CamxxCore I don't think NativeUI will accept anything other than dynamic lists for the UIMenuListItem. I am sure it gives an error.



  • Its saying in the error that I posted, it failed to instantiate script 'MMTest.Class1' because its unable to cast native value to object of type GTA.VehicleClass



  • @LeeC2202 Ah right, I forgot. But my point still stands in any other case xD



  • @CamxxCore Well with the error as it stands it would seem I cannot get the vehicle class in the way it is at this time. I will be looking it over tomorrow to see if I can tweak it to work.



  • @xhiyikfkox Good luck (: Such errors can be frustrating but also rewarding when you do finally figure it out.



  • First Google search after waking up: gta v function call Get_Vehicle_Class_From_Name

    The best part... is the first result which leads to this forum XD

    Vehiclehash[ ] is of VehicleClass.ClassType | GTA5-Mods.com Forums
    https://forums.gta5-mods.com › General Modding Discussion
    13 posts - ‎3 authors
    GET_VEHICLE_CLASS_FROM_NAME, (uint)hash) == VehicleClass. ... @xhiyikfkox Open the latest ScriptHookVDotNet log file in your GTA V directory and see if ... @Jitnaught Does the hash in that function call need to be an (int)hash ?


Log in to reply
 

Looks like your connection to GTA5-Mods.com Forums was lost, please wait while we try to reconnect.