Game Development Community

Need Help With Ammo Clips

by John Hameier · in Torque Game Engine · 02/26/2009 (2:22 pm) · 5 replies

What I am trying to do is make it so that I can use clips along with ammo. For some reason what I have done isn't working correctly. Heres what I did.

In crossbow.cs I added this datablock:

datablock ItemData(CrossBowClips)
{
   // Mission editor category
   category = "Ammo";

   // Add the Ammo namespace as a parent.  The ammo namespace provides
   // common ammo related functions and hooks into the inventory system.
   className = "Ammo";

   // Basic Item properties
   shapeFile = "~/data/shapes/crossbow/ammo.dts";
   mass = 1;
   elasticity = 0.2;
   friction = 0.6;

	// Dynamic properties defined by the scripts
	pickUpName = "Crossbow Clip";
   maxInventory = 5;
};

Under the ShapeBaseImageData(CrossBowImage) datablock I added this:
clips = CrossBowClips;

And in the Crossbow::onfire function I added this:
if (%obj.getInventory(%this.ammo) == 0)
{
   if ( %obj.getInventory(%this.clips) != 0)
   {
      %obj.decInventory(%this.clips,1);
      %obj.incInventory(%this.ammo,30);
   }
}
For some reason this isnt working The first if statement will run, but the second will not.
Also I added
%player.setInventory(CrossBowClips,5);
to the game.cs under the spawn player part.

Any help would be greatly appreciated!!

#1
02/26/2009 (10:36 pm)
Using your code you have listed would result in an error message like this in the console:
Quote:starter.fps/server/scripts/inventory.cs (134): Unable to find object: 'M16Clips' attempting to call function 'getName'
Take a look at the name of your ItemData(CrossBowClips) datablock, and look at what you placed under the ShapeBaseImageData(CrossBowImage) datablock:
clips = M16Clips;
should be
clips = CrossBowClips;
That will make your clips work as written.

Tip: add the clips to the "allowable inventory" section of the player datablock and you'll be able to pick up a clip instead of just giving the player 5 of them at spawn.

Hint: you'll need to add some code (can be done in script) to be able "reload" after running out of ammo in all clips. If you run out and pick up another clip nothing will happen until you pick up some ammo also.

EDIT: oh, and I'm sure it's just a mistyping but the actual "onFire" function name should be CrossbowImage::onFire() instead of CrossBow::onFire().
#2
02/27/2009 (3:29 am)
Sorry I had forgotten to change the M16Clips to CrossBowClips here but I have it it my code Just a Typo. I opened the wrong file. And yes I meant CrossbowImage::onFire(). Also the clip should be automatically reloading thats why I added this bit of code:

if ( %obj.getInventory(%this.clips) != 0)
   {
      %obj.decInventory(%this.clips,1);
      %obj.incInventory(%this.ammo,30); 
   }

I probably make it so you can pick it up, but right now I just want to keep it simple
#3
02/27/2009 (7:28 am)
John, can you post your console log. Maybe if we look at that we might be able see something you missed.
#4
02/27/2009 (9:30 am)
Some of the logic to the inventory system isn't as straightforward as it may first appear. Go ahead and make your clips an "allowable inventory" item in player.cs.
maxInv[CrossbowClips] = 5;
With this addition your code will work as you have it presented here. It's necessary in order for the inventory system to work correctly, without it you won't get past
if ( %obj.getInventory(%this.clips) != 0)
I automatically add items to the maxInv array out of habit, so it actually took me a few minutes to understand why the inventory count for the clip would fail for you.

What I meant about the clips reloading is that once you've used up all clips & ammo and the WeaponImage state is in an "empty" status, you won't be able to "reload" the state based system until you pick up some ammo. You'll see what I'm talking about, but your clip code will work as you expect, up until that point.

#5
02/27/2009 (12:49 pm)
Thanks alot Michael, that worked. I cant believe I forgot about that.