Log in to reply
 

[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.

    Software:
    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.
    https://www.visualstudio.com/

    That download gives you everything you need.

    Open up Visual Studio and you will be greeted with a screen like this:
    alt text
    Click New Project, under start. You will have a screen like this:
    alt text
    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:
    alt text
    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:
    alt text
    Click Browse, and browse to where you put your ScriptHookVDotNet.dll and click Add. The dll will then appear like so:
    alt text
    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':
    alt text
    Make sure it is checked, if not, check it, and click OK. Now, we need to add a couple of using namespaces. Copy mine:
    alt text
    After that, we need to make out script inherit GTA.Script, which needs to be done if you want your script to load ingame.
    alt text
    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:
    alt text
    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.
    alt text
    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.
    alt text
    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:
    alt text
    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:
    alt text
    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:
    alt text
    After that, run the game, and press H. If you had no wanted levels, this is what should've came up:
    alt text
    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!



  • @TobsiCred Very awesome ideas, I will surely cover them in the next parts.



  • @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.



  • 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.



  • Hello,

    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

    TXH



  • If i were to create a tool instead of a script, how would i connect the tool to gta5.exe?



  • try this simple...c# tutorial



  • @AHK1221 The thing public class Class1 : Script isn't a thing





  • @TranslateK1ng34 You're missing opening/closing brackets and have a misplaced closing bracket.

    At the end of "public class Class1 : Script" press Enter then {
    2 lines below "public void onKeyDown(object sender, KeyEventArgs e)", remove the }
    Add a } at the very end.

    Picture representation.



  • @Jitnaught I did that and it's saying the key down stuff doesn't exist



  • @TranslateK1ng34 Screenshot?





  • @TranslateK1ng34 of code?





  • @TranslateK1ng34 The curly braces have to surround the code. You have a { } right next to each other right after "public class Class1 : Script". That closing curly brace } should be at the end of the code instead of right next to the opening curly brace. You also need a closing curly brace for the namespace.

    Picture representation.



  • @Jitnaught it worked thank you!



  • @TranslateK1ng34 If you're having these kinds of problems I would suggest reading some beginners tutorials, read a book, or even watch some YouTube tutorials on C#. It will really help out with fully understanding how programming works. Once you have even a basic understanding you can start understanding what your code is doing, why it's doing it, and how to make it do what you want it to do. Once you've gotten the basics done you can hone these skills by making more and more complicated scripts or making WinForms/console apps. Programming is not something you can just start doing in a day, so if you're wanting to do it and do it well you better be ready to invest some time into learning it.



  • Eu quero fazer um script para bloquear / desbloquear porta / portão. Mas eu não sei como escolher uma porta na minha frente. E se eu puder fazer isso, penso, o próximo passo será tornar-se estático em estático ou dinâmico.
    Mas, meu problema é: como selecionar uma porta na minha frente para aplicar essas mudanças?
    Obrigado, de qualquer maneira xD



  • @Jitnaught how can I make a configuration file for a script like .ini file? and also I want to learn how to play a sound on keypress, thank you.



  • @nicholejan
    For settings:

    yourVariable = Settings.GetValue("Category", "Name", "defaultValue");
    

    Which will get the settings from this INI file:

    [Category]
    Name = value
    

    For playing of sound there are a few ways you can go about it, although I'll only show you two.

    1. Use System.Media.SoundPlayer:
    SoundPlayer player = new SoundPlayer("location/to/sound.wav");
    player.Load();
    
    player.Play();
    

    SoundPlayer can only play WAV files however, and you don't get much control over how to play the sound. But, it's very easy to use in comparison to option #2...

    1. Use the 3rd-party library NAudio:
      Tutorial: https://github.com/naudio/NAudio/blob/master/Docs/PlayAudioFileWinForms.md


  • @Jitnaught I can't make it work, if I put that line of code and include System.Media.SoundPlayer on the library, GTAV doesn't read my script anymore, can you give me a sample script which plays the sound on keypress?



  • @nicholejan

    using System;
    using System.Media;
    using System.Windows.Forms;
    using GTA;
    
    namespace TestScript
    {
        class Main : Script
        {
            public Main()
            {
                KeyDown += Main_KeyDown;
            }
    
             public void Main_KeyDown(object sender, KeyEventArgs e)
             {
                  if (e.KeyCode == Keys.L)
                  {
                       new SoundPlayer("./scripts/sound.wav").Play();
                  }
             }
         }
    }

Log in to reply
 

Looks like your connection to GTA5-Mods.com Forums was lost, please wait while we try to reconnect.