Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: onizuka666 on Wed 05/11/2014 13:28:38

Title: Broken Sword inventory
Post by: onizuka666 on Wed 05/11/2014 13:28:38
Hi Guys,

I'm trying to make an inventory bar which should work like the one in Broken Sword.

I used the following commands:


function on_mouse_click (MouseButton button)
{
  if (IsGamePaused())
  {
    return;
  }
  //LEFT MOUSE BUTTON
  else if (button==eMouseLeft)
  {
    if (GetLocationType (mouse.x,  mouse.y) != eLocationNothing)
    {
      if (player.ActiveInventory ==null)
      {
        ProcessClick(mouse.x,  mouse.y,  eModeInteract);
      }
        else
        {
          ProcessClick (mouse.x,  mouse.y, eModeUseinv);
        }
      }
      else
      {
        if (player.ActiveInventory ==null)
        {
          ProcessClick(mouse.x,  mouse.y, eModeWalkto);
        }
        else
        {
         player.ActiveInventory=null;
        }
      }
    }
  //RIGHT MOUSE BUTTON
  else if (button == eMouseRight)
  {
    if (player.ActiveInventory != null)
    {
      player.ActiveInventory = null;
    }
    else if (GetLocationType (mouse.x, mouse.y)!= eLocationNothing)
    {
      ProcessClick(mouse.x,  mouse.y,  eModeLookat);
    }
  }
  //LEFT MOUSE BUTTON INVENTORY
  else if (button == eMouseLeftInv)
  {
    InventoryItem*item = InventoryItem.GetAtScreenXY (mouse.x, mouse.y);
    if (item != null)
    {
     
        if (player.ActiveInventory == null)
        {
          player.ActiveInventory = item;
        }
        else
        {
          if (item.ID != player.ActiveInventory.ID)
          {
            item.RunInteraction (eModeUseinv);
          }
        }
      }
   
  }
//RIGHT MOUSE BUTTON INVENTORY
else if (button==eMouseRightInv)
{
   if (player.ActiveInventory !=null)
   {
     player.ActiveInventory=null;
     return;
   }
  InventoryItem *item = InventoryItem.GetAtScreenXY(mouse.x, mouse.y);
  if (item != null)
  {
    item.RunInteraction(eModeLookat);
  }
}
}

The problem is i still cannot handle items in the inventory "drag'n'drop-style", and it doesn' matter if the if the built-in inventory click handle is activated or not because whenever i click on my inventory bar it just disappears again until I hover with the cursor to the top edge.

Alsos since I was using this script all the objects I placed in my rooms weren't responding to the commands I scripted. This used to work when I used the default function for mouse clicks.

I read a lot already about the problems with the selection of items in the inventory bar but no thread got me any further.

I hope anyone can help me with this or give me a detailed explanation how to make it work.

Thanks in advance
Title: Re: Broken Sword inventory
Post by: Vincent on Sat 08/11/2014 03:59:35
Quote from: onizuka666 on Wed 05/11/2014 13:28:38
I hope anyone can help me

Oh well, try to change lines as you need.
The trick is to try and try again.
But it should be something like this..

Code (ags) Select

function on_mouse_click(MouseButton button)
{
  int x = mouse.x;
  int y = mouse.y;
 
  if (button == eMouseLeftInv)
  {
    if (mouse.Mode == eModeLookat) //eModeWhateverYouWant
    {
      player.ActiveInventory = InventoryItem.GetAtScreenXY(x, y);
      mouse.Mode = eModeUseinv;
    }
    else if (mouse.Mode == eModeUseinv) //use inventory like to combine items
    {
      InventoryItem * item = InventoryItem.GetAtScreenXY(x, y);
      item.RunInteraction(eModeUseinv);
      mouse.Mode = eModeLookat;
    }
  }
   else if (button == eMouseRightInv) //run interaction look
   {
     InventoryItem * item = InventoryItem.GetAtScreenXY(x, y);
     item.RunInteraction(eModeLookat); // <---------------
     if(mouse.Mode == eModeUseinv) { mouse.Mode = eModeLookat; }
   }
   else if (button == eMouseLeft) //walk normally if Location is Not on inventory
   {
     if (GetLocationType(x, y) == eLocationNothing) { ProcessClick(x,y, eModeWalkto); }
     else // ELSE
     {
       if (mouse.Mode == eModeLookat) { ProcessClick(x, y, eModeInteract); } //interact with item
       else if (mouse.Mode == eModeUseinv)
       {
         ProcessClick(x, y, eModeUseinv); // look at item
         mouse.Mode = eModeLookat;
       }
     }
  }
    else if (button == eMouseRight)
    {
      if (mouse.Mode == eModeUseinv) { mouse.Mode = eModeLookat; } // gInventory.Visible = false;
      else if(GetLocationType(x, y) != eLocationNothing)
      {
        ProcessClick(x,y, eModeLookat);
        mouse.Mode = eModeLookat;
      }
    }
    // end if
  }
Title: Re: Broken Sword inventory
Post by: Adeel on Sat 08/11/2014 10:03:14
From the overview, Vicent's code will work for you. What I'm curios about is the fact that why this part of your code doesn't work:

Code (ags) Select
//LEFT MOUSE BUTTON INVENTORY
  else if (button == eMouseLeftInv)
  {
    InventoryItem*item = InventoryItem.GetAtScreenXY (mouse.x, mouse.y);
    if (item != null)
    {
     
        if (player.ActiveInventory == null)
        {
          player.ActiveInventory = item;
        }
        else
        {
          if (item.ID != player.ActiveInventory.ID)
          {
            item.RunInteraction (eModeUseinv);
          }
        }
      }
   
  }
//RIGHT MOUSE BUTTON INVENTORY
else if (button==eMouseRightInv)
{
   if (player.ActiveInventory !=null)
   {
     player.ActiveInventory=null;
     return;
   }
  InventoryItem *item = InventoryItem.GetAtScreenXY(mouse.x, mouse.y);
  if (item != null)
  {
    item.RunInteraction(eModeLookat);
  }
}


I've always scripted my inventory handling like that and never ran into any problem. In fact, just to be on the safe side, I just scripted it into an abandoned project of mine and it works like charm. I'm going to paste the relevant part of my script here:

Code (ags) Select
function on_mouse_click(MouseButton button) // called when a mouse button is clicked. button is either LEFT or RIGHT
{
  InventoryItem *item = InventoryItem.GetAtScreenXY(mouse.x, mouse.y);

  if (button == eMouseLeftInv){
    if (player.ActiveInventory == null)player.ActiveInventory = item;
    else if (item.ID != player.ActiveInventory.ID) item.RunInteraction(eModeUseinv);
  }

  else if (button == eMouseRightInv){
    if (player.ActiveInventory == null)item.RunInteraction(eModeLookat);
    else player.ActiveInventory = null;
  }
}



Thanking for getting me to code the inventory items' handling for my own project too. I was intending to do it a long time ago but I was just being lazy. :P