vectors, pointers and wii<SOLVED>
Posted: Mon Jul 05, 2010 11:57 pm
Ok so i am making an item system and there are these functions
This is my problem:
The transfer function works once, but when i do it backwards (pick-up item vs drop item) it crashes my Wii. The ID of the object shouldnt change because its basically being copied when transfered to each itemsystem. Is my copy constructor bad? I dont know why it keeps crashing The item works fine before i add it to the player's inventory.
Im lost...
Code: Select all
//itemsys.h
void ItemSys::Transfer(ItemSys*tar,int item_index)
{
for(int i=0;i<Items.size();i++)
{
if(Items[i]->GetID()==item_index)
{
tar->AddItem(Items[i]); //Items is a std::vector<Item*>
DeleteItem(item_index);
}
}
}
void ItemSys::AddItem(Item*copy)
{
Items.push_back(new Item(copy));
}
int ItemSys::AddItem(std::string file, int x, int y)
{
Items.push_back(new Item(file,x,y,L,ID));
ID++;
return ID-1;
}
void ItemSys::DeleteItem(int index)
{
for(int i=0;i<Items.size();i++)
{
if(Items[i]->GetID()==index)
{
delete Items[i];
Items.erase(Items.begin()+i);
}
}
}
//item.h
Item::Item(Item*copy)
{
*this=*copy; //sort of copy constructor
}
Item::Item(std::string file,int x, int y,lua_State * l,int id)
{
//regular item loading function
L=l;
Pos.x=x;
Pos.y=y;
File.open(file.c_str());
//read from file bla bla bla
ID=id;
}
int Item::GetID()
{
return ID;
}
Code: Select all
//half psuedo-code/C++
item_id=Map.ItemSys.AddItem("orb.txt",100,100); //returns the item id
if a button pressed
{
Map.ItemSys.Transfer(&Player1.Inventory,item_id); //pick up item and it works
}
if b button pressed
{
Player1.Inventory.Transfer(&Map.ItemSys,item_id); //this crashes after i pick up an item with the above function
}
Im lost...