//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
}
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...
Last edited by mv2112 on Tue Jul 06, 2010 9:55 pm, edited 1 time in total.
mv2112 wrote:Im not using iterators though, how would it become undefined?
You really should be instead of addressing by index. When you erase and item, the size is decreased, so once you reach the higher end of the loop, you'll be trying to access out of range data.
mv2112 wrote:Im not using iterators though, how would it become undefined?
You really should be instead of addressing by index. When you erase and item, the size is decreased, so once you reach the higher end of the loop, you'll be trying to access out of range data.
The index i'm searching for isnt the actual index of the item in the vector, the index is stored in the item itself and is retrieved with GetID(). So i just loop through the vector untill i find the item with the correct index:
class Item
{
public:
int GetID() {return ID;}
private:
int ID; //has nothing to do with position in the vector
};
//in delete function
for(int i=0;i<Items.size();i++)
{
if(Items[i]->GetID()==item_index)
{
//item_index has nothing to do with i
delete Items[i];
Items.erase(Items.begin()+i);
}
}
How would i be accessing an item that doesnt exist, plus this works when i transfer an item from the world to the player, but not when i transfer the item from the player back to the world.
I copy the item and then delete it when i transfer it from itemsys to itemsys. This calls the destructor so the pointer to the image is NULLed out and SDL causes the crash when the map tries to render the item whos image doesnt exist.