[.NET] [C#] [SHVDN] Modding Basics! Vector3s and Vehicles Manipulation [Part 3]
-
Hello guys! Wussap. I know its been a long time since I posted a tutorial, and tbh I really don't have a good excuse for that
I was just lazy. BUT in this part of the series, we'll see how to manipulate vehicles and that sort of things.
Requirements
Knowledge of the last tutorial(s)What we'll learn to do in this part
- Learn Vector3s(i.e. what positions are called in script, and how to manipulate them)
- Spawning vehicles
- Doing stuff with them
Vector3s
There is one main thing you should know about GTA 5 or really any other 3D game in existence.. Vector3What is Vector3 you ask? Well, a vector3 consists of three float variables, X, Y and Z. All of these variables are coordinates indicating to a point in the game world. X and Y are the left and right coordinates, and the Z is the height coordinate, the 3 Dimensions. Brackeys, a game developer has a very good video explaining game math, which you should totally check out:
Spawning Cars
All world related functions, which includes spawning a car, is stored in a static class World. We can use World.CreateVehicle to spawn a specific car at a specific position(Vector3) with a specific rotation.Finally, we come to the good stuff
. To make our code organized I'll make a function for spawning cars easily:
I'll make the car spawn 5 units in front of the player, but you can edit that easily. There is a very awesome helper function in Game.Player.Character which is GetOffsetInWorldCoords. It gives you the offset in world coords(lol). I'll first make a Vector3 variable:
And then get the player:
And then use the helper function I mentioned above!
This function requires a Vector3 offset, so if we want the car to spawn 5 units in front of us, we'll use new Vector3(0, 5, 0) or if we want it, say, 5 units in front of us(lol), we'll use new Vector3(0, 0, 5). I don't know why anyone would want to use it but ¯_(ツ)_/¯.So now that we have the spawn position, lets spawn our car! To do that, we use the above function, World.CreateVehicle:
This function takes in two parameters, the Model or the model hash of the vehicle we want to spawn, and the position. We already declared a position, so all we have to do is get the model. Luckily, SHVDN has a whole enum full of all the hashes for all the vehicles in the game, and we can spawn them easily. The enum is called VehicleHash. We'll spawn the turismo.
SHVDN converts the VehicleHash into a Model, so we dont have to worry about that. As you can see, the second parameter is the spawning position, so we'll pass the one we got above.
Alright! So our function is ready but we aren't calling it anywhere. The most traditional way to test something is by keybinds sooo lets add that in. Later on, we'll learn to use INI files for configuration of keys too.
Alright! So now our script spawns a vehicle when we press U! Go ahead and test it in game. When you're done, come back and I'll teach you a few tricks you can do with the car you spawnedManipulating the vehicle
The World.CreateVehicle function returns the car it created/spawned so we can use that to get the vehicle. Lets store it in a variable:
Now, you can do literally anything you want with this vehicle! All you have to do, is type in the name of the car, then press the "dot" key(.). Look at all the things you can do!
If I explained each and every one of them, it would take days, so I wont bother
. Just experiment, see what works and what doesn't. If you're confused with anything, ask here and I'll reply back to tell you how it works.
But anyways guys, in the next tutorial, we'll learn about loading stuff from INIs. But till then bye.
-
@TobsiCred Alright, so first of all.. if you did Function.Call(Hash.SET_VEHICLE_MOD_KEY, v.Handle, 0);
then there is no need to call v.InstallModKit();
I think thats why that code isn't working.second of all. I don't need to check if it is valid because its a vanilla or unmodded vehicle. I know it will always be valid since its in the game. If you want to do add-on vehicles, you can do something like this:
void SpawnCar() { Vector3 spawnPos = Game.Player.Character.GetOffsetInWorldCoords(new Vector3(0, 5, 0)); Model addonModel = new Model("yourYFTModelNameHere"); Vehicle spawnedCar; if (addonModel.IsValid) spawnedCar = World.CreateVehicle(addonModel, spawnPos); else return; }
If the else statement is run, that means the user doesnt have the vehicle. So you can send the message there
Third of all. I am not wrapping myself inside the vehicle I spawn. So, I don't care if I'm in a vehicle, at least for this mod. Though doing v.MarkAsNoLongerNeeded() after spawning the car is good practice since it releases it from memory. I'll add that next tutorial
.