Give all Weapons Script - KimonoBoy/SHVDNTutorial-NucleiLite GitHub Wiki

We've covered a lot in the last section about VehicleSpawner, this one is a tougher one as well - here we'll introduce loops which we will use to loop through all Weapons in GTA V and give the Player each and every one of them. Buckle up.

The Code

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

  2. Copy and Paste the following code

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

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

    public void OnKeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.NumPad0)
        {
            GiveAllWeapons();
        }
    }

    public void GiveAllWeapons()
    {
        foreach (WeaponHash weaponHash in Enum.GetValues(typeof(WeaponHash)))
        {
            Game.Player.Character.Weapons.Give(weaponHash, 100, true, true);
            Game.Player.Character.Weapons.Current.Ammo = Game.Player.Character.Weapons.Current.MaxAmmo;
        }
    }
}
  1. Reload() and hit NumPad0 (if you don't have a NumPad assign a different Key in the KeyDown method)

image

Code Breakdown - Simplified

We've created a method that loops through a collection (in this case all WeaponHashes) and gives each Weapon to the Player - let's break it down

        foreach (WeaponHash weaponHash in Enum.GetValues(typeof(WeaponHash)))
        {
            Game.Player.Character.Weapons.Give(weaponHash, 100, true, true);
            Game.Player.Character.Weapons.Current.Ammo = Game.Player.Character.Weapons.Current.MaxAmmo;
        }

A foreach-loop is a loop in C# that goes through all items in a collection - e.g. an Array[] (Enum.GetValues returns an Array)


Game.Player.Character.Weapons.Give(weaponHash, 100, true, true);

The method Give is declared here Weapons.Give it looks like the following

public Weapon Give(WeaponHash hash, int ammoCount, bool equipNow, bool isAmmoLoaded)

So we must pass in a WeaponHash hash (the Weapon to give), an int ammoCount (how much ammo the weapon has), bool equipNow (whether or not to equip the weapon immediately), bool isAmmoLoaded (whether or not the magazine is currently loaded)


Game.Player.Character.Weapons.Current.Ammo = Game.Player.Character.Weapons.Current.MaxAmmo;

Since we define in our Give() method that we want to equipNow, each Weapon is equipped as we loop through the Array, this way we can access the current weapon equipped and set the ammo to that of the Maximum Value that specific weapon can have.

To summarize: We've created a loop that goes through all valid WeaponHashes, we then Give the Player each Weapon we go through, at last we add the maximum ammo capacity to the weapon

Code Breakdown - Advanced

    public void GiveAllWeapons()
    {
        foreach (WeaponHash weaponHash in Enum.GetValues(typeof(WeaponHash)))
        {
            Game.Player.Character.Weapons.Give(weaponHash, 100, true, true);
            Game.Player.Character.Weapons.Current.Ammo = Game.Player.Character.Weapons.Current.MaxAmmo;
        }
    }
public void GiveAllWeapons()

This declares a public method named GiveAllWeapons, which takes no parameters and returns void (nothing).


foreach (WeaponHash weaponHash in Enum.GetValues(typeof(WeaponHash)))

Creates a foreach loop that iterates over each value in the WeaponHash enum, which represents all of the weapons that can be used in the game.

You can learn more about for-each-loops at Foreach loops

WeaponHash is an enum type that is defined in the ScriptHookVDotNet library, and it contains values for all of the weapons in the game, such as pistols, rifles, shotguns, rocket launchers, etc.

You can learn more about Enums at Enums

Enum.GetValues(typeof(WeaponHash)) returns an array of all the values in the WeaponHash enum, which are then iterated over by the foreach loop. The loop assigns each value to the weaponHash variable in turn, which will be used later to give the player's character each weapon.

You can learn more about Arrays and Collections at Arrays[] and Arrays and Collections


Game.Player.Character.Weapons.Give(weaponHash, 100, true, true)

Gives the player's character the specified weapon, with 100 rounds of ammunition and immediately equips the weapon.

The Give method is declared here and looks like the following public Weapon Give(WeaponHash hash, int ammoCount, bool equipNow, bool isAmmoLoaded)

The weaponHash variable represents the current weapon that is being given to the player's character in this iteration of the foreach loop. The other arguments to the Give() method are the starting amount of ammunition (100), a boolean flag that indicates whether the weapon should be equipped by the player immediately (true), and a boolean flag that indicates whether the weapon's magazine should be loaded with ammunition (true).

To Summarize: Gives the player's character the current weapon in the iteration of the foreach loop, with 100 rounds of ammunition and immediately equips the weapon. This process is repeated for every weapon in the WeaponHash enum, allowing the code to give the player's character all of the weapons in the game.

Game.Player.Character.Weapons.Current.Ammo = Game.Player.Character.Weapons.Current.MaxAmmo

Sets the current ammunition of the player's current weapon to its maximum capacity.


Game.Player.Character.Weapons.Current

Returns the Weapon that represents the player's current weapon, and Ammo and MaxAmmo are properties of the Weapon class that represent the current ammunition and maximum ammunition capacity of the weapon, respectively.

By setting the Ammo property to the value of the MaxAmmo property, the code ensures that the player's current weapon is fully loaded with its maximum capacity of ammunition.

Note: We could access each weapon without equipping them, by referencing the index of the weapon, we'll cover indexing later.

Conclusion

We've made a WeaponScript that Gives the Player all Weapons - we've also learned about loops, arrays and enums

Previous - Spawn Vehicle Script
Next - NucleiLite Mod Menu