Game Development Community

How to use a function to create a new child control?

by Dean · in Torque Game Engine · 07/09/2007 (3:50 pm) · 4 replies

I'm just trying to make a function that adds a child control to another control when called. I know how to make it add a control, but not how to add it as a child. here's a basic example:

function CharacterPopup::NewButton()
{
new GuiButtonCtrl()
{
};
}

when I call NewButton, it CREATES the control, but I don't know how to add it as a child to another control, or even set the parent after creation, if possible. I need to be able to create the control for example when I push a button, so simply creating the controls at startup won't work for my purposes.

#1
07/09/2007 (3:55 pm)
You're very close.
function CharacterPopup::NewButton()
{
   %child = new GuiButtonCtrl()
   {
   };
   
   %parent.add(%child);
}

another option is to have the button be present but hidden at startup, and when you want to "create" it, just call setVisible(true).
#2
07/09/2007 (3:59 pm)
I can't use that other option cause I need a variable number of controls, depending on how many items are owned by the player. there could potentially be dozens on the screen, so manually creating them would just be a pain. They won't actually be buttons.... I was just using button as an example. Thanks :)

I'm also assuming I'll need a line like
%parent= CharacterPopup;
before i can use
%parent.add(%child);


*edit: yep. works like a charm. thanks :)
#3
07/09/2007 (4:15 pm)
Roger that.

if you're dealing with an arbitrary number of child controls,
you might be interested in the GuiArray2Ctrl resource.
#4
07/09/2007 (5:28 pm)
Ah, yeah i've seen that. I got it and pulled out the code I needed to put in my own custom control. It's a drag&droppable gui image with button and array capabilities :p The only thing I need now is learn how to add in double-click functionality.