So i am currently working on an rpg game... of course lol. I am having some trouble with the inventory class. The inventory has a max size of 20. in the class when i click the item in my inventory it works most of the time sometimes when i click the last item i get an error and im not sure why i think its an access violation but its not giving me that error. here is my entire class.
#include "darkgdk.h"
#include <vector>
#include "constants.h"
#include <string.h>
#ifndef UI_H
#define UI_H
struct Item
{
char name[30];
int img;
int amnt;
int health;
char type[30];
int damage;
int price;
};
class UI
{
public:
std::vector<Item*> items;
std::vector<Item*>::iterator Itemit;
UI()
{
//active objects
drop = false;
FireScrollisActive = false;
WepTimer = 0;
inTimer=0;
inMenu=false;
}
~UI() {}
void AddItem(char name[30],int img, int amnt, int health, char type[30], int damage, int price)
{
bool same=false;
unsigned p;
for(unsigned i = 0; i< items.size(); i++)
{
if(items[i]->name == name && (items[i]->type!="Weapon" || items[i]->type!="Armor"))
{
same = true;
p = i;
}
}
if(same==false)
{
if(items.size()<MAX_ITEMS)
{
Item *ITEMS = new Item();
strcpy(ITEMS->name,name);
ITEMS->img = img;
ITEMS->amnt = amnt;
ITEMS->health = health;
strcpy(ITEMS->type,type);
ITEMS->damage = damage;
ITEMS->price = price;
items.push_back(ITEMS);
}
if(items.size()==MAX_ITEMS)
{
message.Message("inventory full");
dbPlaySound(ERRORS);
}
}else
{
items[p]->amnt = items[p]->amnt + amnt;
}
}
int IMGButton(int x, int y, int width, int height, int img, char text[30], int amt,char type[30], int damage, int price)
{
dbPasteImage(ITEMBAR,x,y,1);
dbPasteImage(img,x+10,y,1);
if(dbMouseX()>x && dbMouseY()>y && dbMouseX()<x+width && dbMouseY()<y+height)
{
dbPasteImage(ITEMBARS,x,y,1);
dbPasteImage(img,x+10,y,1);
if(dbMouseClick()==1)
{
return 1;
}
}
dbSetTextSize(32);
dbCenterText(x+256,y,text);
dbSetCursor(x+460,y);
dbPrint(amt*1.0);
dbInk(dbRGB(255,255,77),dbRGB(0,0,0));
dbSetCursor(x+40,y);
dbPrint(price*1.0);
dbInk(dbRGB(255,255,255),dbRGB(0,0,0));
if(type=="Weapon")
{
dbInk(dbRGB(255,0,0),dbRGB(0,0,0));
dbSetCursor(x+420,y);
dbPrint(damage*1.0);
dbInk(dbRGB(255,255,255),dbRGB(0,0,0));
}
if(type=="Armor")
{
dbInk(dbRGB(34,185,255),dbRGB(0,0,0));
dbSetCursor(x+420,y);
dbPrint(damage*1.0);
dbInk(dbRGB(255,255,255),dbRGB(0,0,0));
}
return 0;
}
void Update()
{
//health hud
if(PlayerHealth>MAX)
{
PlayerHealth = MAX;
}
if(FireScrollisActive==true)
{
dbPasteImage(FIREA,0,dbScreenHeight()-128,1);
if(dbMouseClick()==1 && inTimer==0)
{
dbPositionObject(MAGIC,dbCameraPositionX(),dbCameraPositionY()-5,dbCameraPositionZ());
dbRotateObject(MAGIC,dbCameraAngleX(),dbCameraAngleY(),dbCameraAngleZ());
inTimer=15;
FireScrollisActive = false;
}
}
dbMoveObject(MAGIC,15);
dbRollObjectLeft(MAGIC,2);
for(int x = 0; x < (PlayerHealth/10); x++)
{
dbPasteImage(HEALTHHUD,(x*64)+25,10,1);
}
for(int x = 0; x < (PlayerArmor/10); x++)
{
dbPasteImage(ARMOR,(x*32)+25,75,1);
}
if(inTimer>0) inTimer--;
if(inTimer == 0 && inMenu == false && dbKeyState(15)==1)
{
Pause = true;
inMenu = true;
inTimer = 20;
}
if(inTimer == 0 && inMenu == true && dbKeyState(15)==1)
{
Pause = false;
inMenu = false;
inTimer = 20;
}
if(inMenu==true)
{
if(inTimer>0) inTimer--;
dbPasteImage(DROP,0,dbScreenHeight()-128,1);
if(drop==true) dbPasteImage(DROPT,0,dbScreenHeight()-128,1);
if(dbMouseX() > 0 && dbMouseY() > dbScreenHeight()-128 && dbMouseX() < 128 && dbMouseY() < dbScreenHeight()+128)
{
dbPasteImage(DROPT,0,dbScreenHeight()-128,1);
if(dbMouseClick()==1 && drop == false && inTimer==0)
{
drop = true;
inTimer = 20;
}
if(dbMouseClick()==1 && drop == true && inTimer==0)
{
drop = false;
inTimer = 20;
}
}
dbSetTextSize(64);
dbCenterText(dbScreenWidth()/2,dbScreenHeight()/2-400,"Inventory");
dbSetTextSize(32);
dbPasteImage(MAININV,(dbScreenWidth()/2)-256,(dbScreenHeight()/2)-(336),1);
for( unsigned i = 0; i < items.size(); i++)
{
if(IMGButton((dbScreenWidth()/2) - 246,(dbScreenHeight()/2)-326+(i*32),492,32,items[i]->img,items[i]->name,items[i]->amnt,items[i]->type,items[i]->damage,items[i]->price)==1 && inTimer ==0)
{
if(drop==false)
{
if(strcmp(items[i]->name,"Health Potion")==0)
{
if(PlayerHealth < MAX)
{
PlayerHealth += items[i]->damage;
if(items[i]->amnt>1) items[i]->amnt--;
if(items[i]->amnt==1) items.erase((items.begin() + i));
dbPlaySound(POTION);
}
if(PlayerHealth == MAX)
{
message.Message("Health Full");
dbPlaySound(ERRORS);
}
}
if(strcmp(items[i]->type,"Armor")==0)
{
dbPlaySound(EQUIP);
PlayerArmor += items[i]->damage;
message.Message("Item Equiped");
items.erase(items.begin() + i);
}
if(strcmp(items[i]->name,"Fire Scroll")==0)
{
//message.Message("used fire scroll");
FireScrollisActive=true;
items.erase(items.begin() + i);
}
inTimer=10;
}
if(drop==true)
{
message.Message("Item(s) Dropped");
items.erase(items.begin() + i);
inTimer = 20;
}
}
}
}
}
bool isInMenu()
{
return inMenu;
}
protected:
bool inMenu;
int inTimer;
bool FireScrollisActive;
bool drop;
};
#endif
This is the error i get from the vector class
#endif /* _HAS_ITERATOR_DEBUGGING */
_SCL_SECURE_VALIDATE_RANGE(_Pos < size());
return (*(_Myfirst + _Pos));
}
Im not really sure what to do from here
here are some pics to whow what happens