-
Notifications
You must be signed in to change notification settings - Fork 40
API Getting Started
- This page is specifically for developers to demonstrate how to get started working with the API.
Before creating a new project, make sure you have the latest:
- Visual Studio
- .NET Framework v4.8 or later
Open Visual Studio and select Create new project/solution.
- Select All Languages and Change to C# (You can use any .NET Language, this page is just demonstrating C#)
- Find "Class Library (.NET Framework)" NOT .NET Core OR .NET Standard, the API is built with the .NET framework!
- Fill out Project info, location, and framework version.
Now you should have an empty .cs source file
- Now that we have an empty class file, we need to make it read-able by the hook.
- First, add a reference to ScriptHookRDRNetApi.dll
Select Browse on the left, then press "Browse" on the bottom right, double click your ScriptHookRDRNetApi.dll
- We also need to reference "System.Windows.Forms"
- Press OK, now go back to "Class1.cs" and add this to the top:
- The project is now referenced, but we need to make the class inherit "Script" so the hook can read it.
- We need a class method, this is where we will tell the hook what events point to what method.
We have 2 main events to setup: Tick, and KeyDown
Start by typing "Tick +=", this will bring up an auto fill recommendation by Visual Studio, press TAB to insert.
This will implement a new method, but before moving on, lets do the same for "KeyDown"
Type "KeyDown +=" under "Tick += Class1_Tick" and TAB to insert the new method
We have one less thing to do for setup, we need to tell the hook how often we want to refresh the tick. This is done by assigning the variable "Interval"
Interval is measured in ms, for a constant tick, assign 1.
Now you should have a new setup script file, to check, make sure it looks something like this:
- In order to call native functions, you need to make sure "using RDR2.Native;" is in your source.
- To call a native, use Function.Call(), if your native returns a value, use
Function.Call<return type>()
- Named natives can be called using the Hash enum. i.e Hash.PLAYER_PED_ID
- In the case of a native without a name, we can call the native directly, and casting it to the Hash enum
(Hash)0xAE99FB955581844A
- Some things are implemented into the API, to make your code easier. You can find out what all is implemented using Intellisense.
- For an example, lets set our player to ragdoll in the KeyDown event.
We will be using WinForms KeyCodes.
For now, we can use Keys.J
Now, everytime you press J in game, whatever is inside the { } brackets will be executed 1 time.
Lets make the player ragdoll. For this we need to get the players ped.
You can do this with the API:
And finally, we only have 1 native to call.
Using the nativeDB, I found the ragdoll native we need to call.
SET_PED_TO_RAGDOLL(Ped ped, int time1, int time2, int ragdollType, BOOL p4, BOOL p5, BOOL p6)
time1 represents how long to ragdoll the ped, and time2 represents how long until the ped can get up.
We will use 0 for ragdollType, and the last 3 params will be false.
This will cause the player to ragdoll for 1 second.
-
Once your code is complete, you can compile it in Visual Studio by pressing CTRL + SHIFT + B or by pressing Build -> Build Solution
-
This will export a .dll file to project/bin/Debug
-
Place the .dll in root/scripts/ folder and start Red Dead Redemption 2.