Game Development Community

Help instantiating a GuiControl from C++.

by Orion Elenzil · in Torque Game Engine · 09/11/2006 (4:37 pm) · 5 replies

Howdy -
related to this functional but as-yet-unapproved resource, i'd very much like to instantiate some GuiControls from within the engine versus script. I got it basically working except had this Very Weird behaviour where if the control had a built-in field such as "command", then when i set the field from script (eg %fooCtrl.command = "blah();";), then the field is changed when i examine it from script, but unchanged when i examine it from engine.

So, to recap my Q,
how do i do the following from engine ? (Besides eval(). Which does work fine, btw, i'd just prefer not to use it.)
// %this is a GuiControl..
%this.add(new GuiControl());

currently in the engine i'm just eval()ing the above,
but here is what i tried before that which had the strange variable issue.
i derived this code by trying to copy the OP_CREATE_OBJECT case in CodeBlock::exec() in CompiledEval.cc.

(safety checks and other irrelevancies snipped for clarity)
GuiControl* GuiArray2Ctrl::addChild(const char* className)
{
   ConsoleObject *object = ConsoleObject::create(className);

   GuiControl* gc = dynamic_cast<GuiControl*>(object);

   gc->registerObject();

   addObject(gc);

   return gc;
}

many thanks in advance,
orion

#1
09/11/2006 (6:56 pm)
I've done this before, but without looking at OP_CREATE_OBJECT. I don['t remember how I did it at that time (don't have the code either), but just vaguely looking at that code in ::exec, can it be that:

1) you are missing a call to gc->assignFieldsFrom(parent); if you have a parent?
2) you are missing calls to

gc->setModStaticFields(true);
gc->setModDynamicFields(true);

I feel that it's probably point 2. HOpe that helps u out.
#2
09/11/2006 (10:55 pm)
Mmm, good points.
I now remember skipping over those because they were dark n scary.
Thanks, i'll try these tomorrow!

ooo
#3
09/13/2006 (8:12 am)
GuiControl * gc = new GuiControl;
gc->registerObject ();
addObject ( gc ); // not sure if you should add it to the root group here?

Not sure if you already tried it out, might be worth a shot. Worked for me anyway.
#4
09/13/2006 (9:40 am)
Hi Stefan -
do member fields of the control like "command" work as expected from script ?
ie
%theControl.command = "echo(42);";
then
%theControl.dump();
and see that there's only one "command" field and that it's set as expected ?

my sitch is a little more complicated because i want to instantiate the object when given the ClassName -
ie, i don't know ahead of time if it's a GuiControl or a GuiMyFunkyWidgetCtrl,
so hence i'm using ConsoleObject::create().


i haven't gotten around to trying Lateral's suggestions.
#5
09/14/2006 (1:18 am)
Yeah, should work. Not sure why it didnt work with your solution, sounds like a missing line somewhere. Perhaps you need to call something on the parent.