HotBarHUD
HUDElement that displays the player's inventory hotbar slots. Subscribes to the possessed character's CharacterInventory to reflect item changes and selection state in real time.
Definition
Namespace: Paragon.Townskeep.HUD
Assembly: Townskeep.dll
public class HotBarHUD : HUDElementInheritance: SerializedMonoBehaviour → ParagonBehaviour → ParagonUIBehaviour → HUDElement → HotBarHUD
Remarks
HotBarHUD bridges the inventory data model and the UI slot display. It discovers HotBarSlotUI children in Awake() and auto-selects slot 0 in Start().
Activation Flow
When the HUD is activated (OnActivated()), the component:
Gets the current player's possessed
CharacterviaPlayer.Current.TryGetPossession<Character>()Subscribes to
character.Inventory.Inventory.OnInventoryChangedfor item add/remove/change eventsSubscribes to
character.Inventory.CurrentSelectionChangedfor selection highlight changes
Item Display
When the inventory fires OnInventoryChanged(index, item, amount):
If
itemis not null →slots[index].SetItem(item, amount)(shows the item icon)If
itemis null →slots[index].Clear()(hides the icon)
Selection
SetSelection(int) deactivates the previous slot's highlight and activates the new one. Each HotBarSlotUI swaps between its Normal and Active background sprites.
Quick Lookup
Get current selection index
hotbar.CurrentSelection
Set selection programmatically
hotbar.SetSelection(index)
Access from HUD
hud.TryGetHUDElement<HotBarHUD>(out var hotbar)
Properties
CurrentSelection
int
Index of the currently selected hotbar slot
Methods
OnActivated (override)
Subscribes to the possessed character's inventory change and selection change events.
Behavior:
Calls
Player.Current.TryGetPossession<Character>()to get the current characterSubscribes to
character.Inventory.Inventory.OnInventoryChangedSubscribes to
character.Inventory.CurrentSelectionChanged
SetSelection
Switches the visual selection highlight from the current slot to the new one.
selection
int
Index of the slot to select
Behavior:
Calls
OnDeactivated()on the previously selectedHotBarSlotUIUpdates
currentSelectionto the new indexCalls
OnActivated()on the newly selectedHotBarSlotUI
Common Pitfalls
No bounds checking on slot index
SetSelection() and OnInventoryChanged() index directly into the slots array without bounds checking. If the inventory has more slots than HotBarSlotUI children in the hierarchy, an IndexOutOfRangeException will occur.
OnActivated assumes a possessed Character exists
OnActivated() calls Player.Current.TryGetPossession<Character>(). If the player does not currently possess a character (returns false), the subscriptions are silently skipped — but no existing slot state is cleared either.
No unsubscription on deactivation
OnActivated() subscribes to inventory events but there is no corresponding OnDisabled() override to unsubscribe. If the HUD is deactivated and reactivated, or the possessed character changes, event handlers may accumulate or reference stale characters.
Start() sets selection to 0
The initial selection is set in Start() via SetSelection(0). If OnActivated() runs before Start() and a selection change event fires, the selection may briefly flicker to the correct index and then snap back to 0.
Examples
Accessing the Hotbar from Game Code
Hierarchy Setup
See Also
HotBarSlotUI — individual slot element
HUDElement — abstract base class
HUD — parent container
Hotbar Overview — subsystem architecture
Last updated