[SOLVED] Help with label text when inventory item is active

Started by tor.brandt, Tue 25/10/2016 01:20:07

Previous topic - Next topic

tor.brandt

This is my current modified version of the repeatedly_execute function of the LW_BASS TwoClickHandler module:

Code: ags

function repeatedly_execute()
{
  // Inventory GUI: 
  // - make visible if mouse "touches" trigger zone
  // - make invisible if mouse leaves inventory GUI
  if (!gInventoryBar.Visible && mouse.y <= INVENTORY_POPUP_POSITION)
  {
    gInventoryBar.Visible = true;
  }
	
  if (gInventoryBar.Visible && mouse.y > gInventoryBar.Height)
  {
    gInventoryBar.Visible = false;
  }
	
  // Action Text
  // We always display the name of what is under the mouse, with one exception:
  // IF the player has an inventory item selected and hovers over the same inventory item, 
  // we display nothing to indicate that an item can not be used on itself
  if (player.ActiveInventory == null)
  {
    lblActionText.Text = Game.GetLocationName(mouse.x, mouse.y);
  }
  else
  {
    InventoryItem *i = InventoryItem.GetAtScreenXY(mouse.x, mouse.y);
    if (i != null && (i.ID == player.ActiveInventory.ID))
    {
      lblActionText.Text = "";
    }

///This is the part I need help with:
    else
    {
      if (GetLocationType(mouse.x, mouse.y) == eLocationNothing)
      {
        lblActionText.Text = String.Format("Use %s with %s", player.ActiveInventory.Name, Game.GetLocationName(mouse.x, mouse.y));
      }
      
      else
      {
        lblActionText.Text = String.Format("Use %s with %s", player.ActiveInventory.Name, Game.GetLocationName(mouse.x, mouse.y));
      }
    }
  }
}


As you see, I've made it so that if an inventory item is active (i.e. is the current cursor), the action label will display "Use [active inv. item] with [that which the cursor is currently over]".

I'd like to make it so that even if an inventory item is active, if the cursor is not over an object, a hotspot, or an inventory item, the action label displays nothing at all.
I tried using the GetLocationType function, and then script that if an inventory item is active, and the current location type is eLocationNothing, the action label displays nothing. This works fine, except that since inventory items is of the location type eLocationNothing, the action label displays nothing when an inventory item is active and the cursor is over another inventory item.

Is there a way around this?
(It seems strange to me that there is no separate location type called "eLocationInvItem" or something, but perhaps there is a good reason for including inv. items in the eLocationNothing type?)

To recap: I'd like my action label to display "Use [active inv. item] with [the name of that which the cursor is over]" when the cursor is over an object, hotspot, another inventory item, and possibly also characters (in my current game there are not yet any characters beside the player character, but it'd be nice to know how to include these in the function for future purposes).

PS. In the code I've marked the part I need help with, but I've included the whole repeatedly_execute function just in case...

Khris

GetLocationType and Game.GetLocationName only work with non-GUI stuff.
Try this:
Code: ags
  String action = Game.GetLocationName(mouse.x, mouse.y);
  InventoryItem *i = InventoryItem.GetAtScreenXY(mouse.x, mouse.y);
  if (i != null) action = i.Name;
  
  if (player.ActiveInventory != null) {
    if (i == player.ActiveInventory) action = "";
    if (action.Length > 0) action = String.Format("Use %s with %s", player.ActiveInventory.Name, action);
  }
 
  lblActionText.Text = action;


I didn't test it, and it's late, you might have to adjust the logic.

tor.brandt

Thanks for your code, Khris!
I modified it in the following way, and even though it's probably not the most elegant piece of scripting, it does the job:

Code: ags

function repeatedly_execute()
{
  // Inventory GUI: 
  // - make visible if mouse "touches" trigger zone
  // - make invisible if mouse leaves inventory GUI
  if (!gInventoryBar.Visible && mouse.y <= INVENTORY_POPUP_POSITION)
  {
    gInventoryBar.Visible = true;
  }
	
  if (gInventoryBar.Visible && mouse.y > gInventoryBar.Height)
  {
    gInventoryBar.Visible = false;
  }
	
// Action Label Text	// We always display the name of what is under the mouse, except when an inventory item is active:
// If the player has an inventory item selected and hovers over the same inventory item, 
// we display nothing to indicate that an item can not be used on itself.
 // If the player has an inventory item selected and hovers over a hotspot, an object, a character, or another inventory item,
// we display: "Use [name of active inventory item] with [name of that which the mouse is over]".

  if (player.ActiveInventory == null)
  {
    lblActionText.Text = Game.GetLocationName(mouse.x, mouse.y);
  }
  
  else
  {
    if (GetLocationType(mouse.x, mouse.y) == eLocationHotspot)
    {
      lblActionText.Text = String.Format("Use %s with %s", player.ActiveInventory.Name, Game.GetLocationName(mouse.x, mouse.y));
    }
    
    else if (GetLocationType(mouse.x, mouse.y) == eLocationObject)
    {
      lblActionText.Text = String.Format("Use %s with %s", player.ActiveInventory.Name, Game.GetLocationName(mouse.x, mouse.y));
    }
    
    else if (GetLocationType(mouse.x, mouse.y) == eLocationCharacter)
    {
      lblActionText.Text = String.Format("Use %s with %s", player.ActiveInventory.Name, Game.GetLocationName(mouse.x, mouse.y));
    }
    
    else if (GetLocationType(mouse.x, mouse.y) == eLocationNothing)
    {
      String action = Game.GetLocationName(mouse.x, mouse.y);
      InventoryItem *i = InventoryItem.GetAtScreenXY(mouse.x, mouse.y);
      if (i != null) action = i.Name;
      
      if (player.ActiveInventory != null)
      {
        if (i == null || i == player.ActiveInventory) action = "";
        if (action.Length > 0)
        {
          action = String.Format("Use %s with %s", player.ActiveInventory.Name, action);
          lblActionText.Text = action;
        }
        
        else 
        {
          lblActionText.Text = "";
        }
      }
    }
  }
}


Now it's working perfectly! :smiley:

Khris

Tested my code with the LW BASS template and fixed two minor issues. It should now do exactly what yours does.


SMF spam blocked by CleanTalk