Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: wynni2 on Tue 12/05/2015 16:42:28

Title: Opening inventory GUI causes graphical overlay to disappear
Post by: wynni2 on Tue 12/05/2015 16:42:28
Hopefully this is the right board...
In one of my rooms, I have a graphical overlay that I'm using as a piece of the foreground.  I've run into an issue where this overlay disappears whenever I open my Inventory GUI.  The other GUIs do not cause this issue, so I'm trying to pin it down. 

1) The user presses "Mouse Middle" or "I" to open the Inventory - the issue occurs regardless of which button is pressed
2) The GUI becomes visible using this code:

Code (ags) Select
function OpenInventory() {
    if (!IsGamePaused() && gMap.Visible == false) { //Only open the inventory if the game is not paused and the map is not open
    if (gInv.Visible == false) { //Open the Inventory if it's closed
    gInv.Visible = true;
    boxIDesc.Text = "";
    lblIName.Text = "";
         }
    else { //Close the inventory it's open
    gInv.Visible = false;
    boxIDesc.Text = "";
    }}}


3) While the GUI is open, this script runs under "repeatedly_execute" - to label each inventory item when you hover over it, and also control the visibility of the arrow buttons:

Code (ags) Select
if (gInv.Visible == true) {
  InventoryItem *iscreen = InventoryItem.GetAtScreenXY(mouse.x, mouse.y);
  if (iscreen != null && iscreen != iToken) { //The iToken cannot be activated by the mouse
  i = iscreen;
[b]lblIName.Text = String.Format(i.Name);[/b]
  if (i != i2) { //Clears the description if you hover on a new item
    boxIDesc.Text = "";
  }  }
  else {
    lblIName.Text = "";
    boxIDesc.Text = "";
  }
  if (boxInv.TopItem > 0) { //Activates the Scroll Up arrow if the list is scrolled down
    btnInvUp.Enabled = true;
    btnInvUp.Visible = true;
  }
  if (boxInv.ItemCount > 9) { //Activates the Scroll Down arrow if the list contains >9 items
      btnInvDn.Visible = true;
      btnInvDn.Enabled = true;
    }  }


EDIT: I went through the "repeatedly_execute" section, and it appears that any lines of code that set the "lblIName" or "boxIDesc" text will also remove the overlay.  Any thoughts on why (and how to fix it)?
Title: Re: Opening inventory GUI causes graphical overlay to disappear
Post by: Gilbert on Wed 13/05/2015 04:17:47
Could you post the codes on displaying the overlay?

Also, does the overlay return after closing the GUI, or it just vanishes forever?
Title: Re: Opening inventory GUI causes graphical overlay to disappear
Post by: Gurok on Wed 13/05/2015 04:30:39
It seems odd that you're calling String.Format with no additional parameters. Do you have an example of a value for i.Name?
Title: Re: Opening inventory GUI causes graphical overlay to disappear
Post by: wynni2 on Thu 14/05/2015 06:33:43
Re: script calling the overlay:

function room_Load()
{
Overlay.CreateGraphical(160, 110, 11, true);
}

I've tried it with transparency being true and false - it doesn't make a difference. 

Re: my use of String.Format - that's just an amateur coding issue.  i.Name includes things like "Wallet" and "Map." 

Right now I'm doing a workaround using a transparent GUI with the "overlay" on it, which gives no issues. 
Title: Re: Opening inventory GUI causes graphical overlay to disappear
Post by: Gilbert on Thu 14/05/2015 06:50:49
Actually I'm quite surprised that the Overlay even stays on screen at all. I thought once nothing is pointing to the Overlay it will be destroyed.
You need to store the returned pointer of Overlay.CreateGraphical() so that you may remove it later manually via Overlay.Remove().
Like:
Code (ags) Select

Overlay* tmp_foreground;//Add this

function room_Load()
{
  tmp_foreground = Overlay.CreateGraphical(160, 110, 11, true); //Store it
}


When you don't need it displayed anymore just do:
Code (ags) Select
tmp_foreground.Remove();
Title: Re: Opening inventory GUI causes graphical overlay to disappear
Post by: Slasher on Thu 14/05/2015 07:27:36
Yes (nod)

if it is within a function it will disappear once that function has ended which is why you need to have the 'Overlay* tmp_foreground;' outside of a function as Gilbert says.
Title: Re: Opening inventory GUI causes graphical overlay to disappear
Post by: Crimson Wizard on Thu 14/05/2015 10:11:06
Code (ags) Select

lblIName.Text = String.Format(i.Name);

Quote from: wynni2 on Thu 14/05/2015 06:33:43
Re: my use of String.Format - that's just an amateur coding issue.  i.Name includes things like "Wallet" and "Map."

This seem to be off-topic, but I must add this could be very dangerous use of Format.
The formatting function in AGS is generally unsafe, according to how its made. I would not go into much technical details, but if there will appear any "%" symbol (value placeholder) in the "i.Name", your game may crash badly. This is because it will expect a variable passed along the formatting string, but there's none.

The rule of thumb is to never use a string that is not supposed to be format, as the first parameter to String.Format, e.g. never pass user input as first parameter.
(This is a classic example of a safety hole that was found in many programs of the past:
http://en.wikipedia.org/wiki/Uncontrolled_format_string)


If you must, for some reason, or simply want to use String.Format, then do it safer way:
Code (ags) Select

String.Format("%s", i.Name); // notice function is explicitly told there is only 1 custom string
Title: Re: Opening inventory GUI causes graphical overlay to disappear
Post by: wynni2 on Thu 14/05/2015 17:39:09
Thanks everyone.  Understand more about overlays and string formatting now.