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 malkere · Jul 02, 2014 at 03:56 AM · inventoryfindrpgkeyhashtable

How to call value by key when key is an object?

I've been playing with dictionaries, array, hashtables, etc. but I can't find just what I'm looking for.

basically I want to have a key that is from class Item, ex: Item.StonePick and a value assigned to it for how many that player has in the inv, ex: 3

now StonePick is an item so it has a _name and lots of variables that would be great to access so I can display "Stone Pick" not StonePick etc. when I got through the list and print out the player inventory, or quests, or achievments or whatever.

I have it semi-working via linq with a hashtable _playerQuests:

 public void Act(){
         if(Player._playerQuests.ContainsKey(Quests.Cliff01)) {
             int place;
             for (int i = 0; i < Player._playerQuests.Count; i++){
                 if (Player._playerQuests.Keys.ElementAt(i) == Quests.Cliff01){ place = i; }
             }
             if (Player._playerQuests.Values.ElementAt(place) == 1){ _chat = "1"; }
         }
         else { _chat = "0"; }
     }

but that means it has to go through everything all the time just so I can call from the index number [] to access the Item._name etc.

There must be a better way??

What I really just want is: Dictionary playerInventory; and Dictionary playerQuests; etc.

Can anyone offer some suggestions? Should I just setup predefined Arrays of 200 until my hard coded Quests go over 200 then change that array to 300 and keep a log of what quest is where? then I could hard call everything directly, but that would destroy any sort of loose search functionality....?

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 gstock · Jul 02, 2014 at 05:07 AM

You can make your Item object implement System.IEquatable and override the method GetHashCode(). That way your Item can be used as a Key based on the ID or Name that you set to it.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 
 public class Item : System.IEquatable<Item> {
 
     public Item(string name) {
         Name = name;
     }
 
     public string Name {
         get;
         set;
     }
 
     public bool Equals(Item other) {
         return Name == other.Name;
     }
 
     public override int GetHashCode(){
         return Name.GetHashCode();
     }
 }
 
 public class ObjectKey : MonoBehaviour {
 
     IDictionary<Item, uint> items = new Dictionary<Item, uint>();
 
     // Use this for initialization
     void Start () {
         Item item1 = new Item("Item1");
         Item item2 = new Item("Item2");
 
         items[item1] = 2;
         items[item2] = 45;
 
         foreach (Item item in items.Keys) {
             Debug.Log(item.Name + " -> Qty: " + items[item] + " " + item);
         }
 
         Item dummySearchItem = new Item("Item1");
 
         Debug.Log("Search Qty: " + dummySearchItem.Name + " -> Qty: " + items[dummySearchItem]);
 
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }

Other option is to create a wrapper object that keeps a reference to the item and the quantity of that item, and store that on a dictionary using the item ID or Name as the key.

 public class InventoryItem {
 
     Item item;
     uint qty;
 
 }

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 malkere · Jul 02, 2014 at 06:05 AM 0
Share

some new ter$$anonymous$$ology for me there, I'll have to spend some time understanding that when I get off work. Thanks for the input gstock!

avatar image malkere · Jul 03, 2014 at 03:38 AM 0
Share

here's what I ended up doing:

 public class InventoryItem{
 
     public Item _item{ get; set; }
     public int _amount{ get; set; }
 
 }

 in main:
     public void showInventory(int windowID){
         //window is 220 x 200
         for (int i = 0; i < Player._playerInventory.Count; i++) {
             GUI.Box (new Rect(5,10+(i*20),185,20), Player._playerInventory[i]._item.Name + ": " + Player._playerInventory[i]._amount);
         }
         if (GUI.Button (new Rect(195,5,20,20), "X")){ windowInventoryOpen = false; }
         //GUI.DragWindow(); //not working
     }

the wrapper suggestion is just what I needed. I then made a public List _playerInventory; so i can easily add and subtract items and their amounts. This way I can call on their names and variables easily as well as I can look up their location in the list.

To be honest your other approach may be better, but i don't yet fully understand it. I'll keep it in $$anonymous$$d as I continue to code. For now I'm able to access the data I wanted and am pressing forward! Thanks for unblocking my block!

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

22 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 avatar image avatar image avatar image

Related Questions

Assigning a GUIStyle with a String Variable? 0 Answers

C# Capture Keypress 3 Answers

How can I store game data outside of scripts.(e.g Total list of inventory items) 2 Answers

GUI Box Doesn't stay open 1 Answer

Key Item in C# 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