Fix Player Script - KimonoBoy/SHVDNTutorial-NucleiLite GitHub Wiki

In this section, we will create a straightforward Script that allows the Player to restore their Health and Armor by pressing a button.

Note: While we will briefly cover the Setup and Testing process again, this will not be done in every Script as it remains the same for all updates. Just keep in mind that whenever you add new scripts or modify existing ones, a reload is required.

Setup

Just like we did in our Hello GTA V Script from the previous section, we need to create a new .cs file. We'll call this one FixPlayer.cs

  1. File -> New File... (or **CTRL + Alt + Windows + N)

image

  1. Give it a name FixPlayer.cs and hit Enter

image

  1. In the Browse window that appears locate your scripts folder inside your GTA V folder

Create File - Browse Fix Player

  1. Press the Create File button and you should be taken to the following .cs file

image

The Code

I'll provide you with the code, we'll break down the code in the Code Breakdown section - but for now, let's just make the FixPlayer script work

  1. Copy and Paste the following code to your newly created file

FixPlayer.cs

using System;
using System.Windows.Forms;
using GTA;
using GTA.UI;

public class FixPlayer : Script
{
    public FixPlayer()
    {
        KeyDown += OnKeyDown;
    }

    public void OnKeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.F && e.Control)
        {
            Game.Player.Character.Health = Game.Player.Character.MaxHealth;
            Game.Player.Character.Armor = Game.Player.MaxArmor;
            Notification.Show("Health and Armor Restored!");
        }
    }
}
  1. Save the file

Test

Just like we did before we need to reload our Scripts

  1. Tap back into your Grand Theft Auto V-environment and bring up the Console (F4 by default)

  2. Type the exact Reload() and hit Enter

image

  1. Once Reloaded hold down CTRL and press F and you should see the following

image

Also notice how your Health and Armor is restored.

Code Breakdown - Simplified

This is a simple script, yet if you're a newcomer, this might seem overwhelming, not to worry - we'll break it down.

using System;
using System.Windows.Forms;
using GTA;
using GTA.UI;

These first few lines of code are importing libraries that allow the code to interact with the game and the user interface.


public class FixPlayer : Script

Next, a class called FixPlayer is created. This class will inherit from a class called Script, which is part of the ScriptHookVDotNet library. This means that the FixPlayer class can access and use features from the Script class to manipulate the game.


public FixPlayer()
{
    KeyDown += OnKeyDown;
}

The FixPlayer class has a constructor method called FixPlayer, which is called when an instance of the class is created (when a new FixPlayer object is created). Inside this constructor method, there is an event handler called KeyDown.

An event is something that happens - e.g. the user presses a button.


KeyDown += OnKeyDown;

The KeyDown event is triggered when a key is pressed down. The += operator is used to add the OnKeyDown method as a listener to this event. This means that when the KeyDown event is triggered, the OnKeyDown method will be called.


public void OnKeyDown(object sender, KeyEventArgs e)

The OnKeyDown method takes two arguments (accepts two inputs): an object called sender and a KeyEventArgs object called e. The sender object is used to identify the source of the event. The KeyEventArgs object contains information about the key that was pressed.


if (e.KeyCode == Keys.F && e.Control)
{
    Game.Player.Character.Health = Game.Player.Character.MaxHealth;
    Game.Player.Character.Armor = Game.Player.MaxArmor;
    Notification.Show("Health and Armor Restored!");
}
  • Game: Is refering to the Game.
  • Player: Is refering to the Player (You)
  • Character: Is refering to the character the Player (You) is currently playing on. (Also known as the Ped)
  • Health: The current Health of your character.
  • MaxHealth: The maximum Health capacity this character has.
  • Armor: The current Armor of your character.
  • MaxArmor: The Max Armor of the Player (Max Armor is related to your Player stats, not your current character)

Inside the OnKeyDown method, there is an if statement that checks whether the F key and (&&) the Control key were pressed simultaneously.

Note: You can see a full list of valid KeyCodes at System.Windows.Forms.Keys

If this condition is true, the Character the Player is currently Playing as has its Health and Armor restored to their maximum values.

The Notification.Show() method is used to display a message on the screen, letting the Player know that their health and armor have been restored.

Code Breakdown - Advanced

For those of you who wish to learn more about C# in general and understand some of the core concepts.

Object Oriented Programming

C# is a popular Object-Oriented Programming language that is used to develop a wide range of software applications. The essence of Object-Oriented Programming is that software applications are composed of various smaller objects that work together to create a cohesive software experience.

For instance, in C#, you can create objects by creating new instances of e.g. the class - FixPlayer.cs and HelloGTAV.cs, objects can work together to form a larger software application. These objects are usually created using classes, which define their properties and behaviors.

You can learn more about Object-Oriented Programming at Object Oriented Programming

Using Directives and Namespaces

using System;
using System.Windows.Forms;
using GTA;
using GTA.UI;

Objects can also rely on other libraries or frameworks to perform specific functions. For example, the ScriptHookVDotNet is a framework that provides access to the Grand Theft Auto V (GTA V) game engine in .NET. To use this framework, you need to add a reference to it in your code using the using statement.

By adding a reference to ScriptHookVDotNet, your FixPlayer.cs object can access the GTA functionality provided by the framework. The using statement is, therefore, a powerful feature in C# that allows you to leverage the functionality of other libraries and frameworks to create more robust and efficient software applications.

Note: The directive after the using statement is called a namespace, by referencing the namespace of which the class resides we gain access to the functionality of those classes

You can learn more about namespaces and using statements here namespaces & using directives

Classes

public class FixPlayer : Script

In C#, a class is a blueprint or a template that defines the properties and behaviors of an object. It is a fundamental concept of object-oriented programming (OOP) and is used to create objects that can contain data and functionality.

You can learn more about classes at Classes and Classes, Structs and Records

Access Modifiers

The keyword public in C# is an access modifier that specifies that the class is accessible from any code in the application. Other common access modifiers include private and protected, which restrict the accessibility of the class to only certain parts of the code.

In short: Access modifiers defines what each class can access. The private only allows access from within the class of which the method or property is defined. The protected keyword allows access for all classes that inherits from this class and the class itself. Public allows access from everywhere.

You can learn more about access modifiers at Access Modifiers

Inheritance

The : operator in the code is used to indicate inheritance. In this case, the class FixPlayer is inheriting from the base class - Script. This means that FixPlayer can use any properties or methods defined in the Script class, in addition to any properties or methods that it defines itself.

Basically with Inheritance we're saying: Whatever the class we inherit from can do, this class can do the same as well as whatever this class defines itself.

Script is a class in the ScriptHookVDotNet library, which provides scripting capabilities for the Grand Theft Auto V Game. By inheriting from the Script class, the FixPlayer class can use the functionality provided by the ScriptHookVDotNet library to modify the behavior of the game.

You can learn more about Inheritance at Inheritance

Constructors

    public FixPlayer()
    {
        KeyDown += OnKeyDown;
    }

This is the constructor method of the FixPlayer class. A constructor is a special method in a class that is called when an instance of the class is created.

You can read more about constructors at Constructors

Events

In this case, the constructor method is adding an event handler called OnKeyDown to the KeyDown event. The KeyDown event is triggered when a key is pressed down in the game.

The += operator is used to add the OnKeyDown method as a listener to this event. The += operator is also known as a addition assignment operator, you can learn more about operators at Operators

This means that when the KeyDown event is triggered, the OnKeyDown method will be called.

You can learn more about events and event listeners at Events

If-statement

  public void OnKeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.F && e.Control)
        {
            Game.Player.Character.Health = Game.Player.Character.MaxHealth;
            Game.Player.Character.Armor = Game.Player.MaxArmor;
            Notification.Show("Health and Armor Restored!");
        }
    }
  • Game: Is refering to the Game.
  • Player: Is refering to the Player (You)
  • Character: Is refering to the character the Player (You) is currently playing on. (Also known as the Ped)
  • Health: The current Health of your character.
  • MaxHealth: The maximum Health capacity this character has.
  • Armor: The current Armor of your character.
  • MaxArmor: The Max Armor of the Player (Max Armor is related to your Player stats, not your current character)

The OnKeyDown method is an event handler that is called when the KeyDown event is triggered. It takes two arguments: an object called sender and a KeyEventArgs object called e.

Inside the OnKeyDown method, there is an if statement that checks whether the F key and the Control key were pressed simultaneously. If this condition is true, the player's character's health and armor are restored to their maximum values.

You can learn more about the if-statement at If statement

The Game.Player.Character.Health and Game.Player.Character.Armor statements are setting the health and armor, of the Character currently being played by the Player, to their maximum values, which are retrieved from the Game.Player.Character.MaxHealth and Game.Player.MaxArmor properties, respectively.

Finally, the Notification.Show() method is used to display a message on the screen, letting the Player know that their health and armor have been restored.

To summarize: The FixPlayer class is designed to modify the behavior of the Grand Theft Auto V game by restoring the Player Character's health and armor when a certain key combination is pressed. It does this by using the ScriptHookVDotNet library and the KeyDown event to trigger the OnKeyDown method, which in turn restores the character's health and armor and displays a notification on the screen.

Conclusion

In this section, we have created a script called FixPlayer that allows the Player to restore their health and armor by pressing a button. We have gone through the setup and testing process, and also provided a breakdown of the code for those who are new to C# or wish to understand the concepts in more depth. This simple script demonstrates the power of object-oriented programming and the ability to leverage existing libraries and frameworks to enhance software applications. With this knowledge, you can create even more complex and robust scripts for Grand Theft Auto V.

Previous - Learning C# and ScriptHookVDotNet
Next - Invincible Script