Log in to reply
 

Broken Spawn Mechanic, some experienced eyes for a review would help.



  • So I have been working on setting up a spawn mechanic for a while that would allow my Mod Menu to sort a type of car, Muscle. I got great help with that but since then and having gotten that working the spawn mechanic has been broken. I worked on it for a while trying to find out what is wrong but the error says something is null. The Specific line returning null was sent is v.PlaceOnGround();
    With this line I have already setup v as Vehicle v = World.CreateVehicle(hash.GethashCode().ToString(), gamePed.Position, gamePed.Heading);
    At the point v.PlaceOnGround(); is called something is null. The ToString() I added after trying the hash.GetHashCode(). Ill post more code for review to get an understanding and hopefully someone can spot my mess up. Thank you for the help in advance.

    void VehicleMuscle()
    {
    UIMenu submenu = modMenuPool.AddSubMenu(vehicleMenu, "Muscle Vehicles");

            List<dynamic> listOfMuscleVehicles = new List<dynamic>();
            VehicleHash[] allVehicleHashes = (VehicleHash[])Enum.GetValues(typeof(VehicleHash));
            VehicleHash[] allMuscleHashes = new VehicleHash[39];
            int x = 0;
            for (int i = 0; i < allVehicleHashes.Length; i++)
            {
                if (Function.Call<int>(Hash.GET_VEHICLE_CLASS_FROM_NAME, (int)allVehicleHashes[i]) == (int)VehicleClass.Muscle)
                {
                    listOfMuscleVehicles.Add(allVehicleHashes[i]);
                }
            }
    

    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 = listOfMuscleVehicles[listIndex];
                    UI.ShowSubtitle(hash.GetHashCode().ToString());
    
                    Ped gamePed = Game.Player.Character;
    
                    Vehicle v = World.CreateVehicle(hash.GetHashCode().ToString(), gamePed.Position, gamePed.Heading);
                    v.PlaceOnGround();
                    gamePed.Task.WarpIntoVehicle(v, VehicleSeat.Driver);
                }
            };

  • MODERATOR

    @xhiyikfkox I would suggest a read through this thread. https://forums.gta5-mods.com/topic/7113/net-c-shvdn-modding-basics-vector3s-and-vehicles-manipulation-part-3/2

    You also really need to be careful with using ToString(), as you discovered previously, it makes errors go away but it creates the wrong datatype for the function call.

    I don't use hashes when I'm spawning vehicles but what happens if you change the create vehicle line to this?

    Vehicle v = World.CreateVehicle(hash, gamePed.Position, gamePed.Heading);

    I also wouldn't use the player position as the location of the spawn as it will spawn directly on top of the player and probably kill them.

    There is good information in that thread I linked, take the time to go through that, it should answer any questions you have and maybe those you're going to have.



  • You are using the function 'GetHashCode' which is not what you think it is. It is a function derived from the underlying type (object) and nothing related to scripthookvdotnet. I could see why this would be confusing for someone starting out. When you create the vehicle, passing the variable 'hash' should be sufficent enough..



  • I will say this, I have done my best to setup everything I know and how I know to. I just got done putting in the code to output the vehicle hash, gamePed.Position and gamePed.Heading to a subtitle so that I could check what is returning null. The vehicle I tried to spawn was the Virgo3, its listed as the first muscle car found in VehicleHash. The place I tried to spawn was on the sidewalk outside of Franklins Aunt's house. The Result of the Subtitle is as follows: 16646064, X:-13.08868 Y:-1455.135 Z:30.44675, 182.8699

    These numbers are in this format: VehicleHash, gamePed.Position, gamePed.Heading

    As the subtitle shows, everything has a value... so im lost at how v.PlaceOnGround(); is broken.


  • MODERATOR

    @xhiyikfkox It's because the wrong datatype is being passed in the creation line, so v never gets set to anything. As that's the case, v.PlaceOnGround() has to return an error, because v isn't anything.



  • @xhiyikfkox Just because something has a value, doesn't mean its the right value (: Take a look again at what both me and @LeeC2202 pointed out about your code.



  • I understand the wrong data type was being passed in, its why I started to use the GetHashCode and then tried .ToString, get GetHashCode was outputing to subtitle the proper hash I needed. I assumed early on that it was an issue with the hash. The list I am pulling from is from my allMuscleVehicles which is the list of names. So it was first trying to summon vehicle by hash but calling its name. So I setup the GetHashCode, which I then put to subtitle output, this displayed the proper hash, so I tried to use that. Still not working. Just using the line Vehicle v = World.CreateVehicle(hash, gamePed.Position, gamePed.Heading); was returning a null value used and not assigned to game object.



  • So this is in the submenu.OnItemSelect section:

    if (item == getVehicle)
    {
    int listIndex = list.Index;
    VehicleHash hash = listOfMuscleVehicles[listIndex];

                    Ped gamePed = Game.Player.Character;
    
                    UI.ShowSubtitle(hash.ToString());
    
                    Vehicle v = World.CreateVehicle(hash, gamePed.Position, gamePed.Heading);
                    v.PlaceOnGround();
                    gamePed.Task.WarpIntoVehicle(v, VehicleSeat.Driver);
                }
    

    as mentioned above it was asked My dont I use the Vehicle v create line with just hash, the result was from ScriptHookVDotNet.log:

    [15:16:23] [ERROR] Caught fatal unhandled exception:
    System.NullReferenceException: Object reference not set to an instance of an object.
    at MMTest.Class1.<>c__DisplayClass5_0.<VehicleMuscle>b__0(UIMenu sender, UIMenuItem item, Int32 index) in C:\Users\David\Desktop\GTA V Script Stuff\Projects\MMTest\MMTest\Class1.cs:line 80
    at NativeUI.UIMenu.SelectItem()
    at NativeUI.UIMenu.ProcessControl(Keys key)
    at NativeUI.MenuPool.ProcessControl()
    at NativeUI.MenuPool.ProcessMenus()
    at MMTest.Class1.onTick(Object sender, EventArgs e) in C:\Users\David\Desktop\GTA V Script Stuff\Projects\MMTest\MMTest\Class1.cs:line 113
    at GTA.Script.raise_Tick(Object value0, EventArgs value1)
    at GTA.Script.MainLoop()


  • MODERATOR

    @xhiyikfkox Instead of the World.CreateVehicle line you have now, replace it with this:

    Model newVehicle = new Model(hash);
    Vehicle v = World.CreateVehicle(newVehicle, gamePed.Position, gamePed.Heading);
    

    I think the problem is that you're trying to pass a Hash as a parameter where it wants a Model.



  • @LeeC2202 Just tried it:

    Model newVehicle = new Model(hash);
    Vehicle v = World.CreateVehicle(newVehicle, gamePed.Position, gamePed.Heading);
    v.PlaceOnGround();
    gamePed.Task.WarpIntoVehicle(v, VehicleSeat.Driver);

    Error:

    [17:01:26] [ERROR] Caught fatal unhandled exception:
    System.NullReferenceException: Object reference not set to an instance of an object.
    at MMTest.Class1.<>c__DisplayClass5_0.<VehicleMuscle>b__0(UIMenu sender, UIMenuItem item, Int32 index) in C:\Users\David\Desktop\GTA V Script Stuff\Projects\MMTest\MMTest\Class1.cs:line 71
    at NativeUI.UIMenu.SelectItem()
    at NativeUI.UIMenu.ProcessControl(Keys key)
    at NativeUI.MenuPool.ProcessControl()
    at NativeUI.MenuPool.ProcessMenus()
    at MMTest.Class1.onTick(Object sender, EventArgs e) in C:\Users\David\Desktop\GTA V Script Stuff\Projects\MMTest\MMTest\Class1.cs:line 80
    at GTA.Script.raise_Tick(Object value0, EventArgs value1)
    at GTA.Script.MainLoop()



  • The original tutorial I was following for making my mod menu is by GTAVModder4Life located at https://forums.gta5-mods.com/topic/1027/c-nativeui-how-to-create-a-mod-menu-using-nativeui-part-2/2 If you look down in the second half of the tutorial there are 2 sets of spawning vehicle he has, one is just a list of vehicles and one is search by name. The List is what I am trying to modify.


  • MODERATOR

    @xhiyikfkox Okay... looks like I was wrong about the hash... that will teach me to concentrate more.

    Well considering that you got the same error, yet have changed the code, it would seem possible that the problem actually lies elsewhere.

    If you have no code in that OnItemSelect handler, other than a UI.Notify("Do This"); does that error still occur. I think you need to work out whether it is something inside the handler, or the handler itself that is causing the error. Otherwise, if it is the handler, you could spend all day changing code and never actually deal with the real problem.

    I'm, sorry I can't be much more help, but I'm juggling three projects, a whole bunch of graphic assets and site watching/moderating, so I'm probably not in the best place to provide answers right now. :(



  • @LeeC2202 after my last post my father came home, he is a programmer, I asked him to take a look at the code and see if he could help. He has broken down the spawn section so it runs parts and checks them. We managed to get the vehicles to spawn but they vanish before you can get into them. Also another bug is we commented out the part of putting the gamePed into the driver seat. That being removed allowed the car to spawn for a moment before vanishing before you got into it.


  • MODERATOR

    @xhiyikfkox Make sure you're not trying to spawn one of the newer vehicles out of the mpimportexport dlc. If you are, you need this mod: https://www.gta5-mods.com/scripts/mplowrider2-in-sp

    If you don't use that, the vehicles will disappear.



  • @LeeC2202 Not sure which vehicles are from any of the DLC as I have only had a copy of the game for a couple weeks. I can say this though, My favorite cars are the Dominator and the Gauntlet. Those work with the tutorial I listed earlier via the link and thus should also work here. I was able to remove the place player in vehicle and keep the dominator spawned by drawing a gun and backing up slowly while looking at the car. Was despawning I believe due to it being on me and when trying to get in character faces away thus poof the car is gone lol.



  • My father also implemented one specific line, under Model newVehicle = new Model(hash); he set newVehicle.Request(); That was something I did not have in my code before hand.


  • MODERATOR

    @xhiyikfkox said in Broken Spawn Mechanic, some experienced eyes for a review would help.:

    My father also implemented one specific line, under Model newVehicle = new Model(hash); he set newVehicle.Request(); That was something I did not have in my code before hand.

    As far as I know, that is already implemented as part of the SHVDN hook, so it doesn't need to be there unless you bypass SHVDN for the vehicle creation.



  • @LeeC2202 Neither me nor my Father over rode SHVDN, he only added 2 new lines I did not use to the code. The other couple lines he added simply are if statements that when they are processed check if the requested process worked or if the item is found to be valid in the cdimage.

    I removed the vehicle.request() line and the mod menu still works the same as with that line. But there is a ... delay. When I select get vehicle it dose not spawn the vehicle right away so you have to hit it 3,4,5 times to get the vehicle to spawn. Some will spawn and stay put while a few go poof after a few seconds weather you get into the car or not.



  • Also the biggest issue we found that was crashing the script was the line gamePed.Task.WarpIntoVehicle(v, VehicleSeat.Driver); While the code should work as the tutorial showed it to and as it did when following that tutorial, since the vehicle is not spawning every time, that line is unable to find the vehicle and thus crashes.


  • MODERATOR

    @xhiyikfkox Part of the way the CreateVehicle() function works is that it requests the model from the game and doesn't return back until it either has the model, or the model is determined to be invalid.

    There is generally a delay of some kind when you spawn a vehicle, for that very reason. It all depends on whether that delay is excessive or not. Having to hit several times does seem to indicate that but it's hard to say if that's code or PC performance related.

    To avoid crashes if the vehicle doesn't spawn, you could always wrap the Task code with

    if (v.Exists())
    {
    /// Do Task code here
    }
    

    That way, if it doesn't exist, it simply falls back to the game``



  • @LeeC2202 I understand a slight delay, that would be expected due to we are hooking into the game to change things such as force spawn a car. The delay I have however is not just a 2 or 3 second delay. As for performance the game runs fine in story, when I play online I set priority to high due to graphics pop in but beyond that never have any issue with performance.

    As for your if (v.Exists()) my dad did something similar but different. I posted below what he did:

    if (newVehicle != null && newVehicle.IsInCdImage && newVehicle.IsValid)
    {
    Vector3 spawnPos = Game.Player.Character.GetOffsetInWorldCoords(new Vector3(0,5,0));
    Vehicle v = World.CreateVehicle(newVehicle, spawnPos, gamePed.Heading);
    if (v != null)
    {
    if (v.PlaceOnGround())
    {
    UI.ShowSubtitle("Vehicle Placed");
    //gamePed.Task.WarpIntoVehicle(v, VehicleSeat.Driver);
    }
    else
    {
    UI.ShowSubtitle("Couldn't Place Vehicle");
    }
    }
    }
    UI.ShowSubtitle(newVehicle == null ? "Null Vehicle" : "Vehicle Model Found: " + " IsInCdImage: " + newVehicle.IsInCdImage.ToString() + " IsValid:" + newVehicle.IsValid.ToString() + " IsVehicle: " + newVehicle.IsVehicle.ToString());
    }



  • the vector3 spawnpos was my doing, did that to force spawn the car in front of me to try to prevent the car from despawning thus I would always be looking at it. Some cars despawn like they are on a 3 second timer though...


  • MODERATOR

    @xhiyikfkox And you are using that mpimportexport mod I linked to previously?



  • @LeeC2202 no, have not added that in at this time


  • MODERATOR

    @xhiyikfkox I posted that because you said your cars were disappearing.


Log in to reply
 

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