Help selecting a gui control sequentially

Started by Custerly, Sat 23/12/2023 20:19:14

Previous topic - Next topic

Custerly

Hello,

I am trying to select and add text to one of 30 labels in a gui, sequentially. So if gui_label_[1-9] have text in them already, the next time the code runs, gui_label_10 is selected.

I'd love if I could do something like this:
Code: ags
 
for (int i = 1; i <= 30; i++)
{
  if (GUI_Label_[i].Text == "")
  {
    GUI_Label_[i].Text = "blah blah"; 
    int = 31; 
  }
}

but that obviously doesn't work.

I found the 'GUIControl.GetByName' function, but that is only in AGS 3.6.1, and I am running the latest stable version. I'm happy to upgrade to 3.6.1 if that'll sort this issue, but I'm not sure how stable 3.6.1 is.

Any help is much appreciated!

Crimson Wizard

#1
It should be something like this:

Code: ags
function AddTextToNextLabel(GUI *mygui, String text)
{
   for (int i = 0; i < mygui.ControlCount; i++)
   {
      Label *label = mygui.Controls[i].AsLabel;
      if (label == null)
         continue; // not a label, skip

      if (label.Text == "")
      {
         label.Text = text;
         break; // stop loop here
      }
   }
}

Then you call this function
Code: ags
AddTextToNextLabel(gMyGuiWithLabels, "bla bla");


Relevant articles in the manual:
https://adventuregamestudio.github.io/ags-manual/GUI.html#guicontrols
https://adventuregamestudio.github.io/ags-manual/GUIControl.html#guicontrolastype
About loop keywords "for", "break" and "continue":
https://adventuregamestudio.github.io/ags-manual/ScriptKeywords.html#for

EDIT: argh, reading the article about "GUI.Controls" makes me cringe, because it sais that it is only for "legacy code", which is wrong.
The article should be rewritten, and have a better example.

Custerly

Oh @Crimson Wizard you are a gem.

As usual, the code you responded with contained multiple different things I wasn't aware I could do. Looking at it, I was very confused at first, but I fiddled around a bit and have got it working.

Thanks once again for you time!

Crimson Wizard

Right, sorry, I could explain these bits further if necessary.
Most of these things are explained in the manual, but it's not organized well in some parts, so it takes time to find what you need.

Custerly

Oh, that was not a criticism. I was able to gather what I needed to understand your response by googling a bit, and I'd have bugged you again if I wan't able to figure it out. It's just that I've been doing some things which are more advanced than my actual skill/knowledge level, so I am constantly realizing that I'm doing things ass backwards. I prefer to learn this way though.

SMF spam blocked by CleanTalk