Introduction to Scripting
In-game tools provided by the game like Director Mode and Rockstar Editor allow more creative spaces within GTA V. However, thanks to a large community of players who have been able to reverse engineer a big part of the game code, it is possible to gain even more control of the game world through scripts. By scripting you will be able to use functions and parameters that control the game world in much greater detail.
Preparation and Setup
-
Download Script Hook V, go to the bin folder and copy
- dinput8.dll
and ScriptHookV.dll files into your GTA V directory- C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V
. -
Download Script Hook V dot net, copy the
- ScriptHookVDotNet.asi
file and- ScriptHookVDotNet3.dll
files into your GTA V directory- C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V
-
Create a new folder in the GTA V directory and call it “scripts”.
-
Download and install
- Visual Studio Community
. Open- Visual Studio
and check the .NET desktop development package and install it. -
Run GTA V and test if Script Hook V is working by pressing
- F4
. This should toggle the console view. Try to type- Help()
and press- Enter
to get a list of available commands.
Creating a Mod File
-
Open
- Visual Studio
-
Select
- File
>- New
>- Project
-
Select
- Visual C# and Class Library (.NET Framework)
-
Give a custom file name (e.g. moddingTutorial)
-
Rename public class
- Class1
as- moddingTutorial
in the right panel Solution Explorer and click Yes on the pop-up window -
In the same panel go to
- References
and click- add References…
-
Click on
- Browse
>- browse to Downloads
-
Select
- ScriptHookVDotNet
>- ScriptHookVDotNet3.dll
and add it -
Go to
- Assemblies
and search for “forms” and select- System.Windows.forms
-
Search for “drawing” and select
- System.Drawing
-
In
- Solution Explorer
right click on your project name and select- Properties
and- Build Events
.
In the- Post-build event command line
window write:
xcopy /Y "$(ProjectDir)bin\Debug\$(ProjectName).dll" "C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto V\scripts"
- In your code file have the following lines at the very top:
using System;
using System.Windows.Forms;
using System.Drawing;
using GTA;
using GTA.Math;
using GTA.Native;
- Modify class
- moddingTutorial
to the following:
namespace moddingTutorial
{
public class moddingTutorial : Script
{
public moddingTutorial()
{
this.Tick += onTick;
this.KeyUp += onKeyUp;
this.KeyDown += onKeyDown;
}
private void onTick(object sender, EventArgs e)
{
}
private void onKeyUp(object sender, KeyEventArgs e)
{
}
private void onKeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.H)
{
Game.Player.ChangeModel(PedHash.Cat);
}
}
}
}
-
Press
- F6
to compile your code into a .dll file. The file- moddingTutorial.dll
will be created in- Documents
>- Visual Studio
>- Project
>- moddingTutorial
>- moddingTutorial
>- bin
>- Debug
and automatically copied in your GTA V- scripts
folder. -
Open GTA V, run the game in Story Mode (mods are only allowed in single player mode, not in GTA Online) and press
- H
to see if the game turns your avatar into a cat.
- Note: every time you make changes to your mod file, you need to build it again by pressing
- F6
in Visual Studio. In the game you must press- F4
to open the console, and type- Reload()
in the console for the game to reload the new script and test again the changes.
onTick, onKeyUp and onKeyDown
The main events of Script Hook V Dot Net are
The code within the
private void onTick(object sender, EventArgs e)
{
//code here will be executed every frame
}
If your function is written inside
private void onKeyUp(object sender, KeyEventArgs e)
{
//code here will be executed whenever a key is released
}
If your function is written inside
private void onKeyDown(object sender, KeyEventArgs e)
{
//code here will be executed whenever a key is pressed
}
We can specify which code is executed based on what keys are pressed/released by using an if statement to check when a specific key on the keyboard is pressed or released.
private void onKeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.H)
{
//code here will be executed whenever the key 'H' is pressed
}
}