Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: bulka_tarta on Thu 21/12/2017 18:57:16

Title: GUIControl.GetAtScreenXY issue.
Post by: bulka_tarta on Thu 21/12/2017 18:57:16
Hi,

I have multiple buttons in my GUI, and all of them do fairly similar stuff. To avoid re-writing same code over and over in GlobalScript, I wanted to create a function which will be used by multiple buttons. The function detects which button you're clicking on, and based on that you can do various stuff.

The problem I'm having is that the game keeps crashing with "Null pointer referenced", and I have no idea how to fix this. I even simplified the code to just couple of lines to see if I made mistake elsewhere.

I tried running the following code in one function that is hooked to multiple buttons:
Code (ags) Select

  GUIControl* button = GUIControl.GetAtScreenXY(mouse.x, mouse.y);
  if ((button.AsButton.NormalGraphic != 0) && (button != null)) //The game keeps crashing on this line
  {
    Display("It works");
  }


The most frustrating thing is that this code works half of the time! Once you can click on bunch of buttons and everything works, and sometimes the game just crashes.

Any ideas how to make this work? I don't really want to re-write lots of code over and over, but it seems like the only solution at the moment.
Title: Re: GUIControl.GetAtScreenXY issue.
Post by: eri0o on Thu 21/12/2017 19:03:32
If your mouse is not over a GUIControl, button is null, so checking it crashes. You must FIRST check if button is null and if it's not you can check it's properties.
Title: Re: GUIControl.GetAtScreenXY issue.
Post by: Crimson Wizard on Thu 21/12/2017 19:04:54
Also, both GUIControl.GetAtScreenXY and button.AsButton will return "null" if there is no control under mouse, or if control is not button. You must test both of these conditions.

Code (ags) Select

GUIControl* button = GUIControl.GetAtScreenXY(mouse.x, mouse.y);
if (button != null && button.AsButton != null)
{
    // do your stuff
}
Title: Re: GUIControl.GetAtScreenXY issue.
Post by: bulka_tarta on Thu 21/12/2017 19:19:34
Thanks for speedy a response!

I knew it was something as simple as that. I would have never come up with the fact that you have to check for both things. I thought the check for "null" that I did would have been enough, but it clearly wasn't.

Thanks again!
Title: Re: GUIControl.GetAtScreenXY issue.
Post by: Khris on Thu 21/12/2017 20:36:55
Quote from: bulka_tarta on Thu 21/12/2017 19:19:34I would have never come up with the fact that you have to check for both things. I thought the check for "null" that I did would have been enough, but it clearly wasn't.
Even if you didn't have to check for button.Asbutton not being null, the order is important. Expressions are evaluated left to right, so you always need to do the most basic != null first.

There's a much better way to implement a function that is used by multiple buttons. Just add this to your Global Script:

function MyGUIButtons(GUIControl* control, Mousebutton button) {
  Button* b = control.AsButton;
  if (b == btnWhatever) {
    //...
  }
  if (b == btnSomeOther) {
    // ...
  }
}


Now simply paste MyGUIButtons (i.e. the exact name of the function) into each button's OnClick textfield in the events pane.

It's basically the same idea, but you can skip all those != null checks.