Passing %this from gui objects
by Wash U Student · in Torque Game Engine · 03/25/2005 (4:12 pm) · 8 replies
I've been trying to write gui functions that will work for a family of buttons, rather than writing one function per button. The example here is a function that will let a button change its own text value. My assumption was that I'd just reference %this in the function called, and everything would be happy, but this doesn't seem to work.
Witness:
..... assorted gui stuff....
new GuiButtonCtrl(MajorAName) {
...
command = "MajorAName.renameButton();";
....
};
new GuiButtonCtrl(Lieutenant1AName) {
....
command = "renameButton();";
....
};
function renameButton(%this)
{
echo(%this.text);
%this.foo();
}
function MajorAName::renameButton(%this)
{
echo(%this.text);
%this.foo();
}
function MajorAName::foo(%this)
{
echo("I am the very model of a modern major general.");
}
So the first button calls an explicitly defined version of the function just for that one gui object, and its %this reference works. Meanwhile, the generic version of the function called by the second button doesn't work. I've tried a number of variations on this scheme without success. Anybody clue me in?
(I'm aware of the bug in this current version of the test that Lieutenant1A name won't be able to call a foo function that doesn't exist, but back when MajorA was attempting to call the same function from the generic renameButton, it didn't work, either. You're seeing test code variation in progress here.)
S.
Witness:
..... assorted gui stuff....
new GuiButtonCtrl(MajorAName) {
...
command = "MajorAName.renameButton();";
....
};
new GuiButtonCtrl(Lieutenant1AName) {
....
command = "renameButton();";
....
};
function renameButton(%this)
{
echo(%this.text);
%this.foo();
}
function MajorAName::renameButton(%this)
{
echo(%this.text);
%this.foo();
}
function MajorAName::foo(%this)
{
echo("I am the very model of a modern major general.");
}
So the first button calls an explicitly defined version of the function just for that one gui object, and its %this reference works. Meanwhile, the generic version of the function called by the second button doesn't work. I've tried a number of variations on this scheme without success. Anybody clue me in?
(I'm aware of the bug in this current version of the test that Lieutenant1A name won't be able to call a foo function that doesn't exist, but back when MajorA was attempting to call the same function from the generic renameButton, it didn't work, either. You're seeing test code variation in progress here.)
S.
#2
As a side note the version of your response that showed up in my eMail autonotify had the command syntax wrong. Clarify a point of curiosity for me: Was that because the autonotifier marked up the command with spurious escape characters, or did you initially write one thing, then correct it?
Thanks again,
S.
03/27/2005 (1:19 pm)
Thanks for the quick response. It's working now, and it's a beautiful thing. In fact, I'm going to go back and retool some of my older code to take advantage of this. That should cut out a fair deal of bloat.As a side note the version of your response that showed up in my eMail autonotify had the command syntax wrong. Clarify a point of curiosity for me: Was that because the autonotifier marked up the command with spurious escape characters, or did you initially write one thing, then correct it?
Thanks again,
S.
#3
I think the autoresponder doesn't like quotes, its like in the resources/code snipits, sometimes the quotes get made into funky things like #180 something or other.
03/28/2005 (8:42 am)
When I edited it, I corrected a couple of typos and (at this point) I don't remember just what I had corrected. But I think my correction was in the body of the message and not the command itself--cause the command was the main thing I was focusing on.I think the autoresponder doesn't like quotes, its like in the resources/code snipits, sometimes the quotes get made into funky things like #180 something or other.
#4
03/28/2005 (9:06 am)
FYI you can also use more generic namespace functions. EG:function GuiButtonCtrl::renameButton(%this)
{
}
#5
03/28/2005 (12:48 pm)
That I did not know... I'll have to play with this.
#6
command = "renameButton( $ThisControl );";
The engine auto assigns "$ThisControl" to the object that is making the current call. That's as generic as you can get, lol.
03/28/2005 (9:12 pm)
More FYI, you can access a control in the command declaration with "$ThisControl"...command = "renameButton( $ThisControl );";
The engine auto assigns "$ThisControl" to the object that is making the current call. That's as generic as you can get, lol.
#7
Can you point me to a working usage of $ThisControl? I have yet to find any reference to it in the engine or script directories
-Ron
03/28/2005 (9:22 pm)
@GonzoCan you point me to a working usage of $ThisControl? I have yet to find any reference to it in the engine or script directories
-Ron
#8
HEAD\torque\example\common\editor\AIEditorToolBar.gui(54): command = "if($ThisControl.getValue()) AIEFrameSetDlg.addTool(EditorToolInspectorGui); else AIEFrameSetDlg.removeTool(EditorToolInspectorGui);";
HEAD\torque\example\common\editor\AIEditorToolBar.gui(71): if($ThisControl.getValue())
HEAD\torque\example\common\editor\AIEditorToolBar.gui(76): $ThisControl.setValue(%isSet);
HEAD\torque\example\common\editor\AIEditorToolBar.gui(84): if($ThisControl.getValue())
HEAD\torque\example\common\editor\AIEditorToolBar.gui(89): $ThisControl.setValue(%isSet);
HEAD\torque\example\demo\client\ui\chatHud.gui(58): altCommand = "$ThisControl.eval();";
HEAD\torque\example\starter.fps\client\ui\chatHud.gui(58): altCommand = "$ThisControl.eval();";
HEAD\torque\example\starter.racing\client\ui\chatHud.gui(58): altCommand = "$ThisControl.eval();";
HEAD\torque\engine\gui\guiControl.cc(1074): Con::setVariable("$ThisControl", buf);
HEAD\torque\engine\gui\guiSliderCtrl.cc(130): Con::setVariable("$ThisControl", buf);
HEAD\torque\engine\gui\guiTextEditCtrl.cc(546): Con::setVariable( "$ThisControl", buf );
03/28/2005 (9:29 pm)
HEAD\torque\example\common\editor\AIEButtonBarDlg.gui(195): command = "$Camera::movementSpeed = $ThisControl.getValue();";HEAD\torque\example\common\editor\AIEditorToolBar.gui(54): command = "if($ThisControl.getValue()) AIEFrameSetDlg.addTool(EditorToolInspectorGui); else AIEFrameSetDlg.removeTool(EditorToolInspectorGui);";
HEAD\torque\example\common\editor\AIEditorToolBar.gui(71): if($ThisControl.getValue())
HEAD\torque\example\common\editor\AIEditorToolBar.gui(76): $ThisControl.setValue(%isSet);
HEAD\torque\example\common\editor\AIEditorToolBar.gui(84): if($ThisControl.getValue())
HEAD\torque\example\common\editor\AIEditorToolBar.gui(89): $ThisControl.setValue(%isSet);
HEAD\torque\example\demo\client\ui\chatHud.gui(58): altCommand = "$ThisControl.eval();";
HEAD\torque\example\starter.fps\client\ui\chatHud.gui(58): altCommand = "$ThisControl.eval();";
HEAD\torque\example\starter.racing\client\ui\chatHud.gui(58): altCommand = "$ThisControl.eval();";
HEAD\torque\engine\gui\guiControl.cc(1074): Con::setVariable("$ThisControl", buf);
HEAD\torque\engine\gui\guiSliderCtrl.cc(130): Con::setVariable("$ThisControl", buf);
HEAD\torque\engine\gui\guiTextEditCtrl.cc(546): Con::setVariable( "$ThisControl", buf );
Torque 3D Owner Steve Kilcollins
Imagination U
While the function MajorAName::renameButton(%this) is assocated with a specific class -- MajorAName:: -- so it will pass %this.
What you can do for you 2nd button to have a generic function work (I use this method myself) is
command = "renameButton("@Lieutenant1AName@"):";
This is builds the class (Lieutenant1AName) into the command string into the function call.
Now, the %this in the renameButton genereric function will act as you want it.