Can't spawn car using ScriptHookV.
-
I'm trying to write my first script following a tutorial, the UI message "Adder has been spawned!" does show up when I hit the 7 Numpad key, but the car isn't spawning, any idea why?
using System;
using System.Windows.Forms;
using GTA;
using GTA.Native;
using GTA.Math;namespace GTAScriptTest
{
public class Main : Script
{
Vehicle car;
public Main()
{
Tick += onTick;
KeyDown += onKeyDown;
}
private void onTick(object sender, EventArgs e)
{} private void onKeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.NumPad7) { car = World.CreateVehicle(VehicleHash.Adder, Game.Player.Character.GetOffsetFromWorldCoords(new Vector3(0, 5, 0))); car.IsInvincible = true; UI.Notify("Adder has been ~b~spawned!"); } } }
}
Also, while I'm at it, should I reference the vehicle using the VehicleHash class or using a string like this:
car = World.CreateVehicle("Adder", Game.Player.Character.GetOffsetFromWorldCoords(new Vector3(0, 5, 0)));
Which is a better practice?
-
I'm scanning your code and I don't see any reason why it wouldn't work, particularly since the Notification is working.
The notification and the GetOffset indicate to me you're referencing SHVDN2 and it's better to go to SHVDN3 because the former is no longer updated/supported.Whether you compiled this or not, I don't see why it's not working, but I would have to test it in VS.
As for VehicleHash.Adder, this is great if you're using intellisense and aren't sure what the exact vanilla file is called.
For addons, definitely use "NameOfAddon".
P.S. It's obvious you have the required references installed and in your code, if not the UI message would not be displayed.
Only thing I can think of is because your spawning car at (0,5,0), that it's spawning where you can't see it, like inside a building.
-
I tested your code and I'm at a loss why the car isn't spawning. I tried different variable declarations, different cars....nothing.
As I wrote before, the code is executing and has the proper references because the notification does appear, just not the car.No idea why this doesn't work.
All I can suggest, is to use SHVDN3, which is supported and updated going forward.
This is how I spawn cars with 3:{ Vehicle vehicle1 = World.CreateVehicle("600LT", PP.Position + PP.ForwardVector * 3.0f, PP.Heading + 90); }
-
@Riser777
Unless we're both missing something as plain as the nose on our faces, I can only think this is linked to SHVDN2?I no longer use DN2 but I have old code snippets with exactly what you have.
No compiler errors, no run time errors, the notification appearing, but no car.It's either going to be, oh shit...I missed that....or what??
-
@JohnFromGWN Hey, I switched to SHVDN3, the code builds just fine with no errors in Visual Studio but when I try to run the script in game I get an error:
Also how do I write UI messages in SHVDN3? Can't call the UI class anymore after switching.P.S. I already tried spawning the car at (0,0,0) but no luck, it's definitely not spawning.
EDIT: Wait, I remembered that I got this error before when trying a different car name other than Adder, so I changed it from "600LT" back to "Adder" and now it works, I guess that was the wrong string for that vehicle or something.
Thank you!So, maybe the problem was from SHVDN2 after all(?), At least for me.
I wonder what the problem is on your side.
EDIT2: Now that I properly got this working, I can't find documentation or anything of the sort, any idea where I can find a list of all the functions, classes and members that I can call/access?
-
The reason 600LT didn't work for you is because you don't have it installed. It's a McLaren addon.
The UI syntax changed in SHVDN3. For example:
GTA.UI.Notification.Show
You can find documentation for the native functions here:
https://alloc8or.re/gta5/nativedb/and SHVDN3 here
https://github.com/crosire/scripthookvdotnet/tree/main/source/scripting_v3Your original code should NOT have worked in SHVDN3 because
GetOffsetFromWorldCoords
only works in SHVDN2.in SHVDN3 it is
GetOffsetPosition
and no, I still haven't figured out what happened but at least your SHVDN3 works.
Also check out this tutorial:
https://forums.gta5-mods.com/topic/41063/tutorial-a-beginner-s-guide-to-gta-v-scripting
-
I don't know why I struggled to find learning resources and documentation to begin with, I guess there's so much GTA 5 content out there that stuff gets drowned in the search results, but either way...
Now I can properly get started with writing scripts, should be fun!
Thank you so much for your invaluable help, Godspeed.
-
Mate... You've placed the KeyDown method inside your Tick method also you're trying to spawn the vehicle directly from the hash but you have to create and request the model first - try the following code instead:
using System; using GTA; using GTA.UI; using System.Windows.Forms; namespace GTAScriptTest { internal class Main : Script { public Main() { KeyDown += OnKeyDown; } private void OnKeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.NumPad1) { SpawnVehicle(VehicleHash.Adder); } if (e.KeyCode == Keys.NumPad2) { SpawnVehicle(VehicleHash.Annihilator); } if (e.KeyCode == Keys.NumPad3) { SpawnVehicle(VehicleHash.DeathBike); } } private void SpawnVehicle(VehicleHash vehicleHash) { Ped playerPed = Game.Player.Character; var vehicleModel = new Model(vehicleHash); vehicleModel.Request(); var vehicle = World.CreateVehicle(vehicleModel, playerPed.Position + playerPed.ForwardVector * 3.0f, playerPed.Heading); vehicle.IsInvincible = true; Notification.Show($"Vehicle Spawned: {vehicleHash.ToString()}"); vehicleModel.MarkAsNoLongerNeeded(); } } }
NumPad1: Spawns Adder
NumPad2: Spawns Annihilator
NumPad3: Spawns DeathbikeNote: The above code is SHVDN3.
-
@Riser777 I think it should be Vehicle not VehicleHash
-
It is VehicleHash. For example:
var vehicle = World.CreateVehicle(VehicleHash.Adder, position, heading);
or
var vehicle = World.CreateVehicle("Adder", position, heading);
The issue that I missed, but @KimonoBoy caught, is the code was in ontick for some reason. It should have been either on a keyup or a keydown.
However the Notification should be like this is with SHVDN3
GTA.UI.Notification.Show
Also, if the car is an addon I don't think you can use
vehicleHash.ToString()
because addon's don't exist in the GTA 5 engine.
-
@JohnFromGWN I was talking about his first post,
you can't use VehicleHash Car = World.CreateVehicle("Adder", position, heading);
Pretty sure it would be Vehicle Car = World.CreateVehicle("Adder", position, heading);
-
@M8T Ah ok. Right!
-
I believe I have the same issue.
Followed this:
https://github.com/KimonoBoy/SHVDNTutorial-Nuclei/wiki/Vehicle-Spawner-Menu#the-scriptIt works as intended til some extent. The whole menu works, and I can always spawn the first vehicle with no issues.
The problem comes when I have already spawned one model, and try to spawn a different one after that, sometimes it will work, sometimes it will not.
I did some research and apparently that could be happening because the game had no enough time to load the model so it is not there when "World.CreateVehicle()" runs.
So:
spawn model 1 - ok
spawn model 1 - ok
spawn model 2 - fail
spawn model 2- ok
spawn model 2 - ok
spawn model 1 - fail
spawn model 1 - okThis is an issue as I'm trying to precisely spawn several vehicles in sequence.
Script.Wait(ms) should help on that, buying time while the model loads, but then there is this on SHVDN latest release notes:
I didn't set NoScriptThread to true, and even tried to explicitly set it to false, but got no success on that.
Using Script.Wait() breaks my current execution block (be it a loop, function or whatever) instantly.
-
@JohnFromGWN Also I'm pretty sure you can force GTA to recognize addon vehicles as actual vehicles in with OpenIV so you can spawn them via script.
-
@Sata I have to disagree with requesting the model or putting in wait times. Never needed it.
I can spawn 30 cars in a few seconds without any issues....although at 30 you are very likely to crash the game!
-
@M8T Another misunderstanding. Ofc you can, I spawn all my addons by scripts. In fact I don't use vanilla cars.
What I meant is that Adder has a hash in the GTA 5 engine as do all vanilla cars. But if I have an addon named "McLarenF11R2" will it be able to convert its hash to the proper model name?
-
@JohnFromGWN Oh I don't think so then. Unless you mod the engine or something lol.
-
@M8T We'd have to test and see. I know Menyoo will sometimes return the name of a modded ped correctly, at other times it just shows some hex crap hash.
-
@Sata None of what you're doing should be required. No requesting models, no waiting for models to load, etc.
I think many of these functions were implemented in 1945 when they started the development of GTA 5 on Commodore 64s. Just kidding, but I have a modest system and no issues whatsover. I could easily spawn an s_load of cars in rapid succession with the same code in this thread, from a LemonUI menu.
-
First hash from Asea, no issue.
second hash from Asea2. If I just press the button again Asea2 will spawn.This Code:
private void SpawnVehicle(VehicleHash vehicleHash) { try { var vehicleModel = new Model(vehicleHash); vehicleModel.Request(); Notification.Show(NotificationIcon.SocialClub , "Done", "vehicleModel" , vehicleModel.ToString(), false, false); var vehicle = World.CreateVehicle(vehicleModel, Game.Player.Character.Position + Game.Player.Character.ForwardVector * 5.0f, Game.Player.Character.Heading + 90); vehicle.PlaceOnGround(); vehicleModel.MarkAsNoLongerNeeded(); } catch (Exception ex) { Notification.Show(NotificationIcon.Sasquatch, "Nope", "Exception!", ex.Message, false, false); } }
What am I missing then?
-
@Sata
Can't tell, you don't have any model names in your code.
-
@JohnFromGWN
what for?I'm getting the values from the game itself.
foreach (var vehicleHash in GTA.Vehicle.GetAllModelsOfClass(_vehicleClass)_vehicleClass = (VehicleClass)@enum;
Crosire himself uses "Wait" here:
https://github.com/crosire/scripthookvdotnet/wiki/How-Tos#spawn-propsTwice actually, once at the request, again when checking if model is loaded already.
But again, wait not working and skipping part of the code
-
@Sata
I'm not sure what you're trying to do but my video is real and whether I spawn 20 cars or 40 cars consecutively they will spawn immediately, accurately, and they will persist.I don't care what crosire does, I'm not a programmer, but if something works faultlessly for me I'm happy. I don't request models although I do requests for complex animations. I rarely use waits and I don't use nolonger needed because I've tested with and without it and have never noticed any memory advantages.
-
@JohnFromGWN I'm trying to spawn multiple cars in sequence using a script, I'll pass the vehicle hashes and expect one car to be spawned for each hash
I can spawn multiple vehicles once the model is loaded:
Are you using SHVDN3 3.6.0 on your project?
-
@Sata
I don't understand why your code doesn't work.Crosire gives an example which you might want to adapt to just vehicles
var modelsYouAreUsing = new HashSet<Model>() { PedHash.AmandaTownley, PedHash.JimmyDisanto, PedHash.TracyDisanto, VehicleHash.Sentinel, "prop_mp3_dock" }; foreach (var model in modelsYouAreUsing) { model.Request(); } while (!modelsYouAreUsing.All(x => x.IsLoaded)) { Wait(0); }
https://github.com/crosire/scripthookvdotnet/wiki/How-Tos#spawn-multiple-entities-simultaniously