Game Development Community

Select Weapon GUI

by Xmadole · in Torque Game Engine · 07/31/2007 (3:00 am) · 14 replies

Is there an example GUI script that when you press a button like ctrl w a set of buttons will come up and when you click them the player will switch to that weapon? (without holding the weapon in the built-in inventionary)

Long sentance, whew. :P

#1
07/31/2007 (10:34 am)
I don't know of a resource off the top my head but it's not hard to do. Just make the gui and for the command on the guibitmap button make it call a function on the client/server that changes the weapon. I could make this, and probably could use it myself, if no one else has some example code for you.

ps: remind me later I may forget..
#2
07/31/2007 (6:15 pm)
Ok sorry it took me so long, I had to get in the mood to look at my computer after work... =)

Here's what I came up with and tested multiplayer on lan so it should work.

First make a gui. I called it weaponSelect.gui. Replace 'buildTool' with your weapons name.

//--- OBJECT WRITE BEGIN ---
new GameTSCtrl(weaponSelect) {
   canSaveDynamicFields = "0";
   Profile = "GuiDefaultProfile";
   HorizSizing = "right";
   VertSizing = "bottom";
   position = "0 0";
   Extent = "640 480";
   MinExtent = "8 2";
   canSave = "1";
   Visible = "1";
   hovertime = "1000";
   cameraZRot = "0";
   forceFOV = "0";

   new GuiWindowCtrl() {
      canSaveDynamicFields = "0";
      Profile = "GuiWindowProfile";
      HorizSizing = "center";
      VertSizing = "relative";
      position = "197 118";
      Extent = "245 143";
      MinExtent = "8 2";
      canSave = "1";
      Visible = "1";
      hovertime = "1000";
      maxLength = "255";
      resizeWidth = "1";
      resizeHeight = "1";
      canMove = "1";
      canClose = "1";
      canMinimize = "1";
      canMaximize = "1";
      minSize = "50 50";
   };
   new GuiBitmapButtonTextCtrl() {
      canSaveDynamicFields = "0";
      Profile = "GuiDefaultProfile";
      HorizSizing = "center";
      VertSizing = "relative";
      position = "251 178";
      Extent = "140 30";
      MinExtent = "8 2";
      canSave = "1";
      Visible = "1";
      Command = "weaponSelect(buildTool);";
      hovertime = "1000";
      text = "buildTool";
      groupNum = "-1";
      buttonType = "PushButton";
      bitmap = "./buildTool.png";
   };
};
//--- OBJECT WRITE END ---
#3
07/31/2007 (6:18 pm)
That gui above goes in \client\ui

Now go into \client\init.cs and add the exec for that gui.
// weapon select menu 
exec("./ui/weaponSelect.gui");

Now open server\scripts\commands.cs and add:

//-----------------------------------------------------------------------------
// Weapon Select Popup Menu
//-----------------------------------------------------------------------------

function weaponSelect(%client, %weapon)
{
	Canvas.popDialog(weaponSelect); //Hide the dialog once selection was made
	commandtoserver('WeaponSelect', %client, %weapon);	
}


function serverCmdWeaponSelect(%client, %weapon)
{
	%client.getControlObject().use(%weapon);
}


Oh yeh add this to client\scripts\default.bind.cs
moveMap.bindCmd(keyboard, "v", "Canvas.pushDialog(weaponSelect);", "");

And DELETE the client\config.cs file

When in game press 'v' to open the menu and press the button and it should switch to your weapon.

Thats it.. if I missed something let me know. =)
#4
08/01/2007 (8:04 am)
Also if you want the gui to only appear as you're press the button down and dissapear when you release the button you can do this instead:


in client\scripts\default.bind.cs

delete the line: moveMap.bindCmd(keyboard, "v", "Canvas.pushDialog(weaponSelect);", "");

and add:

function toggleWeaponSelect( %val )
{
   if ( %val )
   {
      Canvas.pushDialog(weaponSelect); // Show the dialog 
   }
   else
   {
     Canvas.popDialog(weaponSelect); // Hide the dialog 
   }
}

moveMap.bind(keyboard, v, toggleWeaponSelect);

I didn't test this yet, but it should work. Also be sure to replace the ugly gui with some cool transparent artwork =)
#5
08/01/2007 (11:02 am)
Thanks very much.
#6
08/01/2007 (11:14 am)
No prob :)
#7
08/01/2007 (11:16 am)
I get this error: pushDialog(): Invalid Control: weaponSelect
#8
08/01/2007 (11:23 am)
Know what might be wrong?
#9
08/01/2007 (11:42 am)
Did you exec the gui on the client init.cs file?

exec("./ui/weaponSelect.gui");

Check your spelling also caps probably matter.

Also check the gui name:

new GameTSCtrl(weaponSelect) {
#10
08/01/2007 (4:38 pm)
Btw: if you added the toggle command 'v' is now bound to toggleWeaponSelect and not weaponSelect. You should delete that key bind like I stated above and then delete the config again.
#11
08/02/2007 (3:07 am)
I didn't delete the config. Are you sure it's safe?
#12
08/02/2007 (3:12 am)
Ok, I deleted both configs (in /client and /client/scripts/client,) it didn't seem to make a difference. I changed all of the scripts to weaponSelect from WeaponSelect, and it's still the same. I have added 2 .pngs: blaster.png and machinegun.png.
#13
08/02/2007 (6:47 am)
Copy everything the same as I have it and it will work. Don't change the names of the scripts. The server command is supposed to be called with WeaponSelect IE: serverCmdWeaponSelect. And the function that calls the server command is named weaponSelect. I think you have a typo someplace. Use copy/paste in wordpad and it will work. If you made a new gui make sure the name is weaponSelect. Not the filename I'm talking about the gui's actual name. Which is seen below:

//--- OBJECT WRITE BEGIN ---
new GameTSCtrl(weaponSelect) {

The filename is also weaponSelect.gui and needs to be the same as the exec command. I think you might have a typo or a file named wrong.

EDIT:

You only need to delete the client\config file. It will be created again automagically. Be sure you deleted the keybind for 'v' if you want the press/release functionality.

in client\scripts\default.bind.cs

delete the line:
Quote:
moveMap.bindCmd(keyboard, "v", "Canvas.pushDialog(weaponSelect);", "");
#14
08/03/2007 (1:04 am)
I will look into it, thanks for the help.