Finding the InventoryItem Index

Started by Beefy, Thu 08/01/2009 18:58:42

Previous topic - Next topic

Beefy

Hello,

I just started on my first AGS game (using AGS 3.1.1) a few days ago and already ran into trouble:
How can I find out, which index/position an InventoryItem has in the inventory?

I have a lot of combining and swapping in the inventory going on, so I really need a way to figure that out. Right now, thats my approach - and its obviously wrong:

Code: ags

function getIndex(InventoryItem* item) {
  int i=0;
  while(i<(InventoryWindow1.ItemCount)) {
    if(InventoryWindow1.ItemAtIndex[i]==item) return i+1;
    i++;
  }
  return 1;
}

// Some simplified test code: Taking an item (testItem) apart, resulting in two new items (testItem2 and testItem3)
// This happens, when the user is doing a certain action on "testItem"
int index = getIndex(testItem);
player.AddInventory(testItem2, index);
player.AddInventory(testItem3, index+1);
player.LoseInventory(testItem);


If you wonder, why exactly that does not work for me: I quite often have the case of several identical items in the inventory. My getIndex-Function will always return the first of those, but this is not necessarily the one, the user clicked on.

This probably is quite a dumb question, but any help would be greatly appreciated. :-)

Khris

The only issue I can see is that you're returning i+1 instead of i.

Beefy

That's intentional, for AddInventory adds items one before the given index.

But my point is: The function is working sometimes, but most of the time it doesn't because there are several identical items in the inventory. I need to find out a way, to determine the index of one of those.

Pumaman

The InventoryItem object is identical for all the identical items, therefore you cannot tell the difference this way.

The only way to do what you want is use the mouse.x and mouse.y along with the inventory window ItemHeight/ItemsPerRow/ItemWidth/TopItem properties to figure out what index the player clicked on.

Trent R

Couldn't you call UpdateInventory (which resorts it) and then find which is where?

~Trent
To give back to the AGS community, I can get you free, full versions of commercial software. Recently, Paint Shop Pro X, and eXPert PDF Pro 6. Please PM me for details.


Current Project: The Wanderer
On Hold: Hero of the Rune

Beefy

Quote from: Pumaman on Thu 08/01/2009 20:23:33
The InventoryItem object is identical for all the identical items, therefore you cannot tell the difference this way.

The only way to do what you want is use the mouse.x and mouse.y along with the inventory window ItemHeight/ItemsPerRow/ItemWidth/TopItem properties to figure out what index the player clicked on.

Thank you Pumaman - sometimes the right solution is so easy but you still don't see it. I'll use mouse.x and mouse.y. :-)
I haven't tried it yet, but I'd say my problem is solved.

And Trent: I don't want the items to be resorted - I'm currently working on a game, where the player has to do a lot with the inventory, so I don't want to confuse the player by resorting - especially because you can several identical items and there are other items which only differ in color or slightly in form.

Trent R

Ah yes, I re-read your original post and realized that wouldn't work....

~Trent
To give back to the AGS community, I can get you free, full versions of commercial software. Recently, Paint Shop Pro X, and eXPert PDF Pro 6. Please PM me for details.


Current Project: The Wanderer
On Hold: Hero of the Rune

Beefy

I just wanted to tell you, that I tried it out and it works. Thanks again. I don't know if anybody else is interested in that, but here is my solution:

toolBar is the name of my gui, InventoryWindow1 the name of my inventory window within this gui.

It certainly isn't the easiest way to do this, but it works. :-)
Feel free to use the code if you need it.

Code: ags

function getIndex() {
  int inventoryWindowPositionX = InventoryWindow1.X + toolBar.X;
  int inventoryWindowPositionY = InventoryWindow1.Y + toolBar.Y;
  if(mouse.x < inventoryWindowPositionX || mouse.y < inventoryWindowPositionY
   || mouse.x > (inventoryWindowPositionX+InventoryWindow1.Width) 
   || mouse.y > (inventoryWindowPositionY+InventoryWindow1.Height)) {
    player.Say("Error: Inventory index could not be determined. Please contact the game's author to resolve.");
    /*player.Say("Debugging mouse-x: %d", mouse.x);
    player.Say("Debugging mouse-y: %d", mouse.y);
    player.Say("Debugging InventoryWindow1-X: %d", inventoryWindowPositionX);
    player.Say("Debugging InventoryWindow1-Y: %d", inventoryWindowPositionY);*/
    return 0;
  }
  int x = mouse.x-inventoryWindowPositionX;
  int y = mouse.y-inventoryWindowPositionY;
  x = FloatToInt(IntToFloat(x)/IntToFloat(InventoryWindow1.ItemWidth));
  y = FloatToInt(IntToFloat(y)/IntToFloat(InventoryWindow1.ItemHeight));
  //player.Say("Debugging X: %d", x);
  //player.Say("Debugging Y: %d", y);
  return (y*InventoryWindow1.ItemsPerRow)+x;
}

[code]
function getIndex() {
  int inventoryWindowPositionX = InventoryWindow1.X + toolBar.X;
  int inventoryWindowPositionY = InventoryWindow1.Y + toolBar.Y;
  if(mouse.x < inventoryWindowPositionX || mouse.y < inventoryWindowPositionY
   || mouse.x > (inventoryWindowPositionX+InventoryWindow1.Width) 
   || mouse.y > (inventoryWindowPositionY+InventoryWindow1.Height)) {
    // The mouse cursor is not in the inventory area. That should never happen - but if it does, error handling goes here.
    return 0;
  }
  int x = mouse.x-inventoryWindowPositionX;
  int y = mouse.y-inventoryWindowPositionY;
  x = FloatToInt(IntToFloat(x)/IntToFloat(InventoryWindow1.ItemWidth));
  y = FloatToInt(IntToFloat(y)/IntToFloat(InventoryWindow1.ItemHeight));
  return (y*InventoryWindow1.ItemsPerRow)+x;
}
[/code]

SMF spam blocked by CleanTalk