Implementing Get All Weapons Script - KimonoBoy/SHVDNTutorial-NucleiLite GitHub Wiki
We're almost done with this tutorial - let's implement our final script to our menu, the Give All Weapons Script
- Copy and Paste the below code
using System;
using System.Linq;
using System.Windows.Forms;
using GTA;
using GTA.UI;
using LemonUI;
using LemonUI.Menus;
namespace NucleiLite
{
public class Main : Script
{
ObjectPool menuPool = new ObjectPool();
NativeMenu mainMenu = new NativeMenu("NucleiLite", "Main Menu");
NativeMenu playerMenu = new NativeMenu("NucleiLite", "Player Menu");
NativeMenu vehicleSpawnerMenu = new NativeMenu("NucleiLite", "Vehicle Spawner Menu");
NativeMenu weaponsMenu = new NativeMenu("NucleiLite", "Weapons Menu");
bool canSuperJump = false;
public Main()
{
CreateMainMenu();
CreatePlayerMenu();
CreateVehicleSpawnerMenu();
CreateWeaponsMenu();
AddMenusToPool();
KeyDown += OnKeyDown;
Tick += OnTick;
}
private void CreateMainMenu()
{
mainMenu.AddSubMenu(playerMenu);
mainMenu.AddSubMenu(vehicleSpawnerMenu);
mainMenu.AddSubMenu(weaponsMenu);
}
private void CreatePlayerMenu()
{
// Fix Player
NativeItem itemFixPlayer = new NativeItem("Fix Player", "Restores Player's Health and Armor");
itemFixPlayer.Activated += (sender, args) =>
{
Game.Player.Character.Health = Game.Player.Character.MaxHealth;
Game.Player.Character.Armor = Game.Player.MaxArmor;
Notification.Show("Health and Armor Restored!");
};
playerMenu.Add(itemFixPlayer);
// Invincible
NativeCheckboxItem checkBoxInvincible = new NativeCheckboxItem("Invincible", "Your character can no longer die.");
checkBoxInvincible.CheckboxChanged += (sender, args) =>
{
Game.Player.Character.IsInvincible = checkBoxInvincible.Checked;
Notification.Show($"Invincible: {Game.Player.Character.IsInvincible}");
};
playerMenu.Add(checkBoxInvincible);
// Wanted Level
NativeListItem<int> listItemWantedLevel = new NativeListItem<int>("Wanted Level", "Adjust Player's Wanted Level.", 0, 1, 2, 3, 4, 5);
listItemWantedLevel.ItemChanged += (sender, args) =>
{
Game.Player.WantedLevel = args.Object;
};
playerMenu.Add(listItemWantedLevel);
// Super Jump
NativeCheckboxItem checkBoxSuperJump = new NativeCheckboxItem("Super Jump", "Allows the Player to Jump higher than a building.");
checkBoxSuperJump.CheckboxChanged += (sender, args) =>
{
canSuperJump = checkBoxSuperJump.Checked;
};
playerMenu.Add(checkBoxSuperJump);
}
private void CreateWeaponsMenu()
{
NativeItem itemGiveAllWeapons = new NativeItem("Give All Weapons", "Gives the Player all Weapons.");
itemGiveAllWeapons.Activated += (sender, args) =>
{
Ped character = Game.Player.Character;
foreach (WeaponHash weaponHash in Enum.GetValues(typeof(WeaponHash)))
{
character.Weapons.Give(weaponHash, 100, false, true);
character.Weapons[weaponHash].Ammo = character.Weapons[weaponHash].MaxAmmo;
}
Notification.Show("Player gained all weapons with max ammunition.");
};
weaponsMenu.Add(itemGiveAllWeapons);
}
private void CreateVehicleSpawnerMenu()
{
foreach(VehicleHash vehicleHash in Enum.GetValues(typeof(VehicleHash)))
{
NativeItem itemSpawnVehicle = new NativeItem(vehicleHash.ToString(), $"Spawns a {vehicleHash} right in front of you!");
itemSpawnVehicle.Activated += (sender, args) =>
{
Ped character = Game.Player.Character;
Model vehicleModel = new Model(vehicleHash);
vehicleModel.Request();
Vehicle vehicle = World.CreateVehicle(vehicleModel, character.Position + character.ForwardVector * 3.0f, character.Heading + 90.0f);
vehicleModel.MarkAsNoLongerNeeded();
Notification.Show($"Vehicle: {vehicleHash} has been spawned!");
};
vehicleSpawnerMenu.Add(itemSpawnVehicle);
}
}
private void AddMenusToPool()
{
menuPool.Add(mainMenu);
menuPool.Add(playerMenu);
menuPool.Add(vehicleSpawnerMenu);
menuPool.Add(weaponsMenu);
}
private void OnTick(object sender, EventArgs e)
{
menuPool.Process();
if (canSuperJump)
{
Game.Player.SetSuperJumpThisFrame();
}
}
private void OnKeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F5)
{
mainMenu.Visible = !mainMenu.Visible;
}
}
}
}
-
Build and Reload()
-
Activate the Give All Weapons item in the Weapons Menu
We've changed our Give All Weapons Script a tidy bit - instead of equipping each weapon and then adding the ammunition to the current weapon we use indexing to set the ammunition - let me explain
private void CreateWeaponsMenu()
{
NativeItem itemGiveAllWeapons = new NativeItem("Give All Weapons", "Gives the Player all Weapons.");
itemGiveAllWeapons.Activated += (sender, args) =>
{
Ped character = Game.Player.Character;
foreach (WeaponHash weaponHash in Enum.GetValues(typeof(WeaponHash)))
{
character.Weapons.Give(weaponHash, 100, false, true);
character.Weapons[weaponHash].Ammo = character.Weapons[weaponHash].MaxAmmo;
}
Notification.Show("Player gained all weapons with max ammunition.");
};
weaponsMenu.Add(itemGiveAllWeapons);
}
Creates one item - Give All Weapons, when this item is activated, do the following
Ped character = Game.Player.Character;
Create a reference to the Game.Player.Character
foreach (WeaponHash weaponHash in Enum.GetValues(typeof(WeaponHash)))
{
character.Weapons.Give(weaponHash, 100, false, true);
character.Weapons[weaponHash].Ammo = character.Weapons[weaponHash].MaxAmmo;
}
Go through all Weapons in the collection of weapons
character.Weapons.Give(weaponHash, 100, false, true);
Use the reference variable we created earlier to access the Game.Player.Character, then the Weapons of that Character and then we use the Method Give which looks like the following
Give(WeaponHash weaponHash, int ammoCount, bool equipNow, bool isAmmoLoaded)
so the weaponHash currently being used in our loop, 100 ammo, don't equip it and load the ammunition
character.Weapons[weaponHash].Ammo = character.Weapons[weaponHash].MaxAmmo;
Since we don't equip each weapon after we've added them using the equipNow parameter, we can use indexing to access that specific weapon by providing the weaponHash inside the [] brackets
To summarize: We loop through every valid WeaponHash in our Enum of type WeaponHash, then give the character each weapon and finally updating the ammoCount of the weapon
You can see a full list of WeaponHashes at WeaponHash
If you've read the Simplified-version, the only thing we're going to cover in-depth here is the indexing part consider the following
foreach (WeaponHash weaponHash in Enum.GetValues(typeof(WeaponHash)))
{
character.Weapons.Give(weaponHash, 100, false, true);
character.Weapons[weaponHash].Ammo = character.Weapons[weaponHash].MaxAmmo;
}
More specifically the below code
character.Weapons[weaponHash].Ammo = character.Weapons[weaponHash].MaxAmmo;
The syntax character.Weapons[weaponHash] is an example of indexing in C#.
We're using the character object to access the Weapons property, which is a collection of all the weapons that the player currently has in their inventory.
In this case, we're using the [] operator to access a specific element in the character.Weapons collection, based on the weaponHash value. The weaponHash variable is used to determine the index of the weapon that we want to retrieve from the collection.
Indexing is a fundamental concept of arrays and collections in many programming languages, including C#. It allows us to access a specific element in a collection using an index value or key, and perform operations on that element as needed.
You can learn more about Arrays and Collections and Indexing
In our case - to retrieve a specific weapon from the WeaponCollection, we use the weapon's unique identifier, which is represented by the weaponHash variable. The weaponHash variable is an instance of the WeaponHash enumeration, which contains a list of all available weapon types in the game.
So, character.Weapons[weaponHash] retrieves the Weapon object that corresponds to the weapon type represented by weaponHash. We can then access properties and methods of this Weapon object to manipulate the weapon's state, for instance - changing the ammunition.
Note: In order to access the weapon by the weaponHash-indexing the weapon has to be a part of the Player's current arsenal, that is why we first Give the Weapon and then Access it by the index
We've implemented our Give All Weapons Script to our Weapons Menu - the Give All Weapons-item gives the Player all valid Weapons in the Game by going through an Array that is the entire WeaponHash-enum, once the weapon has been added to the Player we access the Weapon by using indexing and updating the ammoCount of that weapon
Previous - Implementing Spawn Vehicle Script
Next - Round-Off