- Home /
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[i];
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[i] != currentPocket[i])
{
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<GameObject> aux = new Stack<GameObject>();
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.
Answer by HappyMoo · Jan 13, 2014 at 11:43 AM
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 http://www.youtube.com/watch?v=oOZ55FLHKH4
Thanks a lot, @Happy$$anonymous$$oo
About using a List ins$$anonymous$$d of Stack, I began coding thinking that the Stack would be useful, but then it tricked me out. I'll change it to a List.
I'll watch the video to understand how i can create a dedicated camera to the pocket.
Thanks again.
Yes, a stack is useful if you need a LIFO (last in, first out) data structure, like lets say a monster should always attack the last attacker first, but shouldn't forget about previous attackers...
Then attackers get pushed on the stack and you peek() at the top to know who to attack. Once somebody isn't reachable anymore or he died, you pop one off the stack and look if you had another attacker before him etc.
Your answer
![](https://koobas.hobune.stream/wayback/20220613130241im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Very simple inventory script... 0 Answers
Limiting an inventory system + dragging items in inventory window... 0 Answers
Marching cubes in Unity? 1 Answer
BurgZerg Arcade inventory system tutorials... Incomplete!! 1 Answer