Game Development Community

using a variable to refrence a gui object name.

by Anthony Merlo · in Torque 3D Professional · 05/31/2011 (11:11 am) · 11 replies

I have a list of gui text buttons called btn1 - btn20.

In tourque script I want to create a loop that will reference each button depending on the iteration.

How would I do something like this?

for(%z=0;%z<20;%z++)
    {
       $buttonName = "btn"@%z;
       btnList.$buttonName.text = %z;
    }

Thanks


#1
06/01/2011 (2:15 am)
for(%z=0;%z<20;%z++)  
{  
   %buttonName = "btn" @ %z;
   %buttonName.text = %z; // works
   ("btn" @ %z).text = %z; // works too
}
#2
06/02/2011 (9:34 pm)
Thanks! That worked.

I have another problem. I'm trying to script the button positions when the GUI opens using something like this:

for (%z=0;%z<%i;%z++)
   {
      %button = "btn" @ (%z+1);
      %position = %z * 40;
      echo("Button " @ %button);
      
      %button.position = "2 " @ %position;
      echo("  Position = " @ %button.position);
      
      %button.Visible = "1";
      echo("  Visible = " @ %button.Visible);
      
   }

For some reason the echo's all look good but the after the script runs it places the buttons all over.

For instance the echo's (the ones executed by the script) will say something like:

Button btn1
Position = 2 0
Visible = 1

But when I type in the console "echo(btn1.position)" it will return something like 2 -756. Like some setting is moving the buttons around on me. I'm not fully versed in all the gui settings so I'm not sure which one would do that. If I type in the console the correct value (btn1.position="2 0") it moves the button to the correct spot.

Also, I'm putting the code in the onWake section, should it be someplace else?

Thanks!
#3
06/03/2011 (8:59 am)
Your Basic GuiControl needs to be defined with some things like:

new GuiMLTextCtrl(MessagePopText) {
         profile = "GuiMLTextProfile";
         horizSizing = "center";
         vertSizing = "bottom";
         position = "32 39";
         extent = "236 24";
         minExtent = "8 8";
         visible = "1";
         helpTag = "0";
         lineSpacing = "2";
         allowColorChars = "0";
         maxChars = "-1";

The main thing that may be causing you problems is:

horizSizing = "center";
vertSizing = "bottom";

Which can affect its placement as it derives it from the Parent
Gui Control..
#4
06/03/2011 (11:17 am)
Right now I have both set to relative. How can I keep it from affecting the placement? I didn't see a setting for none.

Here's the current definition:

new GuiBitmapButtonTextCtrl(btn1) {
            bitmap = "art/gui/ButtonPics/button.png";
            text = "Test1";
            groupNum = "-1";
            buttonType = "PushButton";
            useMouseEvents = "1";
            isContainer = "0";
            Profile = "GuiDefaultProfile";
            HorizSizing = "relative";
            VertSizing = "relative";
            position = "2 2";
            Extent = "507 30";
            MinExtent = "8 2";
            canSave = "1";
            Visible = "1";
            tooltipprofile = "GuiToolTipProfile";
            hovertime = "1000";
            canSaveDynamicFields = "0";
         };



#5
06/03/2011 (11:43 am)
Basically I'm not the best to answer this one..
I need to spend more time on the GUI's even after all these years :)

The way they work is it keeps the object aligned to one side
or the other or to the top or bottom when ever the Size of the Parent
Gui is changed. So if its set to Bottom and the Gui Container its in
has its size changed, it recomputes it's position to keep at the same
distance from the setting.. (center/bottom/etc..)

I dont work with them enough so I usually have to guess and try it
a few times until I remember and find the setting that works for that
instance of a Gui. I believe there is a good section on GUI's on TDN.

Goto the TGEA section on TDN and goto the GUI section, the majority
of that info is still the same. I'm always havin to pop into TDN when
I forget something myself .. *chuckle*
#6
06/03/2011 (1:18 pm)
Thanks. I'll check those out.

I have a feeling my problem might be where I'm putting the code, in the onWake section. It's almost like it creates the GUI, runs the onWake then moves the buttons. I think I need to somehow run the code after that but I don't know what the function call order is. Like some sort of post-process function that gets called at the end.

Or maybe there is a setting to disable resizing so it doesn't change my positions after running onWake?
#7
06/03/2011 (4:54 pm)
I think you have the Idea of the process confused..
You can setup a simple function of your own...

function MyButtonFunction()
{
    for (%z=0;%z<%i;%z++)  
    {  
      %button = "btn" @ (%z+1);  
      %position = %z * 40;  
      echo("Button " @ %button);  
            
      %button.position = "2 " @ %position;  
      echo("  Position = " @ %button.position);  
            
      %button.Visible = "1";  
      echo("  Visible = " @ %button.Visible);  
            
   }  
}

You can then call this function at AnyTime after the Gui has opened.
Like say during the onWake()..

schedule(500, 0, "MyButtonFunction");

which would run that function after the Gui onWake has finished.
Or Any other point in the application..
just at some point call MyButtonFunction(); somewhere in the scripts.

It all depends on your application process and when you want those
buttons displayed.
#8
06/03/2011 (7:00 pm)
Actually, that's the way I was doing it at first. I made a function called createButtons. It didn't work so I tried moving it directly into the onWake. I think the trick is the schedule command. If it works the way it sounds it should execute my function after a delay and hopefully after the engine messes with the button positions which is what I needed.

I just thought there may be another default function, like onWake, that the engine executes at the end of the gui process.

I'll give it it shot and hopefully all will work as it should.

Thanks!
#9
06/03/2011 (8:04 pm)
Still didn't work.

function ItemsDlg::onWake()
{
$total = 10;
schedule(500, 0, "createButtons");
}

function createButtons()
{
   echo("*---Creating Buttons---*");
   
   for (%z=0;%z<$total;%z++)
   {
      %button = "btn" @ (%z+1);
      %position = %z * 40;
      echo("Button " @ %button);
      
      %button.text = "test"@%z; // Set button text
      echo("  Text = " @ %button.text); // Check to see if it worked
       
      %button.position = "2 " @ %position; // Set button position
      echo("  Position = " @ %button.position); // Check to see if it worked
      
      %button.Visible = "1"; // Set button visibility
      echo("  Visible = " @ %button.Visible); // Check to see if it worked
      echo("----");
      
   }
}


Does the fact that it's a child member of a parent gui container make a difference? Do I have to reference it differently? like use itemsDlg.btn1.text or something?

The buttons are within a GuiScrollCtrl which is within a GuiControl.

if I check the console the echo's all reported the correct positions.

btn1's positions says "2 0" during the execution of my script.

If I manually type in "echo(btn1.position); in the console after I open the problem gui it reports "2 -828".

If I manually type in btn1.position = "2 0"; in the console the button appears where it should and everything is fine.

It's like it's ignoring the position being set within script and fills in some "off the wall" number. Even with HorizSizing and VertSizing why would it move the button to -828? That would be WAY off the screen.

I'll keep plugging away at it and see if I can come up with anything.

Thanks!
#10
06/05/2011 (7:17 pm)
Yeah, you need the "Handle" for the Gui object you want move around.

heres a couple of other examples... (Kinda better then my explanations)
function onServerMessage(%message)
{
   // See if there's a sound at the end of the message, and play it.
   if ((%wavStart = playMessageSound(%message)) != -1) {
      // Remove the sound marker from the end of the message.
      %message = getSubStr(%message, 0, %wavStart);
   }

   // Server messages go to the chat HUD too.
   if (getWordCount(%message)) {
      ChatHud.addLine(%message);
   }
}

here [ ChatHud ] is from:
//--- OBJECT WRITE BEGIN ---
new GuiControl(MainChatHud) {
   profile = "GuiModelessDialogProfile";
   horizSizing = "width";
   vertSizing = "height";
   position = "0 0";
   extent = "640 480";
   minExtent = "8 8";
   visible = "1";
   helpTag = "0";
      noCursor = "1";

   new GuiControl() {
      profile = "GuiModelessDialogProfile";
      horizSizing = "relative";
      vertSizing = "bottom";
      position = "0 0";
      extent = "400 300";
      minExtent = "8 8";
      visible = "1";
      helpTag = "0";

      new GuiBitmapBorderCtrl(OuterChatHud) {
         profile = "ChatHudBorderProfile";
         horizSizing = "width";
         vertSizing = "bottom";
         position = "0 0";
         extent = "272 88";
         minExtent = "8 8";
         visible = "1";
         helpTag = "0";
            useVariable = "0";
            tile = "0";

         new GuiBitmapCtrl() {
            profile = "GuiDefaultProfile";
            horizSizing = "width";
            vertSizing = "height";
            position = "8 8";
            extent = "256 72";
            minExtent = "8 8";
            visible = "1";
            helpTag = "0";
            bitmap = "core/art/gui/images/hudfill.png";
            wrap = "0";
         };

         new GuiButtonCtrl(chatPageDown) {
            profile = "GuiButtonProfile";
            horizSizing = "left";
            vertSizing = "top";
            position = "220 58";
            extent = "36 14";
            minExtent = "8 8";
            visible = "0";
            helpTag = "0";
            text = "Dwn";
            groupNum = "-1";
            buttonType = "PushButton";
         };
         new GuiScrollCtrl(ChatScrollHud) {
            profile = "ChatHudScrollProfile";
            horizSizing = "width";
            vertSizing = "height";
            position = "8 8";
            extent = "256 72";
            minExtent = "8 8";
            visible = "1";
            helpTag = "0";
            willFirstRespond = "1";
            hScrollBar = "alwaysOff";
            vScrollBar = "alwaysOff";
            lockHorizScroll = "true";
            lockVertScroll = "false";
            constantThumbHeight = "0";
            childMargin = "0 0";

            new GuiMessageVectorCtrl(ChatHud) {
               profile = "ChatHudMessageProfile";
               horizSizing = "width";
               vertSizing = "height";
               position = "1 1";
               extent = "252 16";
               minExtent = "8 8";
               visible = "1";
               helpTag = "0";
               lineSpacing = "0";
               lineContinuedIndex = "10";
               allowedMatches[0] = "http";
               allowedMatches[1] = "tgeserver";
               matchColor = "0 0 255 255";
               maxColorIndex = "5";
            };
         };
      };
   };
};

Each one of those names is the "Handle" for that Gui Object
and thus allows you to control it.

[ new GuiMessageVectorCtrl(ChatHud) ]

;)
#11
06/05/2011 (7:19 pm)
Watch out for Name and Class clashes, although I think they fixed
that.. Havent tried it in awhile .. So keep em unique