Single-file Weapon Selector plugin, aliased cwWeaponSelect, which replaces the default HUD weapon selection with a scrolling list of five weapons and an information box for the highlighted one.

On the client, PlayerBindPress handles the invprev, invnext, slot and attack binds and runs the ShouldWeaponMenuOpen, OnWeaponIndexChange and OnWeaponSelected hooks. The server part only adds the selectweapon console command, which switches to the weapon at a given index.

Summary

Hooks

Draws the information box for the weapon highlighted in the weapon selector.
Called when the HUD is painted; draws the open weapon selector and animates scrolling through it.
Called to check whether a HUD element should draw; hides the default weapon selection.
Builds the five weapon selector entries centered on a weapon index.
Called when the selected weapon index changes; opens the selector and starts the scroll animation.
Called when a weapon is picked in the selector; closes the selector at once.
Called when the local player presses a bind; drives the weapon selector.
Called to check whether the weapon selector may open; blocks it while the attack key is held.
Think()shared
Called every frame; fades the weapon selector out and closes it five seconds after it was last used.

Hooks

cwWeaponSelect:DrawWeaponInformation(itemTable, weapon, x, y, alpha)

shared gamemodes/catwork/plugins/sh_weaponselect.lua:78

Draws the information box for the weapon highlighted in the weapon selector.

Shows the item's description, the clip and reserve ammo, and the weapon's instructions, purpose, contact and author when set. The box is vertically centered on y. The PreDrawWeaponSelectionInfo hook gets the box's info map (drawBackground, weapon, x, y, width, height, alpha) and can change it before drawing. Does nothing without an item table or a valid weapon.

Parameters

  • itemTable Item The weapon's item, as returned by item.GetByWeapon
  • weapon Weapon The weapon to describe
  • x Number Left edge of the box
  • y Number Vertical center of the box
  • alpha Number Opacity, 0-255

cwWeaponSelect:HUDPaint()

shared gamemodes/catwork/plugins/sh_weaponselect.lua:202

Called when the HUD is painted; draws the open weapon selector and animates scrolling through it.

Shows five weapons around the selected one, scaled by their distance from it, and the selected weapon's information box beside them.

cwWeaponSelect:HUDShouldDraw(element)

shared gamemodes/catwork/plugins/sh_weaponselect.lua:192

Called to check whether a HUD element should draw; hides the default weapon selection.

Parameters

  • element String The HUD element name

Returns

  • Boolean False for CHudWeaponSelection, otherwise nil

cwWeaponSelect:MakeDisplay(index, tab)

shared gamemodes/catwork/plugins/sh_weaponselect.lua:319

Builds the five weapon selector entries centered on a weapon index.

Indexes wrap around the local player's weapon list. Each entry has weapon, scale, x and y.

Parameters

  • index Number Index of the center weapon in the player's weapon list
  • tab Boolean optional, defaults to nil True to return the entries instead of storing them in PLUGIN.Display

Returns

  • List<Map> The entries when tab is true, otherwise nothing

cwWeaponSelect:OnWeaponIndexChange(oldIndex, index)

shared gamemodes/catwork/plugins/sh_weaponselect.lua:351

Implements hook OnWeaponIndexChange

Called when the selected weapon index changes; opens the selector and starts the scroll animation.

Wrapping from the last weapon to the first (or back) scrolls in the direction of travel.

Parameters

  • oldIndex Number The previously selected index
  • index Number The newly selected index

cwWeaponSelect:OnWeaponSelected(index)

shared gamemodes/catwork/plugins/sh_weaponselect.lua:385

Implements hook OnWeaponSelected

Called when a weapon is picked in the selector; closes the selector at once.

Parameters

  • index Number Index of the picked weapon

cwWeaponSelect:PlayerBindPress(player, bind, bIsPressed)

shared gamemodes/catwork/plugins/sh_weaponselect.lua:405

Called when the local player presses a bind; drives the weapon selector.

invprev, invnext and the slot binds open the selector or move the selection (pressing the same slot again, or slots 1 and 2 alternately, steps through the list); attack while it is open selects the weapon through the selectweapon command and runs the OnWeaponSelected hook. Slot binds go to an open cpgui_radiomenu instead. Nothing happens in vehicles, without a weapon, or when the ShouldWeaponMenuOpen hook returns false.

Parameters

  • player Player The local player
  • bind String The bind pressed
  • bIsPressed Boolean Whether the bind was pressed rather than released

Returns

  • Boolean True when the selector handled the bind, which blocks it

cwWeaponSelect:ShouldWeaponMenuOpen(player, oldIndex, newIndex)

shared gamemodes/catwork/plugins/sh_weaponselect.lua:376

Implements hook ShouldWeaponMenuOpen

Called to check whether the weapon selector may open; blocks it while the attack key is held.

Parameters

  • player Player The local player
  • oldIndex Number The currently selected index
  • newIndex Number The index being selected

Returns

  • Boolean False while attacking, otherwise nil

Called every frame; fades the weapon selector out and closes it five seconds after it was last used.

Defined in