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
public void AddOneStar()
{
Game.Player.WantedLevel += 1;
}
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
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 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.