Sprites, buttons, guis, and this
by amaranthia · in Torque Game Builder · 11/07/2006 (10:04 pm) · 6 replies
I'm in an situation that I'm having trouble with....
I've set up my gui menus so that they are dynamically generated when the user clicks on a sprite in my scene. Now I have a gui that needs to open when the user presses a button. The problem is that my code expects me to provide a sprite's %this to build the gui, and I'm not sure how to get a %this because my button isn't a sprite...
I hope this makes sense...
I've set up my gui menus so that they are dynamically generated when the user clicks on a sprite in my scene. Now I have a gui that needs to open when the user presses a button. The problem is that my code expects me to provide a sprite's %this to build the gui, and I'm not sure how to get a %this because my button isn't a sprite...
I hope this makes sense...
#2
11/08/2006 (9:59 am)
Thank you! I'll try this out! :)
#3
Sorry about the confusion!
Here's some code to help out:
The button Gui in mainGui:
The function that is called by the button Gui:
11/08/2006 (8:31 pm)
Oh drats, this isn't what I was looking for. I've already got this part worked out. My problem is that I've got a button Gui in my mainGui, and I need it to call the same function that is called when the sprite click handler is called for a sprite. The problem is that %this has to be from a sprite, and I haven't figured out how to grab the ID of an existing sprite on screen and pass it in for the button Gui. Does this make sense? Sorry about the confusion!
Here's some code to help out:
The button Gui in mainGui:
new GuiBitmapButtonCtrl(itemButtonGui) {
canSaveDynamicFields = "0";
Profile = "GuiDefaultProfile";
HorizSizing = "right";
VertSizing = "bottom";
position = "255 675";
Extent = "80 80";
MinExtent = "8 2";
canSave = "1";
Visible = "1";
Command = "menu::inventory();";
hovertime = "1000";
groupNum = "-1";
buttonType = "PushButton";
bitmap = "";
};The function that is called by the button Gui:
function menu::inventory()
{
%this.initMenu();
%this.guiBackgroundExtent = "508 490";
%this.guiXPosition = "815 560";
%this.guiTitle = $strT[15];
%this.guiDialog = $strD[16];
%this.type = "i";
$typeNum = 3;
%this.itemBegin = 0;
%this.itemEnd = 17;
%this.buildInventory();
}
#4
11/08/2006 (9:50 pm)
Okay, I figured it out! I created a dummy sprite in my onLevelLoaded routine and set it to a global variable, and then referenced the global variable in my GUI. :)
#5
How does referencing a global "dummy" sprite help? When using that, your %this won't be of any useful value. Are you saying you just need to call the function but %this isn't needed for anything in the function? If that is the case, just call the function directly by namespace and pass a zero in for %this.
11/08/2006 (11:48 pm)
That doesn't make any sense, he wouldn't be able to see!How does referencing a global "dummy" sprite help? When using that, your %this won't be of any useful value. Are you saying you just need to call the function but %this isn't needed for anything in the function? If that is the case, just call the function directly by namespace and pass a zero in for %this.
#6
11/09/2006 (8:20 pm)
Hey, I said the same thing. It's really weird, but my dynamic menus are only generated when I pass the ID of a sprite in the level. When I figure out why this is happening, I'll let you know. My Guis aren't stored in Gui files... I'm building them from a template in one of my classes... this may have something to do with it.
Torque 3D Owner Richard McKinney
Default Studio Name
Let's say it looks something like this:
function SpriteClass::onClick(%this) { // a sprite was clicked, so create the gui element: new GuiButtonCtrl(SomeButtonFrom @ %this.getID()) { class = "SpriteGeneratedButton"; spriteID = %this; ... }; }Now your button knows who created it because it has a dynamic field "spriteID". It's also named after the sprite in case that's somehow helpful.
Now you can write:
function SpriteGeneratedButton::onClick (%this) { %this.spriteID.whatever(); // This will call methods on the sprite passing the sprite as %this %this.callSomeFunction (%this.spriteID); // This will call methods of SpriteGeneratedButton with the button as %this, and a second parameter of the sprite }Is that what you were looking for?