Super Jump Script - KimonoBoy/SHVDNTutorial-NucleiLite GitHub Wiki

We've covered a few different scripts such as Fix Player and Invincible all of which are scripts that only needs to be called once. But the GTA Game Engine also allows us to call scripts every frame - which is roughly 60 times per second. We use something called Ticks to perform these kind of tasks. The advanced section of this script will elaborate on the use of Ticks, variables and string-manipulation

The Code

  1. Create a new .cs file called SuperJump.cs

  2. Copy and Paste the below code to your SuperJump.cs script

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

public class SuperJump : Script
{
    bool canSuperJump = false;

    public SuperJump()
    {
        KeyDown += OnKeyDown;
        Tick += OnTick;
    }

    public void OnKeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.H && e.Control)
        {
            canSuperJump = !canSuperJump;
            Notification.Show("Super Jump: " + canSuperJump.ToString());
        }
    }

    public void OnTick(object sender, EventArgs e)
    {
        if (canSuperJump)
        {
            Game.Player.SetSuperJumpThisFrame();
        }
    }
}
  1. Reload() and hit CTRL + H and you should see the following

image

Code Breakdown - Simplified

A few new things are going on here, let's break it down

    bool canSuperJump = false;

We assign a variable that is of type bool

Variable: A variable is a self-defined named storage in memory - a variable can store any value (even entire classes like FixPlayer) and be called for later use. A variable can be re-assigned and changed, unless we explicitly tell it not to (more on readonly and constants in Nuclei - Complete Mod).

bool: A bool is a Data Type that defaults to either true or false

So we create a variable that is of type bool and the default value of this variable is false


    public SuperJump()
    {
        KeyDown += OnKeyDown;
        Tick += OnTick;
    }

Just like the KeyDown is an event that is triggered when a Key is Pressed down the Tick is an event that is triggered many times a second - always listening for changes.


    public void OnKeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.H && e.Control)
        {
            canSuperJump = !canSuperJump;
            Notification.Show("Super Jump: " + canSuperJump.ToString());
        }
    }

When the Player presses the H key and the Control key simoultaniously we change the value of the canSuperJump variable we defined earlier - the ! to the right-hand side of the equal sign says that we should set the canSuperJump variable to whatever value it is currently not.


    public void OnKeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.H && e.Control)
        {
            if (canSuperJump == false)
            {
                canSuperJump = true;
            } else
            {
                canSuperJump = false;
            }
            Notification.Show("Super Jump: " + canSuperJump.ToString());
        }
    }

The Notification.Show() takes the text - Super Jump: and appends (adds) the value of the canSuperJump variable to the sentence. The .ToString() is used to display the value of a type as a string


    public void OnTick(object sender, EventArgs e)
    {
        if (canSuperJump)
        {
            Game.Player.SetSuperJumpThisFrame();
        }
    }

Remember how our Tick checks the code inside it roughly 60 times a second? So this method is constantly updating - meaning that many times a second we ask if canSuperJump is true and if it is we call the Game.Player.SetSuperJumpThisFrame(); which allows the Player to Super Jump that one frame.

Code Breakdown - Advanced

Let's introduce some new concepts in this section

Variables

bool canSuperJump = false;

Variables are core concepts of almost any programming language. Variables allows us to store values in one part of our code and re-use them or change their value in other parts. Variables are also great for helping the overall code structure and readability, and they make it easier to access values multiple times. To create a variable in C# we first specify the Data Type, then the variable-name and then we assign a value to it by the = operator - the value assigned must always correspond to the defined Data Type

You can learn more about variables at Variables and more about Simple Data Types at Simple Data Types

Later we'll cover more Complex Data Types like classes

Ticks

    public SuperJump()
    {
        KeyDown += OnKeyDown;
        Tick += OnTick;
    }

We've already covered the KeyDown event, so let's focus solely on the Tick event. In ScriptHookVDotNet, the Tick refers to a method that is called once per game tick (which occurs approximately every 16ms).

The Tick method is a method defined in the Script class (from which we inherit), which is the base class for all scripts in ScriptHookVDotNet. It is called by the game engine every tick and is where developers can put code that they want to run continuously, such as checking for input from the player, updating the position of a character or object, or checking for collisions. This means that everything inside the code block below runs everything inside it roughly 60 times per second

    public void OnTick(object sender, EventArgs e)
    {
        if (canSuperJump)
        {
            Game.Player.SetSuperJumpThisFrame();
        }
    }

! (Not Operator)

    public void OnKeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.H && e.Control)
        {
            canSuperJump = !canSuperJump;
            Notification.Show("Super Jump: " + canSuperJump.ToString());
        }
    }

When the Player presses the H key and the Control key simoultaniously we change the value of the canSuperJump variable we defined earlier - the ! to the right-hand side of the equal sign says that we should set the canSuperJump variable to whatever value it is currently not. The ! is called a not operator. So if it's false we should set it to not false (true) and if it's true we should set it to not true (false)

You can learn more about the not operator (!) and operators in general at Operators

String Concatenation

Notification.Show("Super Jump: " + canSuperJump.ToString());

When working with strings we can do something called string concatenation - the idea is simple: Take one string and "add" another string to it. Consider the following:

"Super Jump: " + canSuperJump.ToString()

We have one string with the following content "Super Jump: " and another string with the following content canSuperJump.ToString(), the + puts those two strings together (in order) and produces an entirely new string that results to the following
"Super Jump: true" if canSuperJump is true otherwise "Super Jump: false"

You can learn more about string-manipulation and string-concatenation at String Concatenation

Note: There are a lot of better ways to manipulate strings and their output, one of those is called String Interpolation which will cover later!

Conclusion

In this section we've been shortly introduced to variables, string concatenation and Ticks - we'll elaborate on all concepts later! We've created a SuperJump script that checks every Tick if the canSuperJump is true and if it is allows us to SuperJump

Previous - Wanted Level Script
Next - Spawn Vehicle Script