AGS Awards nominations close at 13:59 GMT on Saturday 10 February 2018. You haven't yet nominated, so you've got 17 days and 2 hours left to play the games and decide which to nominate!

Author Topic: GUI.Y co-ordinates are out of range  (Read 469 times)

GUI.Y co-ordinates are out of range
« on: 10 Jan 2018, 17:28 »
Hi, I've been experimenting with trying to make text for hotspots appear near the mouse when players move it over the hotspots, the script I use is:

gGui1.SetPosition(mouse.x + 10, mouse.y + 10);

under repeatedly execute in the global script. It works and looks fine until I move the mouse too close to the edge of the window, because then the game crashes and I get a message saying that the GUI.Y co-ordinates are out of range, how can I prevent this?

Crimson Wizard

  • AGS Project Tracker Admins
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    •  
    • Lifetime Achievement Award Winner
    •  
Re: GUI.Y co-ordinates are out of range
« Reply #1 on: 10 Jan 2018, 17:53 »
I keep forgetting to fix this, AGS has some weird condition that if GUI is moved out of room background, then it stops the game.

Anyway, there are two methods, depending on how you want your gui to act.
If you need something to stay on screen at all times, like hotspot description, then use X, Y, Width and Height to ensure it does not go over viewport range.

Code: Adventure Game Studio
  1. int x = mouse.x + 10;
  2. int y = mouse.y + 10;
  3. if (x < 0)
  4.     x = 0;
  5. if (y < 0)
  6.    y = 0;
  7. if (x + gGui1.Width > System.ViewportWidth)
  8.     x = System.ViewportWidth - gGui1.Width;
  9. if (y + gGui1.Height > System.ViewportHeight)
  10.     y = System.ViewportHeight - gGui1.Height;
  11. gGui1.X = x;
  12. gGui1.Y = y;
  13.  

If you allow it to go out of screen, then simply check for coordinates to go over the viewport range and make it invisible and then visible again.
But I guess this is not your case.
« Last Edit: 10 Jan 2018, 20:33 by Crimson Wizard »

Re: GUI.Y co-ordinates are out of range
« Reply #2 on: 10 Jan 2018, 20:05 »
Thank you, but I'm afraid that I don't understand how I should use the code you provided in the game script.
I tried putting it under gGui1.SetPosition(mouse.x + 10, mouse.y + 10); in the Global Script, but that didn't solve the problem.

Re: GUI.Y co-ordinates are out of range
« Reply #3 on: 10 Jan 2018, 20:13 »
You don't need gGui1.SetPosition, it's replaced by
Code: Adventure Game Studio
  1. gGui1.X = x;
  2. gGui1.Y = y;

gGui1.SetPosition(x, y) should also work.

Re: GUI.Y co-ordinates are out of range
« Reply #4 on: 10 Jan 2018, 20:27 »
You don't need gGui1.SetPosition, it's replaced by
Code: Adventure Game Studio
  1. gGui1.X = x;
  2. gGui1.Y = y;

gGui1.SetPosition(x, y) should also work.
I tried disabling the line with gGui1.SetPosition, and it stopped the game from crashing when I moved the mouse towards the window border,
but then the hotspot description appeared at the top of the window instead of next to the mouse.

Crimson Wizard

  • AGS Project Tracker Admins
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    •  
    • Lifetime Achievement Award Winner
    •  
Re: GUI.Y co-ordinates are out of range
« Reply #5 on: 10 Jan 2018, 20:31 »
There is a typo in my script:

if (y > 0)
   y = 0;

Should be

if (y < 0)
   y = 0;
« Last Edit: 10 Jan 2018, 20:34 by Crimson Wizard »

Re: GUI.Y co-ordinates are out of range
« Reply #6 on: 10 Jan 2018, 21:10 »
There is a typo in my script:

if (y > 0)
   y = 0;

Should be

if (y < 0)
   y = 0;
OK, I changed the typo and everything seems to be working, but the text sometimes appear far away from the cursor if the hotspot is near the right border, so I wonder, can I make the GUI text align in the middle when the cursor is near the right border?
There is also some overlap on the text and sprite when the cursor shifts to an inventory item, any advice how I can fix this?

Crimson Wizard

  • AGS Project Tracker Admins
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    •  
    • Lifetime Achievement Award Winner
    •  
Re: GUI.Y co-ordinates are out of range
« Reply #7 on: 11 Jan 2018, 00:03 »
the text sometimes appear far away from the cursor if the hotspot is near the right border, so I wonder, can I make the GUI text align in the middle when the cursor is near the right border?

I think you might just resize the GUI & Label to match the text's width. E.g., assuming your label is the only thing on GUI and positioned at 0,0, try this
Code: Adventure Game Studio
  1. int width = GetTextWidth(lblHotspotName.Text, lblHotspotName.Font) + X; // replace X with small number (1,2,..) just for safety
  2. lblHotspotName.Width = width;
  3. gGui1.Width = width;
  4.  

There is also some overlap on the text and sprite when the cursor shifts to an inventory item, any advice how I can fix this?

Could you post screenshot, I am not sure I understand the situation correctly?
« Last Edit: 11 Jan 2018, 10:54 by Crimson Wizard »

Re: GUI.Y co-ordinates are out of range
« Reply #8 on: 11 Jan 2018, 10:33 »
the text sometimes appear far away from the cursor if the hotspot is near the right border, so I wonder, can I make the GUI text align in the middle when the cursor is near the right border?

I think you might just resize the GUI & Label to match the text's width. E.g., assuming your label is the only thing on GUI and positioned at 0,0, try this
Code: Adventure Game Studio
  1. int width = GetTextWidth(gGui1.Text) + X; // replace X with small number (1,2,..) just for safety
  2. lblHotspotName.Width = width;
  3. gGui1.Width = width;
  4.  

There is also some overlap on the text and sprite when the cursor shifts to an inventory item, any advice how I can fix this?

Could you post screenshot, I am not sure I understand the situation correctly?
I tried putting the script below the previous script, but I just got this message when I tried.
GlobalScript.asc(28): Error (line 28): Not enough parameters in call to function

As for the cursor overlapping the text, here is a screenshot of the problem:

Crimson Wizard

  • AGS Project Tracker Admins
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    •  
    • Lifetime Achievement Award Winner
    •  
Re: GUI.Y co-ordinates are out of range
« Reply #9 on: 11 Jan 2018, 10:54 »
I tried putting the script below the previous script, but I just got this message when I tried.
GlobalScript.asc(28): Error (line 28): Not enough parameters in call to function

Oh, duh, I forgot the second parameter to GetTextWidth. Also I said "gGui1.Text", while it should have been label's text:  GetTextWidth(lblHotspotName.Text, lblHotspotName.Font).

As for the cursor overlapping the text, here is a screenshot of the problem:


Well, before making any fixes to the code, what would be the suitable solution from the design perspective? Do you want the text to be at same position, just above the item? Or moved aside?

Re: GUI.Y co-ordinates are out of range
« Reply #10 on: 11 Jan 2018, 16:56 »
Well, before making any fixes to the code, what would be the suitable solution from the design perspective? Do you want the text to be at same position, just above the item? Or moved aside?
I'd like the text to move aside from the cursor, the main priority is to have the text be clearly readable.
Quote
Oh, duh, I forgot the second parameter to GetTextWidth. Also I said "gGui1.Text", while it should have been label's text:  GetTextWidth(lblHotspotName.Text, lblHotspotName.Font).
I tried changing the last line of gGui1.Width = width; to GetTextWidth(lblHotspotName.Text, lblHotspotName.Font) = width; but then I got this message:
GlobalScript.asc(28): Error (line 28): undefined symbol 'lblHotspotName'

Snarky

  • Global Moderator
  • Mittens Earl
  • Private Insultant
    • I can help with proof reading
    •  
    • I can help with translating
    •  
Re: GUI.Y co-ordinates are out of range
« Reply #11 on: 11 Jan 2018, 17:03 »
Obviously you replace "lblHotspotName" with the name of the label that you use to display the hotspot name.

Also, your assignment should be the other way around, the way CW did it.

Crimson Wizard

  • AGS Project Tracker Admins
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    •  
    • Lifetime Achievement Award Winner
    •  
Re: GUI.Y co-ordinates are out of range
« Reply #12 on: 11 Jan 2018, 18:21 »
I tried changing the last line of gGui1.Width = width; to ;

I think there is some misunderstanding here. You must not remove "gGui1.Width = width", that line should stay, because it sets GUI's width.
Line "width = GetTextWidth(lblHotspotName.Text, lblHotspotName.Font);" calculates width of text and stores that value in variable "width".

And yes, I used lblHotspotName as a placeholder name, because I did not know how you called your label.
« Last Edit: 11 Jan 2018, 18:59 by Crimson Wizard »

Re: GUI.Y co-ordinates are out of range
« Reply #13 on: 11 Jan 2018, 22:16 »
Alright, I changed the code:
Code: Adventure Game Studio
  1. int width = GetTextWidth(hotspottext.Text, hotspottext.Font) + 1; // replace X with small number (1,2,..) just for safety
  2. hotspottext.Width = width;
  3. gGui1.Width = width;
The game doesn't crash, but I see no visual difference from before either. What have I missed?

Khris

    • Lifetime Achievement Award Winner
    •  
    • I can help with play testing
    •  
    • I can help with scripting
    •  
    • I can help with translating
    •  
    • Khris worked on a game that was nominated for an AGS Award!
Re: GUI.Y co-ordinates are out of range
« Reply #14 on: 11 Jan 2018, 22:24 »
Just move up the GUI if the cursor mode is eModeUseinv:
Right after
Code: Adventure Game Studio
  1. int y = mouse.y + 10;
insert
Code: Adventure Game Studio
  1. if (mouse.Mode == eModeUseinv) y -= 20;

Re: GUI.Y co-ordinates are out of range
« Reply #15 on: 11 Jan 2018, 23:25 »
Just move up the GUI if the cursor mode is eModeUseinv:
Right after
Code: Adventure Game Studio
  1. int y = mouse.y + 10;
insert
Code: Adventure Game Studio
  1. if (mouse.Mode == eModeUseinv) y -= 20;
Thanks, that solved the overlapping cursor!

Crimson Wizard

  • AGS Project Tracker Admins
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    •  
    • Lifetime Achievement Award Winner
    •  
Re: GUI.Y co-ordinates are out of range
« Reply #16 on: 12 Jan 2018, 16:12 »
Alright, I changed the code:
Code: Adventure Game Studio
  1. int width = GetTextWidth(hotspottext.Text, hotspottext.Font) + 1; // replace X with small number (1,2,..) just for safety
  2. hotspottext.Width = width;
  3. gGui1.Width = width;
The game doesn't crash, but I see no visual difference from before either. What have I missed?


If I recall right, the problem you were having was that
everything seems to be working, but the text sometimes appear far away from the cursor if the hotspot is near the right border, so I wonder, can I make the GUI text align in the middle when the cursor is near the right border?


I decided to create a quick test game to be sure we have same thing before our eyes.
This is the script I am using (not necessarily matching yours 100%):

Code: Adventure Game Studio
  1. function repeatedly_execute() {
  2.   /*
  3.     /// This part is NOT IMPORTANT for this question
  4.   */
  5.   int x = mouse.x;
  6.   int y = mouse.y;
  7.   Hotspot *h = Hotspot.GetAtScreenXY(x, y);
  8.   Object *o = Object.GetAtScreenXY(x, y);
  9.   Character *c = Character.GetAtScreenXY(x, y);
  10.  
  11.   String text;
  12.   if (h != null && h != hotspot[0])
  13.     text = h.Name;
  14.   else if (o != null)
  15.     text = o.Name;
  16.   else if (c != null)
  17.     text = c.Name;
  18.    
  19.   if (String.IsNullOrEmpty(text)) {
  20.     gGui1.Visible = false;
  21.     return;
  22.   }
  23.    
  24.   gGui1.Visible = true;
  25.   hotspottext.Text = text;
  26.  
  27.   /*
  28.     /// This part is RELATED to this question
  29.   */
  30.   x = mouse.x + 10;
  31.   y = mouse.y + 10;
  32.   if (x < 0)
  33.       x = 0;
  34.   if (y < 0)
  35.       y = 0;
  36.   if (x + gGui1.Width > System.ViewportWidth)
  37.       x = System.ViewportWidth - gGui1.Width;
  38.   if (y + gGui1.Height > System.ViewportHeight)
  39.       y = System.ViewportHeight - gGui1.Height;
  40.   gGui1.X = x;
  41.   gGui1.Y = y;
  42.   int width = GetTextWidth(hotspottext.Text, hotspottext.Font) + 1; // replace X with small number (1,2,..) just for safety
  43.   hotspottext.Width = width;
  44.   gGui1.Width = width;
  45. }
  46.  

What this code does: it adjusts the label width, which makes hotspot gui smaller, and text appears as close to the hotspot as possible. Or so it seems.

Re: GUI.Y co-ordinates are out of range
« Reply #17 on: 12 Jan 2018, 17:25 »
I tried putting your code in, and it solved the problem with the text position, but now the problem with the overlapping cursor sprite is back,
where am I supposed to put the if (mouse.Mode == eModeUseinv) y -= 20; command?

Crimson Wizard

  • AGS Project Tracker Admins
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    •  
    • Lifetime Achievement Award Winner
    •  
Re: GUI.Y co-ordinates are out of range
« Reply #18 on: 12 Jan 2018, 17:40 »
where am I supposed to put the if (mouse.Mode == eModeUseinv) y -= 20; command?
Hmm, good question.
The problem here is that there may be several "overlapping" edge cases.

I think this may work for starters:
Code: Adventure Game Studio
  1.   x = mouse.x + 10;
  2.   y = mouse.y + 10;
  3.   if (mouse.Mode == eModeUseinv)
  4.       y -= 20;
  5.  

But then, if the hotspot is at the screen top, and you move the mouse too close to the screen border, it will be adjusted by the following code and overlap item again.

Re: GUI.Y co-ordinates are out of range
« Reply #19 on: 12 Jan 2018, 22:49 »
Thank you, it seems to work, the sprites are no longer overlapping. However, I'm starting to feel like fore every problem that is solved, a new one pops up,
for the text over the hotspot doesn't disappear immediately after clicking on it, if walking, talking, a room transition or an animation is triggered by
clicking on a hotspot, item or character, the description text stays on the screen until the script has finished running.
How do I make the description text disappear after clicking on something?