Invincible Toggle Script - KimonoBoy/SHVDNTutorial-NucleiLite GitHub Wiki

In this section we'll be creating a Script that accepts a Key input and Toggles the state of the Player's Invincibility - when Invincible the Player takes no damage from anything.

The Code

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

  2. Copy and Paste the following code to the newly created file

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

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

    public void OnKeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.I && e.Control)
        {
            if (Game.Player.Character.IsInvincible)
            {
                Game.Player.Character.IsInvincible = false;
                Notification.Show("You're no longer invincible.");
            } else
            {
                Game.Player.Character.IsInvincible = true;
                Notification.Show("You're now invincible.");
            }
        }
    }
}
  1. Test it in game by hitting CTRL + I. (Remember to Reload())

image

Code Breakdown - Simplified

We've already covered the using statements, KeyPresses and the class section in the Fix Player Script section, so let's focus solely on what's important for this specific script

if (e.KeyCode == Keys.I && e.Control)
        {
            if (Game.Player.Character.IsInvincible)
            {
                Game.Player.Character.IsInvincible = false;
                Notification.Show("You're no longer invincible.");
            } else
            {
                Game.Player.Character.IsInvincible = true;
                Notification.Show("You're now invincible.");
            }
        }

We make a conditional statement (if statement) that asks if the Key pressed corresponds to the Key I and if the Control is pressed simoultaneously, if it is true perform the following task


            if (Game.Player.Character.IsInvincible)
            {
                Game.Player.Character.IsInvincible = false;
                Notification.Show("You're no longer invincible.");
            } else
            {
                Game.Player.Character.IsInvincible = true;
                Notification.Show("You're now invincible.");
            }

We can have if-statements inside if-statements, adding an extra layer of logic to the conditions


if (Game.Player.Character.IsInvincible)

Since we want a Toggle that can toggle our Invincibility state to on/off, we ask if the Player Character is already Invincible and if the character is, run the following


                Game.Player.Character.IsInvincible = false;
                Notification.Show("You're no longer invincible.");

Turning the invincible state off by setting it to false and Display a Notification that informs the Player that we are no longer invincible


else
            {
                Game.Player.Character.IsInvincible = true;
                Notification.Show("You're now invincible.");
            }

The else keyword runs the code inside the new block - if the Character is not already invincible

In this case we set our character's invincible-state to on and Display a Notification to the Player that the character is now invincible

Summarize: We press CTRL + I and if our character is arleady invincible turn it off, if our character is not invincible turn it on and Display a Notification to the Player that corresponds to the invincible state.

Code Breakdown - Advanced

We'll quickly introduce the concept of Data Types and keywords, have a look at the below code

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

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

    public void OnKeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.I && e.Control)
        {
            if (Game.Player.Character.IsInvincible)
            {
                Game.Player.Character.IsInvincible = false;
                Notification.Show("You're no longer invincible.");
            } else
            {
                Game.Player.Character.IsInvincible = true;
                Notification.Show("You're now invincible.");
            }
        }
    }
}

Keywords

The using, if, else, public, class, false/true and void are all considered keywords - keywords are built-in and are reserved, meaning that you cannot use them as e.g. method names, each keyword serves a specific purpose.

You can learn more about Keywords and their purpose at Keywords

Data Types

                Game.Player.Character.IsInvincible = true;
                Notification.Show("You're now invincible.");

The true and the "You're now invincible." are considered Simple Data Types.

true: This Data Type is of type boolean, a boolean returns either true or false.

"You're now invincible.": This Data Type is of type string, a string represents a sequence of characters.

We'll elaborate on the concept of Data Types when we get to the concept of variables, we'll also explain how a class is like a Complex Data Type

I'll link to the following Data Types at a later time as well. And this

Conclusion

In conclusion, we have created a script that toggles the invincibility state of the player character in the game. The script listens for a specific key combination (Ctrl + I) and if pressed, toggles the player character's invincibility state. The script also displays a notification to the Player to inform them if their invincibility state has been turned on or off. We have also discussed the code breakdown, including the use of keywords, data types, and if-statements.

Previous - Fix Player Script
Next - Wanted Level Script