[C#][Tutorial] Basics of creating a script PART 1
Hello guys, I wanted to create a good script tutorial, which will explain the basics of ScriptHookVDotNet, the most amazing library crosire has created. Lets start.
NOTE: You NEED to know the basics of .NET/C# to continue. There are a lot of good courses on the net, google them and you will find them.
There isn't much of software involved here, the only thing you need is Visual Studio. You can get the paid version, but i prefer Visual Studio Community.
That download gives you everything you need.
Open up Visual Studio and you will be greeted with a screen like this:
Click New Project, under start. You will have a screen like this:
Make sure to select Class Library, like in the above pic. Then put your mods name. I have put in Tutorial_Script. After you are done, hit OK.
Now, you will be brought to the script:
There are a few things you have to do now. To setup your script, you will need to add a reference to ScriptHookVDotNet.dll. To add that right-click 'references' in the Solution Explorer and click Add Reference. This will open this dialog:
Click Browse, and browse to where you put your ScriptHookVDotNet.dll and click Add. The dll will then appear like so:
Make sure it is checked. Before we do anything else, we also need to add another reference. To do that, go to the Assemblies tab and browse for 'System.Windows.Forms.dll':
Make sure it is checked, if not, check it, and click OK. Now, we need to add a couple of using namespaces. Copy mine:
After that, we need to make out script inherit GTA.Script, which needs to be done if you want your script to load ingame.
Now we need to add the constructor. The constructor runs once when the game starts, so here is what all of the stuff should go where you want it to run at the start. This is also we will put Tick and KeyDown events:
You can see the red lines indicating an error. The error is that we have just declared the events, but did not put anything related to it. So lets do that now.
There, error fixed. Now, this script will load ingame. But this doesnt do anything. Lets fix that. For example.... lets say by the press of a key, we can set a player's wanted level to 0. ScriptHookVDotNet(i will abbreviate that to SHVDN later on) has a very awesome Player class, which handles our player. This is in the Game class. We can access our Player properties using Game.Player since SHVDN makes it so easy for us. So now, we'll code the the functionality. But wait! We also need to detect the key press! That is where out KeyDown event comes in, it triggers every key press. And the KeyEventArgs class contains all the info about the key press. So, if we do e.KeyCode, we can get what key was pressed. With a simple if statement, we can code this too.
So now, at the press of a key, our wanted level resets to 0. Do note that the above code will come in the onKeyDown function. Basically, Game.Player.WantedLevel is an integer that holds the players wanted level. We can get or set that integer to anything we want. But now, if the player is already on 0 wanted levels, this code will still be run. In this scenario, it doesnt do any harm, but if it were for a huge scenario, like if a mission is already completed but you can do it again, this may cause problems. So now, we will code something to notify the player that they are already non-wanted:
The UI.ShowSubtitle method has one other overload where it also takes the duration. The normal method takes in a string, which will be displayed like a subtitle. So now, if we have wanted levels, only then will this code be run. Lets test this ingame. To do that, we first have to build the script, like this:
Click Build Solution, and if you did everything correctly, you will have an output saying Build Succeeded.
If it is, then you can copy the file to your scripts folder. The path is here:
C:\Users(your-username)\Documents\Visual Studio 2015\Projects\Tutorial_Script\Tutorial_Script\bin\Debug
Copy Tutorial_Script.dll to your scripts folder:
After that, run the game, and press H. If you had no wanted levels, this is what should've came up:
Otherwise, it should've reset it to 0. If it did, congrats you made your first script! The next tutorial will be a lot more in-depth, it will contain native calling and other nice features of SHVDN. Until then, bye!
@GTAVModder4Life thank you, this will help me alot. seen so much tutorials and they are all the same. spawn somebody, make him your bodyguard every trainer could do that. i want to know more about vehicle stuff, like open all doors with a button wich i can define in ini, or indicator buttons, engine on of. hope you make a tutorial about this. at least in the comments we can talk about different stuff and everybody can help each other by building the first scripts, so THANK YOU again for this basic tutorial and the next tutorials
@all i know there are already scripts, mods, trainers which can handle my wishes but i really want to do a script by myself and include just the functions i need/use in my mod. hope with the upcoming tutorials i get enough knowledge to do this.
@TobsiCred Very awesome ideas, I will surely cover them in the next parts.
@GTAVModder4Life oh man, this means a lot to me. one year ago i started to make my first own scripts, its easy to do just one line, like i said spawn this vehicle, make someone your bodyguard, but nobody explains the interesting stuff or even new stuff and nobody answer the questions but for much people on youtube or gta5mods it can help alot show your cars and stuff when you can open all doors by one key (like enro), or change the color by one key, without having a ugly menu over the half video (like 1w1w1w). i am looking forward to your tutorial. very excited.
@GTAVModder4Life nice and simple if you go to the Project tab --> Properties, you can enter the directory that you want to copy your .DLL to whenever you build your solution so you don't have to copy it manually every time! I think it's under Build Events, can't check right now.
@stillhere Will cover that in the next tutorial.
@GTAVModder4Life looks very helpful, I'll be trying this out layer
Now, we need to add a couple of using namespaces. Copy mine:
couple of what? Instructions need proofreading.
@poisonwind34 if you are going to follow these tutorials, you NEED basic C# skills. Look at the note.
I got the problem that KeyCode is not having any defenition..
Does anyone know how to fix it? i have the exact Code like in the Thread above
If i were to create a tool instead of a script, how would i connect the tool to gta5.exe?