Game Development Community

Spawning player with no weapon, then giving them one later on?

by Chris · in Torque Game Engine · 06/09/2009 (11:54 pm) · 7 replies

Hey guys, so here's what I'm trying to do. The player starts with no weapon in his hands (which looks funny since his model runs around arms holding an invisible weapon). But later on in the game, I want him to have the weapon in his hands and be able to shoot it. This later time will be after a global flag is set to true.

So I have it execute server/scripts/crossbow.cs and server/scripts/weapon.cs after this global flag is set to true. The console says the scripts were compiled/loaded successfully, but the weapon isn't added into the player's hands. How can I achieve this?

Any help is greatly appreciated as always.

#1
06/10/2009 (4:52 am)
In the player spawn code, look for this line;
%player.mountImage(crossbowImage,0);


I can't remember if it should be changed to;
%player.unmountImage(crossbow,0);
or
%player.mountImage(); //--empty field

One of those should work.


www.garagegames.com/community/forums/viewthread/51854

Starting around post #125.

If I'm wrong, let me know.

Update:
Oh, wait, I should have read the post better. You can get the player spawned without a weapon but can't add it later?

OK, Make sure your inventory has

MaxInv[crossbow]=1;

Are you using the stock player or a custom player?

I've been told to avoid global flags.

Tell us more about your situation. I think the Starter.FPS using the onCollision to mount the image.

function Weapon::onPickup(%this, %obj, %shape, %amount)
{
   // The parent Item method performs the actual pickup.
   // For player's we automatically use the weapon if the
   // player does not already have one in hand.
   if (Parent::onPickup(%this, %obj, %shape, %amount)) {
      serverPlay3D(WeaponPickupSound,%obj.getTransform());
      if (%shape.getClassName() $= "Player" && 
            %shape.getMountedImage($WeaponSlot) == 0)  {
         %shape.use(%this);
      }

I'm sorry. I just seem to be babbling. Just ignore this post. I'm a doofus. It's too early in the morning for me.

Tony
#2
06/10/2009 (9:01 am)
The starting inventory is given to the player at spawn, you'll find that in "scripts/server/game.cs". Just simply not give the player the weapon at spawning.

Not executing script files, especially datablocks, at mission load is a bad idea in a multiplayer game. There's a stage in there where the server sends the information to the client, if the datablocks are executed after mission load then the client never has the data replicated to it -- which means those datablocks don't exist for the client and doesn't know how to handle that information when the server communicates with it.

Once your condition is set you can easily enough loop through all clients and follow the same procedure already evident in game.cs and give them weapons and allow them to be mounted.

Weapon/ammo pickups will work as normal unless you account for that too.
#3
06/10/2009 (9:13 am)
ic
#4
06/10/2009 (9:25 am)
Thanks for the replies guys.

Michael, I don't see anything about inventory or weapons in game.cs except the scripts that it executes initially. I'm looking in inventory.cs, and I see the function:

function ShapeBase::setInventory(%this,%data,%value)
{
   // Set the inventory amount for this datablock and invoke
   // inventory callbacks.  All changes to inventory go through this
   // single method.

   // Impose inventory limits
   if (%value < 0)
      %value = 0;
   else {
      %max = %this.maxInventory(%data);
      if (%value > %max)
         %value = %max;
   }

   // Set the value and invoke object callbacks
   %name = %data.getName();
   if (%this.inv[%name] != %value) 
   {
      %this.inv[%name] = %value;
      %data.onInventory(%this,%value);
      %this.getDataBlock().onInventory(%data,%value);
   }
   return %value;
}

How would I invoke this function to add a specific weapon later on in the game?

Thanks again guys.
#5
06/10/2009 (10:03 am)
function GameConnection::createPlayer() in game.cs -- there's a section that deals with starting inventory.

// Starting equipment
%player.setInventory(Crossbow, 1); // This gives you the crossbow
%player.setInventory(CrossbowAmmo, 10); // This gives you crossbow ammo
%player.mountImage(CrossbowImage, 0); // This mounts the crossbow

Once your condition is set you would loop through the client group and pretty much follow that procedure there. Determine individual clients then determine the %player variable (the control object) for each client and give them whatever weapon(s) your game uses.

Note: only one weapon can be mounted at a time, so only call that one once. You can use setInventory for any number of unique items.
#6
06/10/2009 (11:01 am)
Michael thanks, I found this. However I tried putting these statements within an if statement so the player wouldn't have the weapon until a global variable is declared true. Yet even when the global variable is declared true, it doesn't make the weapon show up in the player's hands. Here's the code:

if($gameflag == true)
{
// Starting equipment  
%player.setInventory(Crossbow, 1); // This gives you the crossbow  
%player.setInventory(CrossbowAmmo, 10); // This gives you crossbow ammo  
%player.mountImage(CrossbowImage, 0); // This mounts the crossbow  
}
else
{ $gameflag = false; }
#7
06/11/2009 (12:15 pm)
What is %player? In the function in game.cs where that code is used, take a look at what the variable %player is set to. Without specifying exactly what you mean by '%player', the code cannot do what you intend it to do.