Display mini inventory based on Pickups

I’ve been working on a Script to display some kind of mini inventory that displays my “active” items, then i would be able to fast switch between them. I call it a Pocket.

If you’ve ever played Minecraft or Terraria you’ll know wham i’m trying to do.

The approach i got until now is something based on an array of indexes to my real inventory (backpack), these indexes will point to some ItemPack, an ItemPack is an Item with some quantity. So, i have a Stack of pocketInstances, which role is to whenever the pocket changes, it Destroy all instances and create new ones.

Also, each frame the pocketInstances position are updated related to the camera position.

Here is the code approach i’m using at the moment, but probably isn’t the best way thinking about performace, so if you have some tips or another way to reach it, i would appreciate your opinion.

using UnityEngine;
using System.Collections.Generic;

public class PocketDisplay : MonoBehaviour
{
	private GameObject steveGO;
	private Backpack steveBackpack;
	private SteveControl steve;
	public GameObject fakeBlock;

	private Stack<GameObject> pocketInstances;
	private int[] lastPocket;

	private Transform cam;

	void Start()
	{
		pocketInstances = new Stack<GameObject>();
		lastPocket = new int[10];
	}

	void Awake()
	{
		steveGO = GameObject.FindWithTag("Player");
		steveBackpack = steveGO.GetComponent<Backpack>();
		steve = steveGO.GetComponent<SteveControl>();

		cam = Camera.main.transform;
	}

	void Update()
	{
		UpdateInstancesPosition();

		if (SamePocket())
			return;

		ClearPocketInstances();

		for (int i = 0; i < lastPocket.Length; i++)
		{
			GameObject instance;

			Vector3 pos = new Vector3(cam.position.x - 5 * 0.7f + i * 0.7f, cam.position.y - 4.5f, 0f);
			int packIndex = lastPocket*;*
  •  	instance = Instantiate(packIndex < 0 ? fakeBlock : steveBackpack.packs[packIndex].Item.gameObject, pos, Quaternion.identity) as GameObject;*
    
  •  	if (packIndex < 0)*
    
  •  		Destroy(instance.GetComponent<BoxCollider2D>());*
    
  •  	pocketInstances.Push(instance);*
    
  •  }*
    
  • }*

  • private bool SamePocket()*

  • {*

  •  int[] currentPocket = steve.Pocket;*
    
  •  for (int i = 0; i < lastPocket.Length; i++)*
    
  •  {*
    

if (lastPocket != currentPocket*)*
* {*
* lastPocket = (int[]) currentPocket.Clone();*
* return false;*
* }*
* }*

* return true;*
* }*

* private void ClearPocketInstances()*
* {*
* while (pocketInstances.Count > 0)*
* Destroy(pocketInstances.Pop());*
* }*

* private void UpdateInstancesPosition()*
* {*
* if (pocketInstances.Count == 0)*
* return;*

* Stack aux = new Stack();*
* int offset = pocketInstances.Count - 1;*

* while (pocketInstances.Count > 0)*
* {*
* aux.Push(pocketInstances.Pop());*
_ aux.Peek().transform.position = new Vector3(cam.position.x - 5 * 0.7f + offset * 0.7f, cam.position.y - 4.5f, 0f);
* offset–;
}*_

* while (aux.Count > 0)*
* {*
* pocketInstances.Push(aux.Pop());*
* }*
* }*
}
Thanks for all.

Don’t clone int arrays. Array.Copy them.

And why do you use a Stack<> instead of a List<> - it seems you kinda have to work hard(offset, aux push out, push in) to simulate the functionality you would have with a List<>

If you transform.parent all your pocket GOs under one GO representing the pocket, you only have to move one GO.

If you display the pocket GOs on their own cam, you don’t need to move them at all - see Minecraft Mech #48: Let's Program - YouTube