[SOLVED] Use Controls' array to make all buttons invisible

Started by Baguettator, Sun 14/06/2020 11:52:03

Previous topic - Next topic

Baguettator

Hi everybody,

So I decided to create a new topic, to make it clearer and more intelligent than posting again and again in the same topic, because it doen't mean anything with time... :)

My problem is that I want to create a "loop" to make all the buttons of a GUI invisible, in place of coding "button1.Visible=false, button2.Visible=false etc...". Because there is a lot of buttons, it could be terrible to have to write it by hands :)

I tried to use the Controls[] with a loop, but it doesn't work, because of a null pointer reference. I tried like that :

Code: ags

int nombreBoutons = 0;
Button* boutons[9];
Button *Buttonpoint;

for (int n = 0; n < 9; n++){
    GUIControl *control = gAffichageRessourcesColonie.Controls[n];
    if (control.AsButton != null) {
      boutons[nombreBoutons].Buttonpoint.Visible=false;
      nombreBoutons++;
    }
  }


I tried that with and without the Pointer, but it's not working. I'm having an error that I don't understand...

Someone could help me about that ?

Many thanks !

Laura Hunt

#1
I might be wrong (lots of people here are way more pro at AGS than I am), but it seems you're doing a lot of unnecessary stuff. What's the point of creating nombreBoutons, when you're already tracking this using n in the for loop? What is Buttonpoint for? And maybe I'm missing something, but boutons[nombreBoutons].Buttonpoint.Visible=false; doesn't make sense, since this translates to Button.Button.Visible.

Try doing just this:

Code: ags
for (int n = 0; n < 9; n++){
    GUIControl *control = gAffichageRessourcesColonie.Controls[n];
    if (control.AsButton != null) control.Visible=false;
  }


(This assumes that your GUI has only 9 controls, of course; otherwise, use for (int n = 0; n < gAffichageRessourcesColonie.ControlCount; n++) )

Baguettator

Ooooookay, many thanks it works ! Effectively, I was doing a LOT of inutile things, that was easier than I expected...

Thanks again !


SMF spam blocked by CleanTalk