11 Replies - 5992 Views - Last Post: 08 January 2013 - 09:04 AM Rate Topic: -----

#1 magnite7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-August 12

class List not adding variables properly

Posted 02 January 2013 - 07:07 PM

When I add an item to my inventory in my game, if it already exists it is supposed to add on to the quantity but instead, it adds once and then doesn't change it afterwards. Where am I going wrong?

This is my inventory manager, for testing purposes, when I send the item into AddItem the quantity is always set to 1.
// Item = The item......int = the qauntity
public List<InvItem> inventory = new List<InvItem>();

private const int MAX_ITEM_STACK = 30;
private const int MAX_INV_SLOTS = 15;
private const string UNABLE_TO_ADD_ERROR = "ERROR: Cannot add the item to the inventory. \nInventoryManager.cs(AddItem(Item item, int quantity)) Line 30";

public void AddItem(InvItem item) {
	// i is only here for testing, testing for qauntity adding
	// i is 0 so i will always get the first item cause i only have one item in the game so far
	int i = 0;
	if (item.item != null & item.quantity > 0) {
		if (ContainsItem (item)) {
			Debug.Log ("Item Q: " + item.quantity);
			inventory[i].Quantity += item.quantity;
			Debug.Log ("Inv  Q: " + inventory[i].Quantity);
		} else {
			if (inventory.Count < MAX_INV_SLOTS) {
				inventory.Add (item);
			}
		}
	} else {
		Debug.LogError (UNABLE_TO_ADD_ERROR);	
	}
}

private bool ContainsItem(InvItem item) {
	for (int i = 0; i < inventory.Count; i++) {
		if (inventory[i].item.id == item.item.id) {
			return true;
		}
	}
	return false;
}



This is custom Inventory Item class
public class InvItem {
	public ItemType type = ItemType.Null;
	public Item item = null;
	public int quantity = 0;

	public int Quantity {
		get { return quantity; }
		set { quantity = value; }
	}
		
	public enum ItemType {
		Weapon,
		Armor,
		Consumable,
		Item,
		Null
	}
	
	public ItemType SetType(string type) {
		switch (type) {
			case "Weapon":
				return ItemType.Weapon;
			default:
				return ItemType.Null;
		}
	}
}



The ideal quantity outcome when I add the item in (Item has a fixed quanityt of 1 for testing) is:
0 1 2 3 4 5 6 7 8 9... etc
But instead it goes like this:
0 1 2 2 2 2 2 2 2 2... etc

Where am I going wrong?

If more info is needed, I can give more.

Is This A Good Question/Topic? 0
  • +

Replies To: class List not adding variables properly

#2 BigR1983  Icon User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • Posts: 221
  • Joined: 12-April 10

Re: class List not adding variables properly

Posted 02 January 2013 - 07:55 PM

Change line 15 to be
inventory[i].Quantity++;

since you have found an item in your inventory, you want to increment that item by 1, and since in the definition of your item the default quantity is 0, you will increment by 0 each time.

Try that and see what happens

Also, is there a reason that you have 2 public quantity variables in your InvItem class? is the lower case one for internal use? If so, make it private, and use the public Quantity for accessing from outside.

This post has been edited by BigR1983: 02 January 2013 - 07:57 PM

Was This Post Helpful? 0
  • +
  • -

#3 magnite7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-August 12

Re: class List not adding variables properly

Posted 02 January 2013 - 10:00 PM

I tried that and it still goes to 2 and stops.
The first one should be private, thanks for pointing that out. 'Quantity' is used to get and set 'quantity'.

This post has been edited by tlhIn`toq: 03 January 2013 - 07:28 AM
Reason for edit:: Don't quote the entire previous message: We all see it.

Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: class List not adding variables properly

Posted 03 January 2013 - 02:08 AM

Where is the Item class and what exactly are you creating as items?

I also don't like your SetType method. If you have an enum, why aren't you using it?

This post has been edited by Momerath: 03 January 2013 - 02:09 AM

Was This Post Helpful? 1
  • +
  • -

#5 magnite7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-August 12

Re: class List not adding variables properly

Posted 03 January 2013 - 08:29 AM

Heres my item class
using UnityEngine;
using System.Collections;

[System.Serializable]
public class Item : ScriptableObject {

	public int id = 0;
	public string prefabName = "";
	public string itemName = "Unkown";
	public string descr = "No description.";
	public int cost = 0;
	public float mass = 0;
}




I can actually get rid of that method, its not being used, was a previous testing thing.
Was This Post Helpful? 0
  • +
  • -

#6 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: class List not adding variables properly

Posted 03 January 2013 - 09:23 AM

And the lines where you create items and add them?
Was This Post Helpful? 0
  • +
  • -

#7 magnite7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-August 12

Re: class List not adding variables properly

Posted 05 January 2013 - 08:58 AM

private InvItem newEntry = new InvItem();

void Update () {
	if (canInteract) {
		if (Input.GetButtonDown ("Interact")) {			
			newEntry.Quantity = 1; // Will be randomly generated
			Debug.Log ( newEntry.Quantity);
			invMan.AddItem(newEntry);
		}
	}
}

void onmouseenter() {
	canInteract = true;
}

void OnMouseExit() {
	canInteract = false;	
}

public void SetItem(string type, Item item) {
	newEntry.type = newEntry.SetType(type);
	newEntry.item = item;
}


Was This Post Helpful? 0
  • +
  • -

#8 magnite7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-August 12

Re: class List not adding variables properly

Posted 07 January 2013 - 11:29 PM

bump?
Not sure if I'm allowed to do this, but I couldn't find any rules against it.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,161
  • Joined: 05-May 12

Re: class List not adding variables properly

Posted 08 January 2013 - 06:22 AM

In your code, you have:
Debug.Log ("Item Q: " + item.quantity);
inventory[i].Quantity += item.quantity;
Debug.Log ("Inv  Q: " + inventory[i].Quantity);



Can you change that to:
Debug.Log ("Item Q: " + item.quantity);
Debug.Log ("Before: Inv  Q Hash: " + inventory[i].GetHashCode());
Debug.Log ("Before: Inv  Q: " + inventory[i].Quantity);
inventory[i].Quantity += item.quantity;
Debug.Log ("After: Inv  Q: " + inventory[i].Quantity);



And then report back to us what your log results are?

This post has been edited by Skydiver: 08 January 2013 - 06:32 AM

Was This Post Helpful? 0
  • +
  • -

#10 magnite7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-August 12

Re: class List not adding variables properly

Posted 08 January 2013 - 07:01 AM

Item added. Quantity: 1

---

Item Q: 1

Before: Inv  Q Hash: -8526

Before: Inv  Q: 1

After: Inv  Q: 2

---

Item Q: 1

Before: Inv  Q Hash: -8526

Before: Inv  Q: 1

After: Inv  Q: 2

---

Item Q: 1

Before: Inv  Q Hash: -8526

Before: Inv  Q: 1

After: Inv  Q: 2



This is the results.
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,161
  • Joined: 05-May 12

Re: class List not adding variables properly

Posted 08 January 2013 - 07:44 AM

Yikes! Since the hash code is not changing and InvItem is a class, that hints strongly that it is not a matter of the object in the list being replaced. I would set a breakpoint on setter of your Quantity property and see who is changing the 2 back to a 1.
Was This Post Helpful? 0
  • +
  • -

#12 magnite7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-August 12

Re: class List not adding variables properly

Posted 08 January 2013 - 09:04 AM

I fixed it, I guess when I made the item and created the item, it somehow put a lock or something on the quantity. I moved the new InvItem initialization to inside the interact block so that the new InvItem is only created when interacted with and this fixed the problem. Heres my updated code

	private bool canInteract;
	private InventoryManager invMan;
	private InvItem newEntry;
	private Item thisItem;
	private string type;
	
	// Use this for initialization
	public void Start () {
		canInteract = false;
		invMan = GameObject.FindGameObjectWithTag("GameManager").GetComponent<InventoryManager>();
	}
	
	// Update is called once per frame
	void Update () {
		if (canInteract) {
			if (Input.GetButtonDown ("Interact")) {	
				newEntry = (InvItem)ScriptableObject.CreateInstance<InvItem>();
				SetNewEntry (newEntry);
				invMan.AddItem(newEntry);
			}
		}
	}
	
	void onmouseenter() {
		canInteract = true;
	}
	
	void OnMouseExit() {
		canInteract = false;	
	}
	
	public void SetItem(string tp, Item item) {
		type = tp;
		thisItem = item;
	}
	
	private void SetNewEntry(InvItem entry) {
		entry.type = newEntry.SetType (type);
		entry.item = thisItem;
		entry.Quantity = 1; // Will be randomly generated
	}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1