Game Development Community

On the fly GUI

by Jake Callery · in Torque Game Engine · 11/24/2007 (4:13 pm) · 7 replies

Hey guys, this can't be as hard as I'm making it out to be.

All I want to do is create a new UI element on the fly during game play.

So basically I want to take the GameTSCtrl(PlayGui) (which runs when the game is running) and add to it
a couple of GuiTextCtrl's to it at different times during game play.

So the question is, how can I add UI elements inside the GameTSCtrl n the fly?

Thanks so much for the help!

Jake

#1
11/24/2007 (4:21 pm)
I'm not to sure, I've never wanted to do it on the fly before. But if you want an icon to appear when your in the Zone, your bleeding etc.

Then I would create my GUI, and set those objects as setVisible(false); and then when they walk in to the Zone then they get told to set there Object to setVisible(true);

You can also move the GUI objects through code also, and I'm sure you can add them dynamically but not too sure how you would do that. You could get problems with the way they look doing that kind of thing, at different resolutions etc.

EDIT: you can also for images, change the image used this way. It's the way i go about this sort of thing. I'm not completely sure as to what you want in the final product though. :-)
#2
11/24/2007 (4:48 pm)
Thanks for the tip Edward, that would be a good backup plan for me. I would just need to make enough premade ones for all cases.

Still, there has to be a way to do it...

Oh wait.. I remember something about getCanvas.... that might work, I will need to look that up next.

Thanks again!
#3
11/24/2007 (4:51 pm)
Ahh.. boo.. it was Canvas.getcontent() and that doesn't seem to be it... that can be used to call functions in the GUI... but I can't seem to get the syntax to get it to make new GUI elements.

gotta keep looking :)

Jake
#4
11/25/2007 (6:40 am)
Personally I would do what Edward suggested. I think it's good practice to pre-make all of your gui elements instead of trying to create them on the fly. Just set their visibility to 0 at the start of the game. Through script when an event gets triggered just code myguielement.visibility =1 and set it to whatever info you want. It will be much easier then creating gui elements on the fly.
#5
11/25/2007 (10:35 am)
The only issue with that is the use of resources that may not need to be used. There must be a way to do this. Constructor can't possibly have all of those UI elements loaded all of the time.

Thanks for the ideas!

Jake
#6
11/26/2007 (1:06 am)
Jake, I wouldn't get hung up about the resources though, GUI elements really don't take up much processing grunt in the grand scheme of things and you'll find making elements and hiding them a whole lot easier to manage change with than altering code - remember once you dynamically create elements you won't get benefit of the GUI Editor to just drag and drop them, alter settings, etc.

That said there are always reasons for adding elements on the fly, for example populating a list of characters where you could have an unspecified number of saves, so here's how to do it:

// First create the object
%new_obj = new GuiTextCtrl(myTextControl) {
            canSaveDynamicFields = "0";
            Profile = "GuiTextProfile";
            HorizSizing = "center";
            VertSizing = "bottom";
            Position = "70 8";
            Extent = "18 18";
            MinExtent = "8 8";
            canSave = "1";
            Visible = "1";
            hovertime = "1000";
            text = "250";
            maxLength = "255";
         };

// Then add a parent for it, in the case you mentioned you wanted it directly to the
// main GameTSControl

PlayGui.add (%new_obj);

I think you'll also want to consider not adding them directly to the playgui but to some other control as a parent i.e. a GuiControl, that way you can easily remove them all with a call to the parent UI elements .clear() function.
#7
11/26/2007 (6:06 am)
Aaahh excellent! Thanks very much.

I wasn't too worried about resources, but I couldn't come up with a better example.

What I am trying to do is add icons for players as they join the game. So I won't know what icons/etc to add in advance, and I wouldn't want to have them all loaded in all combinations if only a few people are playing.

Thanks very much for the info! Really appreciate it.

Jake