Simple inventory for keyboard control

Started by , Thu 19/05/2011 19:48:51

Previous topic - Next topic

m0ds

Hi there, I've got myself stumped with a problem for something I'm making.

Everything in my game is keyboard controlled rather than mouse, and I'd like there to be a way of using the inventory with your keyboard instead of a mouse. I'm trying to find a fairly simple way of being able to cycle whats in your inventory, some way of getting AGS to recognise how many items are currently in it and go through each one, and then, on selecting one, have it "active".

This may be a bit more advanced so feel free to move it if necessary, but I'm wondering if there is a fairly simple way to do it but I just can't quite figure out how. I'm using 2.72, and if it's going to be impossible in that edition or way too complex, I might just take inventory puzzles out altogether. But if anyone has any suggestions how to do it, or ideas for the simplest mechanics to make this work, please help!

I'm thinking: Press tab to bring up a little box that shows 3 of your total inventory items (lets say there are 6). Use the left and right arrow key to cycle through them, the image appearing larger when it's highlighted, for example. Press enter to select that inventory item and make it the player's "active inventory".

I guess the main problem I'm having, beside the cycling script, is simply how to be able to hit enter on an item and make it the active inventory item. Normally that's processed in a mouse click. The other major problem is how AGS recognises what inventory items have been found, and can still cycle them no matter what the order of the inventory items in the inventory itself.

Many thanks :)

barefoot

Hi

for a start you should get the latest AGS version (like 3.2) and start from there..

You've been involved in a lot of games...

barefoot
I May Not Be Perfect but I Have A Big Heart ..

Khris

I've done something similar in a no mouse test game of mine.
My inventory GUI has a button at the center that doesn't do anything and has a simple frame assigned as image.
The inventory window, which is as high as one item, moves left and right behind the button.
Pressing tab shows/hides the inventory, and when it is visible, left and right move the inventory window by ItemWidth pixels (provided it can move further in that direction i.e. if the first/last item is in the center inside the frame, you can't move it further out).
Immediately after the movement, the new active item is calculated:

Code: ags
  current_item = ((bInvSelection.X+bInvSelection.Width/2)-invRow.X)/invRow.ItemWidth;
  player.ActiveInventory = invRow.ItemAtIndex[current_item];

(bInvSelection is the button, invRow is the inventory window.)

Thus there's no need to confirm the selection.

m0ds

Thanks Khris, that makes some sense. It gives me something to try out, and I kind of see how it's working.

Where did you put the code for pressing left and right and it making the inventory window move? I can almost see what you're stating but I still can't get my head around how to make that work. Anyway, I will tinker - thanks!

barefoot - I use a lot of older software. I like it :)

Khris

If the inventory GUI is supposed to pause the game you need to handle the keypresses at the top of on_key_press. (Otherwise the IsGamePaused() line quits the function prematurely.)

Here are the relevant pieces of code:

Code: ags
void HandleInventory(eKeyCode k) {

  int items = invRow.ItemCount;
  invRow.Width = items*invRow.ItemWidth;
  int current_item = ((bInvSelection.X+bInvSelection.Width/2)-invRow.X)/invRow.ItemWidth;
  if (k == keys.DUp || k == keys.DDown) return;
  if (k == keys.ACancel) {
    gInventory.Slide(5.0);
    return;
  }
  if (k == keys.AConfirm) {
    if (items == 0) return;
    player.ActiveInventory = invRow.ItemAtIndex[current_item];
    gInventory.Slide(5.0);
    return;
  }
  if (items < 2) return;
  if (k == keys.DLeft && current_item > 0) invRow.X += invRow.ItemWidth;
  if (k == keys.DRight && current_item < items-1) invRow.X -= invRow.ItemWidth;
  current_item = ((bInvSelection.X+bInvSelection.Width/2)-invRow.X)/invRow.ItemWidth;
  player.ActiveInventory = invRow.ItemAtIndex[current_item];
}

void on_key_press(eKeyCode k) {
  
  // inventory
  if (k == keys.Inventory) {
    if (invRow.ItemCount > 0 && player.ActiveInventory == null) {
      int m = mouse.Mode;
      player.ActiveInventory = invRow.ItemAtIndex[0];
      mouse.Mode = m;
    }
    gInventory.Slide(5.0);
  }
  if (gInventory.Transparency == 0) {
    HandleInventory(k);
    return;
  }
}


I'm using the keys.XXX struct to store all the ascii codes so I can easily remap the keys.
GUI.Slide() is used to slide the GUI into/out of the screen. It should work if you replace those with "gInventory.Visible = !gInventory.Visible;"

m0ds

Food for thought, thank you very much sir! I'm going to give it a try over the weekend, thanks for helping me with that one, I'll let you know how it goes :)

SMF spam blocked by CleanTalk