int variable from dictionary?
Hi, i have simple inventory dictionary, here is the script:
public class Items
{
public string name;
public int itemID;
public int quantity;
}
Dictionary<int, Items> inventory = new Dictionary<int, Items>();
Later I set up some items which working but I would like to set an int from an item quantity from other script:
public Inventory myInventory;
public int countInt;
void start(){
countInt = myInventory[0].quantity // I don't know how to get it :(
}
the type for myInventory
should be the same as the definition of your Dictionary
and you'll need to make sure that it's referring to the same thing...
also, unity won't call start()
... try Start()
;)
Of course it's Start, my mistake, correct in script anyway :) I want my countInt value from Inventory script an Item quantity, for example from quantity of first item in inventory which I know I have 3, so I want my countInt to be 3.
In an gameobject I have attached script where is countInt variable (second on post above). On my player I have got inventory script (first on post above). What I want is my countInt depends on first item quantity.
Do you ever call (pseudocode) player.inventory[0].quantity = 3; to assign that?
Classes are all referenced, changes made to an object in one script are reflected in another.
First, we'll make the Items class:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Items
{
public string name;
public int itemID;
public int quantity;
// Instantiate
public Items(string _name, int _id, int _quantity)
{
this.name = _name;
this.itemID = _id;
this.quantity = _quantity;
}
// Sanity Check
public void Describe()
{
Debug.LogFormat("Item: {0}, ID: {1}, Quantity: {2}", this.name, this.itemID, this.quantity);
}
}
$$anonymous$$eeping your dictionary format, we'll make a simple Player class and attach this to some cube in scene.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Player : $$anonymous$$onoBehaviour {
// Player's Inventory
public Dictionary<int, Items> inventory = new Dictionary<int, Items>();
// ID and dictionary index will be the same in this example
public const int POTION_ID = 0;
// Use this for initialization
void Start () {
// Create an item
Items potion = new Items("Potion", POTION_ID, 3);
// Print how many of an item we start with
inventory.Add(POTION_ID, potion);
}
// Update is called once per frame
void Update () {
// Print this quantity whenever you press a key
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
AddPotions(3);
}
}
// Add item to inventory
void AddPotions(int newCount)
{
// Check how many we have
Debug.Log ("Previous Potions:");
inventory[POTION_ID].Describe();
// Add the number of potions, then describe the potion instance
inventory[POTION_ID].quantity += newCount;
// Check the update
Debug.Log ("Current Potions:");
inventory[POTION_ID].Describe();
}
}
Giving us the desired result after pressing Spacebar:
Sorry @TreyH, I think I should at start post everythong I have :) So here it is:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Inventory : $$anonymous$$onoBehaviour {
public class Items
{
public string name;
public int itemID;
public int quantity;
}
Dictionary<int, Items> inventory = new Dictionary<int, Items>();
void Start () { // add first item
Items myItem = new Items();
wytrych.name = "first";
wytrych.itemID = 0;
wytrych.quantity = 3;
inventory.Add(k, myItem);
k++;
}
public bool IsItemExists(int itemID) // if exists return true
{
return inventory.Contains$$anonymous$$ey(itemID);
}
public void AddItem(int idFor$$anonymous$$ey)
{
if(inventory.Contains$$anonymous$$ey(idFor$$anonymous$$ey))
inventory[idFor$$anonymous$$ey].quantity ++;
else
{
Items newItem = new Items();
newItem.name = "item_" + idFor$$anonymous$$ey;
newItem.itemID = idFor$$anonymous$$ey;
newItem.quantity ++;
k++;
inventory.Add(idFor$$anonymous$$ey, newItem);
print("nie mam, dodaje nowe");
}
}
public void RemoveItem(int keyIds)
{
if(inventory[keyIds].quantity > 1)
inventory[keyIds].quantity --;
else
inventory.Remove(keyIds);
}
}
And another script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class otherScript : $$anonymous$$onoBehaviour {
public Inventory myInventory;
private int countInt;
public int myID;
void Start()
{
countInt = // here i don't know how to get first item quantity from inventory, which I know is 3
}
public void Check()
{
if(door_inven.IsItemExists(myID))
{
//do some stuff, working fine
}
}
public void RemoveItem()
{
myInventory.RemoveItem(myID);//works fine
}
public void AddItem()
{
myInventory.AddItem(myID);//works fine
}
}
Well,
countInt = myInventory[FIRST_ITE$$anonymous$$_ID].quantity;
is how you would do it, but again you need to assign that somewhere else. $$anonymous$$y example shows how to do this, have you tried to implement it?
I get error when I'm trying to do it with my version
error CS0021: Cannot apply indexing with [] to an expression of type `Inventory'
I check it with yours and it works, however i get warning
warning CS0108: `Items.name' hides inherited member `UnityEngine.Object.name'. Use the new keyword if hiding was intended
But I can now set countInt so it looks like it's better now :) Now I just need get deep into this and make some functions, thanks :) Edit: I cannot reply below so I'm writing here: Thanks @TreyH, everything is working now, I even wrote remove quantity/item and it's also working. Of course countInt = ... also working fine :)
Your answer
Follow this Question
Related Questions
List check argument of item 3 Answers
How do I let the player pick up an item with left click? 0 Answers
Add Item to inventory - Shop UI intro and setup 0 Answers
Inventory and item class design 0 Answers