Keeping it Simple

How not to do it:

  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6. /// <summary>
  7. /// Needs work.
  8. /// </summary>
  9. public class Inventory : MonoBehaviour {
  10.  
  11.   private Dictionary<string, int> backpack;
  12.  
  13.    private bool showInventory;
  14.  
  15.    void Awake () {
  16.     }
  17.  
  18.  void Start () {
  19.         DisplayInventory (true);
  20.    }
  21.  
  22.  public void DisplayInventory (bool state) {
  23.         showInventory = state;
  24.  }
  25.  
  26.  public bool RemoveObject (string iObject, int number) {
  27.         try {
  28.           backpack[iObject] = backpack[iObject] - number;
  29.             if (backpack[iObject] <= 0) {
  30.                backpack.Remove (iObject);
  31.          }
  32.           return true;
  33.        } catch (Exception ex) {
  34.            print ("[" + this.GetType ().ToString () + "] The object " + iObject + " does not exist.");
  35.           return false;
  36.       }
  37.  
  38.  }
  39.  
  40.  public bool AddObject (string iObject, int number)
  41.  {
  42.       if (backpack.ContainsKey (iObject)) {
  43.           backpack[iObject] += number;
  44.        } else {
  45.            backpack[iObject] = number;
  46.         }
  47.       return true;
  48.    }
  49.  
  50.  void OnGUI () {
  51.         if (showInventory) {
  52.            int counter = 0;
  53.            foreach (KeyValuePair<string, int> kv in backpack)
  54.            {
  55.               string displayString = kv.Key + ": " + kv.Value.ToString ();
  56.              GUI.Label (new Rect (10, counter * 40, 200, 30), displayString);
  57.                counter++;
  58.          }
  59.       }
  60.   }
  61. }

How to do it instead:

  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4.  
  5. public class ChickenCounter : MonoBehaviour
  6. {
  7.  private int counter = 0;
  8.    public GameObject particleEffect;
  9.  
  10.  void OnTriggerEnter (Collider hit)
  11.  {
  12.       if (hit.tag == "bonus") {
  13.             counter++;
  14.          Instantiate (particleEffect, hit.transform.position, hit.transform.rotation);
  15.           Destroy (hit.gameObject);
  16.       }
  17.   }
  18.  
  19.  void OnGUI () {
  20.         GUI.Label(new Rect(10,10,200,30), "Noms: " + counter);
  21.    }
  22.  
  23. }

Seriously, don’t

use the first code snippet. It’s a needlessly complex way of doing it, and – bonus points! – it doesn’t even compile in Unity.

Yet. I have plans on completing it. Just not today, because it’s trying to accomplish way more than finally would be visible in the game.

Bonus Information for People in my Class

If you used the PropertyList class in Magnus’ infamous SimulationSystem, you should feel right at home with C#’s Dictionary class. It is basically the same thing, and it can be used in a similar way.

Only difference: you have to define what types you intend to use as Keys and Values in the form of Dictionary<type,type> upon instantiating it. Thankfully, this also means that you don’t have to assign a type whenever you get data out of the dictionary again.

You can get access to the Dictionary class by adding using System.Collections.Generic to the top of your C# source file.

Social Tags:

Add new comment