- Home /
Inventory SlotID vs. RectPosition
I didn't know how to describe it better so the longer version: I have an inventory (slots-class displayed as buttons) and everything is fine with that except that the items I pick up are stored in the correct slot per code but displayed in the wrong one. I am having trouble finding the cause for that.
I have a List of slots that contain information about the position of the rect they will use in onGUI, a slotID (which is basically the index they have in the list) and an itemID. In a double-for-loop I create a grid of slots, the grid-coordinates are used to create the rect-positions (from bottom left to top right). The itemID is by default set to -1 which means that the slot is empty. If the mouse has been clicked, the script goes through the list of slots to find the one which contains the mouse-position. (I checked that and it works). If I pick up an item, the script searches the list of slots for the first empty slot (which is the slot 0, I checked that too and it works). Since the content of the slot-list has changed the gui is redrawn but the itemIcon is drawn in the top left (which is slot12) although the item is correctly saved in slot0.
I use one and the same list of slots for all those actions. I use the same way to go through the list (always a for-loop from 0 upwards) and I use the slotID to verify the found slot is the one I want. Everything I checked is correct but the icon is drawn on the wrongs spot nontheless.
Some relevant scriptparts:
The pick-up: int slotID = FindEmptySlot();
if(slotID != -1)
{
Slots[slotID].itemID = item;
}
else
{
Debug.Log("Inventory is full");
}
int FindEmptySlot() { int slotID = -1;
for(int i = 0; i < Slots.Count; i++)
{
if(Slots[i].itemID == -1)
{
slotID = i;
break;
}
}
return slotID;
}
The GUI: void InventoryWindow(int id) { Texture2D icon;
for(int i = 0; i < Slots.Count; i++)
{
if(Slots[i].itemID != -1)
{
icon = itemData.Items[Slots[i].itemID].itemIcon;
GUI.Button( Slots[i].slotRect, icon);
}
else
{
GUI.Button( Slots[i].slotRect, "");
}
}
}
The input: if(Input.GetMouseButtonDown(0)) {
for(int a = 0; a < Slots.Count; a++) {
if(Slots[a].slotRect.Contains(Input.mousePosition)) { Debug.Log(Slots[a].slotID + " : " + Slots[a].itemID); break; } }
Doesn't look to nice sorry but my internet is freaking out right now, hope you can read it though. If you need more code to see whats happening please tell me and I will post that too. Would be great if anyone of you could help since I need to fix this til friday.
Did you keep in $$anonymous$$d that a rect with position (0,0) is TOP LEFT? Adding to the X coordinate makes it go right, adding to the Y coordinate makes it go down. So if your inventory slot 0 is bottom left, its coordinate is (0,[screen-height]) .
Its not evident in the code, but I think this is your problem, judging solely from your description.
duh that might be the problem. thank you I will fix it now. Sometimes I don`t see the obvious things cause I am thinking too complex or sth..thanks
although if I click on a button it tells me it is slot0 when it should be slot0 and clicking on it is checked with the rect.contains function...shouldn't the icon be in the right rect aswell?
I will however change the rect-positions and hope that it works.
=> checked it and it works. I am a bit puzzled because of the thing above but maybe I will not $$anonymous$$d and just carry on. Thanks again.
Your answer
Follow this Question
Related Questions
Populate inventory with item icons 1 Answer
Inventory, why didn't work element? 1 Answer
How to make a simple inventory?!? Beginner coder JS 1 Answer
GUI focus control - inventory system issue 1 Answer
New GUI and Inventory problem. 1 Answer