Game Development Community

Spawning with a weapon example?

by Tim Newell · in Torque Game Engine · 12/07/2001 (6:08 pm) · 18 replies

Are there any examples anywhere with spawning with a weapon instead of having to pick one up?

-Tim aka Spock

#1
12/07/2001 (9:42 pm)
I did some code to do this a while ago, so just looked through it again then.
If you just want to give the player a default weapon when the spawn, then in
function createPlayer(%client, %spawnLoc){...}
before the last line (setControl) add

%weapon = new Item()
{
dataBlock = Rifle;
};
MissionCleanup.add(%weapon);
%player.pickup(%weapon, 1);

What i wanted to have is for the player to choose their weapons before entering the game so there would be a gui screen to select their weapons, and the weapon names stored in a $pref:: value. Then in createPlayer(), instead of the previous code snippet, i would call commandtoClient(%client, 'RequestEquipment');.
So corresponding function that gets called is
function ClientCmdRequestEquipment(%this)
{
CommandtoServer('Equip',$Pref::Equipment::PrimaryWeapon,$Pref::Equipment::SecondaryWeapon);
}

then i have

function serverCmdEquip(%client, %prim, %sec, %side, %aux1, %aux2, %aux3)
{
%player = %client.player;

%weapon = new Item()
{
dataBlock = $PrimaryWeapons[%prim, name];
};
MissionCleanup.add(%weapon);
%player.pickup(%weapon, 1);

// same again for sec, side and aux weapons
}

The problem i see with this is that the client could call the serverCmdEquip() function in game and get a new load of weapons, which shouldnt be allowed. This was just a first attempt, I havent thought how else to do it since i first coded it.
#2
09/08/2004 (6:21 am)
Uh what file would you be doing this in???
#3
09/08/2004 (10:30 am)
%player.mountImage(crossbowImage,0);

You can use the above line in game.cs below this line:

%player.setTransform(%spawnPoint);

in function GameConnection::createPlayer(%this, %spawnPoint)

This would spawn your player with the crossbow.

*EDIT You would also have to add it and ammo to the inventory system.
#4
03/14/2007 (10:40 am)
Can you elaborate on this more? Your post isn't to clear and this is exactly what i need to know for my game. Can you clean up your post a little or perhaps post some example scripts?
#5
03/14/2007 (11:33 am)
I made a resource that does this depending on what class you select. You can have a look at it HERE.

I started with the team implementation resource, but you can just look at the code for how I changed the weapons. The code is pretty much the same as what they said above. not really.
#6
03/14/2007 (3:56 pm)
I've been working on your code now mb for about 3 hours... still haven't got it working yet.. I'm trying to call the weapons slection from fps/common/server... I think it will word but i get errors.. take a look...

function GameConnection::onConnect( %client, %name, %weapclass)
{

//find out what was passed so we can work with it
// WEAPONS


        switch$ ( %this.weapClass.weapId )
        {
            // Class 1
            case 1:
    %player.setInventory(mfac,1);
  	%player.setInventory(crossbowAmmo,10);
   	%player.mountImage(mfacImage,0);
	break;

            // Class 2
            case 2:
	%player.setInventory(Pistol,1);
    %player.setInventory(PistolImage,1);
	%player.setInventory(PistolAmmoClip,4);
                break;

            // Class 3
            case 3:
	%player.setInventory(Pistol,1);
  	%player.setInventory(PistolAmmoClip,4);
   	%player.mountImage(PistolImage,0);
                break;

            // Class 4
            case 4:
    %player.setInventory(Shotgun,1);
  	%player.setInventory(ShotgunAmmoClip,5);
   	%player.mountImage(ShotgunImage,0);
                break;

            // Class 5
            case 5:
    %player.setInventory(Shotgun,1);
  	%player.setInventory(ShotgunAmmoClip,5);
   	%player.mountImage(ShotgunImage,0);
                break;
    }

I don't think i'm going to get this working.. I really need help with this.
#7
03/14/2007 (7:09 pm)
Does anyone have an example of the above code by Daniel they could send me... Or if anyone has some example scripts send them to stephentungate@bluebottle.com ... I need to know how to select a weapon from the GUI and then spawn with the weapon on the mount selected.
#8
03/15/2007 (6:42 am)
@Charles: what errors are you getting? That code you listed should go in GameConnection::createPlayer not GameConnection::onConnect.
#9
03/15/2007 (9:38 am)
LINK TO PICTURE OF GUI!!!!!!!!!!!!
www.cybridassaultnetwork.com/OTHER/example.jpg
LINK TO GAME ART AND WHAT I WANT TO HAPPEN WITH THIS POST~!!!!!!!!!
img02.picoodle.com/img/img02/7/2/18/f_Maverick2m_f607f7a.jpg
Link to our project site!!! (Still working on site)
www.ssmods.com/project



Here is what I have done So far.. I hope it helps someone who is having trouble doing what I am doing. And I hope you can help me MB.

In StartMission.Gui put down a guipopupmenuctrl and add to the variable
prefs::player::myweapon (or whatever you want your drop down menu to be called)

it should look like this

new GuiPopUpMenuCtrl(weaponChoice) {
         canSaveDynamicFields = "0";
         Profile = "GuiPopUpMenuProfile";
         HorizSizing = "left";
         VertSizing = "bottom";
         position = "140 332";
         Extent = "126 19";
         MinExtent = "8 2";
         canSave = "1";
         Visible = "1";
         Variable = "pref::Player::myweapon";
         hovertime = "1000";
         text = "Choose mount1";
         maxLength = "255";
         maxPopupHeight = "200";
      };

After you have that done you will want to put a few lines of script in StartMission.GUI in order to let your new dropdownmenu know what it's supposed to do.
//notice weaponchoice refers to the name of the dropdownmenu and myweapon refers to the prefs choice!!!
//weapon choice************************************************************************
//setup the pop up menu
weaponChoice.add("MFAC",5);
weaponChoice.add("EMP",6);
weaponChoice.add("ELF",7);

function weaponChoice::onSelect(%this, %id, %text)
{
   echo("ID = " @ %id);
   if(%id != -1)
   {
      if(%id == 5)
      {

         //echo("you chose option 1--->");
         echo("weaponChoice="@weaponChoice.getSelected());
         echo("weaponChoice="@weaponChoice.getText());
         weaponChoice.setText("mfac");
         //jtPlayerBodyChoice.setText("PlayerBody");


      } else if(%id == 6)
      {
         //echo("you chose option 2");
         echo("weaponChoice="@weaponChoice.getSelected());
         weaponChoice.setText("emp");
         //jtPlayerBodyChoice.setText("PlayerBody2");

      } else if(%id == 7)
      {
         //echo("you chose option 3");
         echo("weaponChoice="@weaponChoice.getSelected());
         weaponChoice.setText("elf");
         //jtPlayerBodyChoice.setText("PlayerBody");
      }
   }
}


Now on the server side in game.cs you will do something like this!!

function GameConnection::createPlayer(%this, %spawnPointz, [b]%myweapon[/b])
{
   if (%this.player > 0)  {
      // The client should not have a player currently
      // assigned.  Assigning a new one could result in 
      // a player ghost.
      error( "Attempting to create an angus ghost!" );
   }

   // Create the player object
   %player = new Player() {
      //dataBlock = PlayerBody;
      //jtmd
      //dataBlock = %jtPlayerBody;
      dataBlock=%this.jtPlayerBody;


      client = %this;
   };
   MissionCleanup.add(%player);

   // Player setup...
   %player.setTransform(%spawnPoint);
   %player.setShapeName(%this.name);

 [b]  echo(%myweapon);
   switch$(%myweapon)
   {
      case "mfac":
         %player.setInventory(mfac,1);
         %player.setInventory(crossbowAmmo,10);
         %player.mountImage(mfacImage,0);
      case "emp":
         %player.setInventory(emp,1);
         %player.setInventory(crossbowAmmo,10);
         %player.mountImage(empImage,0);
      case "elf":
         %player.setInventory(mfac,1);
  	      %player.setInventory(crossbowAmmo,10);
   	     %player.mountImage(mfacImage,0);
      default:
         %player.setInventory(crossbow,1);
         %player.setInventory(crossbowAmmo,10);
         %player.mountImage(crossbowImage,0);
   }

   // Update the camera to start with the player
   %this.camera.setTransform(%player.getEyeTransform());

   // Give the client control of the player
   %this.player = %player;
   %this.setControlObject(%player);[/b]
}
This is where i am so far.. I hope someone will help me and I hope this will help someone else. I am new to Torque so I think by explaining this in a way that I understand it will help alot of people. I hate tutorials where people are like "if your integers are larger then your doplers and danglers etc... lol speak english.
#10
03/15/2007 (9:48 am)
You are missing at least one step in there: GameConnection::createPlayer() is called from GameConnection::onClientEnterGame() (or possibly one more method down the call stack, I can't remember off the top of my head).

Are you sending the parameter %myWeapon into ::createPlayer(), and if so, how is the calling method getting that information?
#11
03/15/2007 (9:56 am)
That is where I need help stephen. I'm sorta stuck.. Do i need to use commandtoclient and commandtoserver? What would you suggest?
#12
03/18/2007 (11:11 am)
Hello??
#13
03/20/2007 (9:55 am)
I sent you an email which explains how I did it a little better than in my resource. Hope it helps you have a better understanding.
#14
03/21/2007 (7:52 am)
Could you also send that to stephentungate@bluebottle.com I don't have access to the email in my GG profile. I greatly appreciate your help.
#15
03/21/2007 (9:01 am)
No prob, I fowarded it to your new email.
#16
03/21/2007 (9:17 am)
I updated the file to explain even more... so it should easier to understand :)
emailed to the address above.
#17
03/22/2007 (9:37 am)
Thanks again.. once i get the weapons spawning I will post full tutorial here for everyone to have...
#18
03/22/2007 (11:02 am)
No prob. I also updated (again) and added the rtf file I sent you to the resource so if anyone else has questions about it they can read it.