Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Custerly on Sat 23/12/2023 20:19:14

Title: Help selecting a gui control sequentially
Post by: Custerly on Sat 23/12/2023 20:19:14
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:

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!
Title: Re: Help selecting a gui control sequentially
Post by: Crimson Wizard on Sat 23/12/2023 20:29:03
It should be something like this:

Code (ags) Select
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) Select
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.
Title: Re: Help selecting a gui control sequentially
Post by: Custerly on Sat 23/12/2023 20:54:35
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!
Title: Re: Help selecting a gui control sequentially
Post by: Crimson Wizard on Sat 23/12/2023 21:02:40
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.
Title: Re: Help selecting a gui control sequentially
Post by: Custerly on Sat 23/12/2023 21:10:37
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.