Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: turtletennis on Wed 01/12/2021 21:54:01

Title: Cursor changes back to default after using an item and won't return
Post by: turtletennis on Wed 01/12/2021 21:54:01
For a one-button game jam I created a small adventure game using AGS. I am trying to get it so that when you click on the player the use item mode is switched to and the cursor changes to the next item's icon. When there's only one item in my inventory it only works once, if I attempt to repeat it the cursor resets to the default crosshair, even after clicking on the player again.
I'm using the tumbleweed project template. My code works correctly if I have multiple items in my inventory, but only once if I just have one item.
steps:
Pick up spear
click on player
cursor displays as spear
click on a hotspot
cursor resets to crosshair
click on player
expected: cursor displays as spear
actual: cursor stays as crosshair.

Code (ags) Select

function cGreg_AnyClick()
{
  int minIndex=3;
  int maxIndex=6;
  int a=minIndex;
  bool nothingSelected = player.ActiveInventory==null;
  if(nothingSelected) player.Say("Currently nothing is selected");
 
  while(a<=maxIndex)
  {
    if(nothingSelected || player.ActiveInventory==inventory[a])
    {
      int b=a+1;
     
      if(b>maxIndex) b=minIndex;
     
      while(b!=a)
      {

        if(player.HasInventory(inventory[b]))
        {

          player.ActiveInventory=inventory[b];
          player.Say(inventory[b].Name);
          Verbs.SetAction(eGA_UseInv);
          mouse.ChangeModeGraphic(eModeUseinv, player.ActiveInventory.CursorGraphic);
          return;
        }
        b++;
        if(b>maxIndex) b=minIndex;
      }
      if(nothingSelected) break;
    }
    a++;
  }
  if(player.ActiveInventory==null)
  {
    player.Say("Nothing");
  }
  else
  {
    player.Say(player.ActiveInventory.Name);
    Verbs.SetAction(eGA_UseInv);
    mouse.ChangeModeGraphic(eModeUseinv, player.ActiveInventory.CursorGraphic);
   
  }
}
Title: Re: Cursor changes back to default after using an item and won't return
Post by: Khris on Thu 02/12/2021 11:15:44
Your code is always looking for the next item, it simply doesn't really handle the case of only having a single item.

Try something like this:
Code (ags) Select
InventoryItem* FindNextItem(int min, int max) {

  int currentItemIndex = -1;
  int itemsIndexes[] = new int[max - min + 1];
  int itemsCount = 0;
  for (int ii = min; ii <= max; ii++) {
    if (player.InventoryQuantity[ii] > 0) {
      itemsIndexes[itemsCount] = ii;
      if (inventory[ii] == player.ActiveInventory) currentItemIndex = itemsCount;
      itemsCount++;
    }
  }

  if (itemsCount == 0) return null;
  return inventory[itemsIndexes[(currentItemIndex + 1) % itemsCount]];
}


Now use  player.ActiveInventory = FindNextItem(3, 6);.
(Untested!)
Title: Re: Cursor changes back to default after using an item and won't return
Post by: turtletennis on Thu 02/12/2021 19:59:07
Great, thanks, it works. One more problem, I've noticed that when I set the cursor to be the inventory item's CursorGraphic it's using the hotspot offset of the default cursor (7,7) instead of the offset for the item. I saw there's a mouse.ChangeModeHotspot function, but the inventory item doesn't have a variable for the hotspot's location, is there a way to get this?
Title: Re: Cursor changes back to default after using an item and won't return
Post by: Matti on Thu 02/12/2021 20:04:31
You can set the item's cursor hotspot in the editor, either by clicking on the mouse cursor image or by setting HotspotX and HotspotY in the item's properties panel.
Title: Re: Cursor changes back to default after using an item and won't return
Post by: turtletennis on Thu 02/12/2021 20:14:38
I have done, but it doesn't help. I have a work-around where I add HotspotX and HotspotY as properties and duplicate the HotspotX and HotspotY entries in the inventory item and use those. That works fine.
I guess it's because I'm manually overriding the cursor image. Is there a way to automatically set the cursor to the activeInventory item if you're in UseInv mode?

My code is now

Code (ags) Select
  InventoryItem* nextItem = FindNextItem(3, 6);
  player.ActiveInventory = nextItem;
  if(nextItem!=null)
  {
    mouse.ChangeModeGraphic(eModeUseinv, nextItem.CursorGraphic);
    Verbs.SetAction(eGA_UseInv);
   
    mouse.ChangeModeHotspot(eModeUseinv, nextItem.GetProperty("HotspotX"), nextItem.GetProperty("HotspotY"));
  }

but it would be nice to not have to duplicate the values.
Title: Re: Cursor changes back to default after using an item and won't return
Post by: Khris on Fri 03/12/2021 10:39:35
You can try  mouse.Mode = eModeUseinv;  instead instead, but I don't know how well that's going to play with the Tumbleweed template.