Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by woodoo · Jan 13, 2014 at 09:18 AM · inventoryalgorithm

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image woodoo · Jan 13, 2014 at 02:37 PM 1
Share

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.

avatar image HappyMoo · Jan 13, 2014 at 03:11 PM 1
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges