GUIControl.GetAtScreenXY issue.

Started by bulka_tarta, Thu 21/12/2017 18:57:16

Previous topic - Next topic

bulka_tarta

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

  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.

eri0o

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.

Crimson Wizard

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

GUIControl* button = GUIControl.GetAtScreenXY(mouse.x, mouse.y);
if (button != null && button.AsButton != null)
{
    // do your stuff
}

bulka_tarta

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!

Khris

#4
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:

Code: ags
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.

SMF spam blocked by CleanTalk