Advertisement
LeeMace

Weapon Dictionary (practice)

Jun 2nd, 2023
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.19 KB | Gaming | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3.  
  4. public class Weapon {
  5.  
  6.     //make dictionary of soldiers and weapons
  7.     private Dictionary<Soldier, string> weaponsDictionary = new();
  8.  
  9.     public void AddToDictionary(Soldier key, string value) {
  10.  
  11.         weaponsDictionary.Add(key, value);
  12.     }
  13.  
  14.     public void RemoveFromDictionary(Soldier key) {
  15.  
  16.         if (weaponsDictionary.ContainsKey(key)) {
  17.             weaponsDictionary.Remove(key);
  18.         } else {
  19.             Debug.LogError($"The key {key} does not exist in the dictionary");
  20.         }
  21.     }
  22.  
  23.    
  24.  
  25.     public int CountTheDictionary() {
  26.  
  27.         return weaponsDictionary.Count;
  28.     }
  29.  
  30.     public string GetWeaponsDictionary() {
  31.  
  32.         string contents = "";
  33.         foreach (KeyValuePair<Soldier, string> kvp in weaponsDictionary) {
  34.  
  35.             contents += kvp.Key + ": " + kvp.Value + "\n";
  36.             // Debug.Log($"Key = {kvp.Key} Value = {kvp.Value}");
  37.         }
  38.         return contents;
  39.     }
  40.  
  41.     public void ShowAllDictionaryValueInfo() {
  42.  
  43.         foreach (KeyValuePair<Soldier, string> kvp in weaponsDictionary) {
  44.  
  45.             Debug.Log($"Key: {kvp.Key}, Value: {kvp.Value}");
  46.         }
  47.     }
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement