Wanted Level Script - KimonoBoy/SHVDNTutorial-NucleiLite GitHub Wiki
We've made a script that can Restore Health and Armor, a script that can Toggle the Player Invincible and now we want to make a script that can adjust the Player's Wanted Level
The Code
Just like we did the other scripts we're going to create a new .cs file that represents our WantedLevel.cs script
-
Create a new file called WantedLevel.cs
-
Copy and Paste the below code into the new WantedLevel.cs file
using System;
using System.Windows.Forms;
using GTA;
using GTA.UI;
public class WantedLevel : Script
{
public WantedLevel()
{
KeyDown += OnKeyDown;
}
public void OnKeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Add || e.KeyCode == Keys.Oemplus)
{
AddOneStar();
} else if (e.KeyCode == Keys.Subtract || e.KeyCode == Keys.OemMinus)
{
RemoveOneStar();
}
}
public void AddOneStar()
{
Game.Player.WantedLevel += 1;
}
public void RemoveOneStar()
{
Game.Player.WantedLevel -= 1;
}
}
Note: Yes, Oemplus and OemMinus are intended to be differently called, for some weird reason the Keys are using different naming conventions....
-
Save the file and Reload()
-
Press either the + or - key or press the NumPad + or NumPad - key
Your Wanted Level should now increase or decrease depending on your Key input.
Note: A full list of keys can be found at System.Windows.Forms.Keys
Code Breakdown - Simplified
This code is a bit different from the previous scripts, let's break the differences down
if (e.KeyCode == Keys.Add || e.KeyCode == Keys.Oemplus)
We check if the Add (+) Key from the NumPad is pressed or (||) the Oemplus (+) Key from the regular Keyboard is pressed, if either is pressed we call this method
AddOneStar();
Which performs the following task
public void AddOneStar()
{
Game.Player.WantedLevel += 1;
}
Takes the current Wanted Level of the Player and adds one to it. The += takes whatever the current value is and add the amount to the right to it.
else if (e.KeyCode == Keys.Subtract || e.KeyCode == Keys.OemMinus)
{
RemoveOneStar();
}
You can have as many else if in succession as you'd like - this code does the same as the one explained just earlier, but instead of adding a Wanted Level it removes one as seen by the code below
public void RemoveOneStar()
{
Game.Player.WantedLevel -= 1;
}
Code Breakdown - Advanced
Let's have a look at the new things going on here
Methods
public void AddOneStar()
{
Game.Player.WantedLevel += 1;
}
In C#, a method is a block of code that performs a specific task or set of tasks. Methods are used to group related code together and make it reusable.
The AddOneStar() and the RemoveOneStar() are methods that accepts zero arguments (inputs) - the method performs any tasks inside its code block defined by the { }
Methods also need some access modifier specified, this determines if the method should only be available to the class of which the method resides or if it should be accessible from somewhere else. In our case we specified our method as public, meaning the access level is public for everyone calling this class
The void
keyword specifies that the method returns nothing. We'll learn more about return values later.
You can learn more about methods at Methods
Operators & Expressions
In C#, an expression is a combination of values, variables, operators and method calls that evaluates to a single value. Expressions can be used to compute values, make decisions, and perform a variety of other operations in a program.
Consider the below example:
int x = 5; // assignment expression
int y = 7; // assignment expression
int z = x + y * 2; // arithmetic expression
bool isGreaterThan = x > y; // comparison expression
string greeting = "Hello, " + name; // string concatenation expression
In the first line, int x = 5;
is an example of an assignment expression. It assigns the value 5 to the variable x.
In the third line, int z = x + y * 2;
is an example of an arithmetic expression. It uses the + and * operators to perform addition and multiplication, respectively, and assigns the resulting value to the variable z.
In the fourth line, bool isGreaterThan = x > y;
is an example of a comparison expression. It uses the > operator to compare the value of x to the value of y, and assigns the resulting boolean value to the variable isGreaterThan.
In the fifth line, string greeting = "Hello, " + name;
is an example of a string concatenation expression. It combines the string "Hello, " with the value of the variable name, and assigns the resulting string to the variable greeting.
Expressions are an important part of C# programming, as they allow you to perform complex computations and make decisions based on the results of those computations.
The += operator is an operator that adds whatever value at the right-hand side of the equal sign to the left-hand side of the equal sign - for instance 1 += 5
would result to 6
You can learn more about operators and expressions and their functionality at Operators & Expressions
Conclusion
In this section we've shortly been introduced to Methods and the or operator ||, we've also learned how to add values to existing values by the use of the += operator.