Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork630
How Tos
Here you can find a collection of "How-To" guides on doing various things in C# (explained with v3 API).
Vehicles
World
Peds
Player
Other
Development
Quite a common thing to want to do is change the modifications(bumpers, spoilers, engine upgrades, etc) on a vehicle.
This can be achieved easily by changingvehicle.Mods[VehicleModType].Index property, however, there are a few things you need to do before you can use it.
If you do not do this, callingVehicle.Mods[VehicleModType] Index setter on a vehicle will do nothing.
To do this, you'll need to go to an LS Customs in-game, open the menu section for the mod you want to apply (eg. Bumpers) and count down the list to the mod you wantstarting at zero.
ie. The second mod on the list will be number1 when you call it in your code.
vehicle.Mods[VehicleModType.FrontBumper].Index=3;vehicle.Mods[VehicleModType.RearBumper].Index=1;
// 5 meters in front of the playervarposition=Game.Player.Character.GetOffsetPosition(newVector3(0,5f,0));// At 90 degrees to the players headingvarheading=Game.Player.Character.Heading-90;varvehicle=World.CreateVehicle(VehicleHash.Dubsta,position,heading);vehicle.DirtLevel=15f;vehicle.Mods.CustomPrimaryColor=Color.White;vehicle.Mods.CustomSecondaryColor=Color.Black;vehicle.Mods.LicensePlate="GTA V";vehicle.PlaceOnGround();// Set the vehicle modsVehicle.Mods.InstallModKit();vehicle.Mods[VehicleModType.FrontBumper].Index=3;vehicle.Mods[VehicleModType.RearBumper].Index=1;vehicle.Mods[VehicleModType.Hood].Index=2;
Getting Started
Before you can spawn a prop, you will first need to know the entity hash code for the prop you wish to spawn.
ObjectList.ini of DurtyFree's Data Dumps lists most object names (or props in other words) in the game. You can view images of objects at some websites, such asGTA-OBJECTS.XYZ orgtahash.ru.
You can search these lists by pressingCtrl + F in your browser and typing something, like "fire" to find the Fire Extinguisher.
Spawning the prop
To spawn the prop, you should load its model first. By explicitly loading models and waiting, you can handle the code on your own. Youcan callWorld.CreateProp (or otherWorld.Create* methods) without explicitly loading models, but that will cause the script to wait for up to 1 second if the model is not loaded at the time.
If you create some physical entities (peds, vehicles, objects) by directly calling native functions, you should load models you are planning to use before you call natives likeCREATE_PED, or the game may fail to create a physical entity and return 0.
// Declare a campfire model struct and wait for up to 250 millisecondsvarmodel=newModel("prop_beach_fire");model.Request(250);// Check the model is validif(model.IsInCdImage&&model.IsValid){// Ensure the model is loaded before we try to create it in the worldwhile(!model.IsLoaded)Script.Wait(50);// Create the prop in the worldWorld.CreateProp(model,Game.Player.Character.GetOffsetPosition(newVector3(0,5f,0)),true,true);}// Let the game release the model as no longer needed to remove it from memory.model.MarkAsNoLongerNeeded();
Spawning pickups is slightly different to spawning props.
Todo
To spawn a ped you'll use theWorld.CreatePed() method like so:
// Create a hooker 5 meters in front of the playerWorld.CreatePed(PedHash.Hooker02SFY,Game.Player.Character.GetOffsetPosition(newVector3(0,5f,0)));
To change the properties of the ped, create a variable and set this method to it:
varped=World.CreatePed(PedHash.Hooker02SFY,Game.Player.Character.GetOffsetPosition(newVector3(0,5f,0)));// Set some propertiesped.CanBeDraggedOutOfVehicle=false;ped.Armor=75;ped.Weapons.Give(WeaponHash.APPistol,500,true,true);
To make the ped do things, you'll need to use theTask property:
// Make the ped aim at the player for 5 secondsped.Task.AimAt(Game.Player.Character,5000);// Make the ped fight against the playerped.Task.FightAgainst(Game.Player.Character)
There are many Tasks that peds can perform. Use IntelliSense to explore the list ofped.Task. items.
Methods such asWorld.CreatePeds automatically block further code execution until the specified models are loaded and then create within 1 second or failed to do so within the timeout. Therefore, you need to load all the models before you can create entities simultaniously without failure (some entities appear earlier than others or being absent).
To make sure load all the models at the same time, wait for all the models to get loaded like so:
varmodelsYouAreUsing=newHashSet<Model>(){PedHash.AmandaTownley,PedHash.JimmyDisanto,PedHash.TracyDisanto,VehicleHash.Sentinel,"prop_mp3_dock"};foreach(varmodelinmodelsYouAreUsing){model.Request();}while(!modelsYouAreUsing.All(x=>x.IsLoaded)){Wait(0);}
Then you can create entities at the same time, like this:
varplayerPed=Game.Player.Character;varplayerPos=playerPed.Position;varcreatedPeds=newList<Ped>();varcreatedProps=newList<Prop>();foreach(varmodelinmodelsYouAreUsing){if(model.IsPed){// Create peds 1 meters around the playervarcreatedPed=World.CreatePed(model,playerPos+Vector3.RandomXY());// Warning: World.Create* still may return null for there being too many entities in the gameif(createdPeds!=null){createdPeds.Add(createdPed);}}// assume the model is for a prop. In practical, you may need to check extra stuff other than Model.IsPed and Model.IsVehicleelse{varcreatedProp=World.CreateProp(model,playerPos+Vector3.WorldUp*2f,false,false);if(createdProp!=null){createdProps.Add(createdProp);}}model.MarkAsNoLongerNeeded();}// immediately mark as no longer needed since this snippet is just a example oneforeach(varcreatedEntityincreatedPeds.Cast<Entity>().Concat(createdProps)){createdEntity.MarkAsNoLongerNeeded();}
To change the skin of the player, you first need to load a ped model into memory, then assign it to the player.
// Request Devin Weston's character modelvarcharacterModel=newModel(PedHash.Devin);characterModel.Request(500);// Check the model is validif(characterModel.IsInCdImage&&characterModel.IsValid){// If the model isn't loaded, wait until it iswhile(!characterModel.IsLoaded)Script.Wait(100);// Set the player's model (a new ped for player will be created)Game.Player.ChangeModel(characterModel);Game.Player.Character.Style.SetDefaultClothes();}// Let the game release the model from memory after we've assigned it.characterModel.MarkAsNoLongerNeeded();
NOTE:
ped.Modeldoes not have a setter so you cannot directly set it withped.Model = characterModel
To teleport a player to a location, simply change the player'sPosition property like so:
Game.Player.Character.Position=newVector3(402.807f,3175.139f,53.14062f);
NOTE: This also works for teleporting peds, vehicles, and props.
Todo
Todo
Sometimes ScriptHookVDotNet (SHVDN) will not directly support something you want to do. In this circumstance, you'll need to call native functions to do this yourself.
For example, theVehicle class in SHVDN does not currently have aRadio property to allow you to change the radio station or enable/disable the radio.
To do this you will need to use theFunction.Call() method, passing in the hash code and the parameters.
To find out what the hash value is and the parameters that you need, take a look atGTA V Native DB byalloc8or.
If you click theAUDIO button and search forSET_VEHICLE_RADIO_ENABLED withCtrl + F, you can find the hash info and parameters.
NOTE: If you do not know the friendly name of a hash, click to expand the category that you think the hash will be in, then press
Ctrl + Fto search for it. For example, click "AUDIO", then pressCtrl + Fto search for "radio" to find all radio-related functions.
From this box, we can see thatSET_VEHICLE_RADIO_ENABLED takes two parameters -> a vehicle handle, and a toggle boolean. From this, we can then disable the radio for the player's current vehicle like so:
// Pass in the Hash name and the parameters listed on Native DB.Function.Call(Hash.SET_VEHICLE_RADIO_ENABLED,Game.Player.Character.CurrentVehicle,false);
If you want to return something when calling a native, you must cast theFunction.Call to the type you want to return by putting the type inside of angle brackets, like so:Function.Call<int>().For example:
vartest=Function.Call<int>(GTA.Native.Hash.CREATE_SYNCHRONIZED_SCENE,Game.Player.Character.Position.X,Game.Player.Character.Position.Y,Game.Player.Character.Position.Z-1,0,0,180,2);
We wanted to return the synchronized scene id, so we cast theFunction.Call to anint. Now we can access the return value.
Some native functions takes pointer parameters likefloat* and writes values at pointer addresses of certain parameters, such asGET_GROUND_Z_FOR_3D_COORD. In that case, you can get values with theOutputArgument class, as well as raw pointers.For example (safe code):
varplayerPos=Game.Player.Character.Position;// no need to call the getter of `Ped.Position` 3 times (if do so, the code performs slower)floatgroundZ=-200f;// -200f is the lowest limit of Z coord in the stock gameusing(varoutArg=newOutputArgument())// with using statements, the Dispose method will be called on OutputArgument instances and this avoids the OutputArgument finalizer getting called (the finalizer of OutputArgument dispose internal resource much slower than the Dispose method){if(Function.Call<bool>(Hash.GET_GROUND_Z_FOR_3D_COORD,playerPos.X,playerPos.Y,playerPos.Z,outArg,true,false))// Get ground (strictly collided CBuilding position below the position passed) height if found{groundZ=outArg.GetResult<float>();}}
with unsafe code, you can achieve the same goal faster than the safe code like this:
floatgroundZ=0;if(!Function.Call(Hash.GET_GROUND_Z_FOR_3D_COORD,playerPos.X,playerPos.Y,playerPos.Z,&groundZ,true,false)){groundZ=-200f;}
Getting values with pointers can also reduce heap allocations and the garbage collector (GC) will be pressured less than the safe code. It will take less time garbage collecting with the unsafe code (althoughInputArgument is defined as class in v3 and earlier APIs).
Although a leak of native headers revealed all the native function names that are present in v1.0.2699.0 along with the parameter names for them, sometimes you may need to call native functions with raw hashes where theNative.Hash enum does not contain the plain English hash name. Or, you want to call them with raw hashes for some other reasons.
For example, you can callSET_ARTIFICIAL_LIGHTS_STATE by passing the raw hash value of0x1268615ACE24D504 like so:
Function.Call((Hash)0x1268615ACE24D504,true);
NOTE: You can call
Hash._0x1268615ACE24D504in v2 API, but you cannot call like this in v3 API and hashes named likeHash._0x1268615ACE24D504will not be available in later API versions.
If you find a hash on Native DB that takes a parameter of typeAny, it is highly likely that no-one has figured out what the parameter type is yet.
You'll need to do some experimenting with passing in different values to see if something works.
The hash name will sometimes give you clues as to what the parameters should be. For example, if the hash is calledSET_ARTIFICIAL_LIGHTS_STATE and takes oneAny type param, it's highly likely that the parameter is a boolean, to toggle the artificial lights state on or off.
NOTE: If you figure out what the parameter types are,create new pull requests on the Native DB’s GitHub repository! This will help everyone else who uses the Native DB.
To speed up development of your GTA V mod, you can set up a post-build event in Visual Studio to automatically copy your compiled mod DLL to yourscripts folder.
To do this, right-click on your project and click "Properties".
In the screen that appears, click "Build Events" on the left, then in the "Post-build event command line" textbox enter the following:
COPY "$(TargetPath)" "C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\scripts"NOTE: You will need to enter the correct path to your scripts folder.