Counting Items With Lyra Inventory System in Unreal Engine Multiplayer Game

In this post I’d like to share shortly a simple way how to enhance Lyra inventory system so that inventory items could be counted. I made this for a simple co-op Multiplayer game.

collecting-coins_collectibles-gameplay

The key idea: The number of items collected by a player is calculated as a personal score and adds to the team score.

collected items score table

The project uses Blueprints only – no C++ code was changed to save time. For future development, it’s reasonable to create a separate inventory system for items (based on ALyraWeaponSpawner class)

Enhanced Inventory System parts

  1. Using Lyra inventory system via DataAsset (ULyraPickupDefinition), which is created for each type of collectible items, e.g. I named it CollectiblePickupData_Coin_MBCG.
  2. Lyra inventory element (ULyraInventoryItemDefinition) for “coins”, e.g. I named it ID_Collectible_Coin_MBCG. It is used in CollectiblePickupData_Coin_MBCG
  3. GameplayTags used for:
    • Tracking collected items per player and team (using GameplayTagStack)
    • Communication between classes (using GameplayMessageSubystem)

Added Gameplay Tags

  • MBCG.Collectibles.Coin.QuantityCollected (counts collected items of specific type)
  • MBCG.Collectibles.Collected.Message (connects to team statistics)
  • MBCG.Collectibles.Collected.Score (counts all collected items). used for both player (with PlayerState) and team score (with LaraTeamSubsystem)

Note that player statistics on picked up items don’t reset after death (respawn) because the LyraInventoryManagerComponent is used in PlayerState (you can set it up in ShooterCore feature plugin).

Handling collected items of different types

B_WeaponSpawner (ALyraWeaponSpawner) is responsible for items spawning and pickup logic (this is how it’s done in Lyra). I just copied B_WeaponSpawner to create a custom spawner for collectibles (named it B_CollectibleSpawner_MBCG).

As a quick implementation ALyraWeaponSpawner C++ class can be used as a parent for B_CollectibleSpawner_MBCG blueprint. But it may be reasonable to create a separate C++ class fo that. I did not create a separate class in this small project.

The key functionality exposed to blueprints lies in GiveWeapon function (in the context of my project it should be rather named GiveCollectible though).

This function is called from ALyraWeaponSpawner::AttemptPickUpWeapon_Implementation under ROLE_Authority, so we are good to make changes on server:

The key elements are in focus in this function are:

  • AddItemDefinition node: adds entry in the Lyra inventory list
  • AddStatTagStack node for ExistingWeaponInstance (read: ExistingCollectibleInstance).
  • Broadcasting message (so that it could be handled in individual player’s and team’s stat processing)

See comments on the screenshot for more details:

Handling individual players score and teams score

Whereas in B_WeaponSpawner we handle counting of different types of collectibles, B_ShooterGameScoring_Base is where we handle counting for total score for individual players and teams. This runs after HasAuthority check.

The added functionality is basically slightly modified logic of eliminations (kills) counting in Lyra:

Displaying stats in Widget

Modified a bit LyraActivatableWidgets:

  • W_SB_TeamStat (Team score output via specifying MBCG.Collectibles.Collected.Score for the output widget)
  • W_SB_PlayerRow_TDM (Individual score output: specifying the MBCG.Collectibles.Collected.Score Gameplay Tag responsible for the score)

P.S.: Found a somewhat similar and neat solution for a stack container in C++. Also leaving here another link for Lyra-based inventory (Russian)