Pick up Items and Deep Copies
I tried to make a 2d plattformer with Pickup items. When the player collides with some Item, he is supposed to add this item into his inventory(array called items) and delete that item in the game. So i tried to make deep copies of that item into the inventory and failed to do so. The inspector shows the added items as "none" or missing" The other threads here didnt helped me too much. So this are the codes for that:
public bool AddItem(Pickup item)
{//returnt ob aufgehoben wird
for (int i = 0; i < items.Length; i++)
{
if (item.Itemname == items[i].Itemname)
{
if (items[i].Maxstack == items[i].Count)
{
items[i].Count = items[i].Count + item.Count;
return false;
}
else
{
items[i].Count = items[i].Count + item.Count;
return true;
}
}
}
Resize(items.Length + 1, ref items);
items[items.Length -1] = item.Clone() as Pickup;
return true;
}
private void Resize(int Size, ref Pickup[] itemss)
{
Pickup[] temp = new Pickup[Size];
for (int c = 1; c < Mathf.Min(Size, itemss.Length); c++)
{
temp[c] = itemss[c].Clone()as Pickup;
}
itemss = temp;
}
and the .clone();
public object Clone()
{
return MemberwiseClone();
}
If it helps i can show my entire unity project aswell (Teamviewer or upload). I will back in a couple of hours.
What fields does the Pickup have? As long it contains ref types $$anonymous$$emberwiseClone() won't do the job as it shallow copies them. Check that out that should clear things out, and if you have time read all parts. Cheers. (Note: if you can't figure it out post the Pickup class for further help)
Pickup only contains rather simple types like in and float. Does string count as ref type? $$anonymous$$y pickup class inherites from my item class.
public class Item:$$anonymous$$onoBehaviour {
string _name = "Demoname";
int _vkpreis = 1;
int _kaufpreis = 2;
int _maxstack = 1;//wieoft stackbar
int _count = 1;
public virtual void Setup(string itemname, int vkpreis, int kaufpreis, int maxstack, int count)
{
_name = itemname;
_vkpreis = vkpreis;
_kaufpreis = kaufpreis;
_maxstack = maxstack;
_count = count;
}
public object Clone()
{
return $$anonymous$$emberwiseClone();
}
public int Count
{
get { return _count; }
set {
if (value > _maxstack)
{
_count = _maxstack;
}
else if (value < 0)
{
_count = 0;
}
else
{
_count = value;
}
}
}
public virtual void Use() { }
public int $$anonymous$$axstack
{
get { return _maxstack; }
set { _maxstack = value; }
}
public int $$anonymous$$aufpreis
{
get { return _kaufpreis; }
set { _kaufpreis = value; }
}
public int Vkpreis
{
get { return _vkpreis; }
set { _vkpreis = value; }
}
public string Itemname
{
get { return _name; }
set { _name = value; }
}
}
Answer by grfdhfrgtj · Feb 09, 2017 at 10:08 PM
I solved the problem myself after some time. Hope it will help someone.. I threw the Clone-Funktion away and made myself a constructor. So i can use classA Acopy = new ClassA(A). Caution: The new Object triggers start and awake.
Thats my constructor:
public Pickup(Pickup previousPickup)
{
onplayer = previousPickup.Onplayer;
player = previousPickup.player;
Count = previousPickup.Count;
Maxstack = previousPickup.Maxstack;
Kaufpreis = previousPickup.Kaufpreis;
Vkpreis = previousPickup.Vkpreis;
Itemname = previousPickup.Itemname;
}
Your answer