Game Development Community

Spawn a player with a weapon?

by Dustin Mellen · in Torque 3D Professional · 07/07/2010 (3:34 pm) · 10 replies

I wanted to know what datablocks and functions I would need to automatically mount a weapon to the player when they spawn. Also, I would like the weapon to use multiple ammo types if at all possible. Basically, I want to create a universal weapon that uses different ammo types, like Megaman. At the moment, I only know how to create a weapon pickup in the level that mounts to the player and they only use one ammo type.

#1
07/07/2010 (3:53 pm)
See the loadout() function in gameCore.cs (Full Template or FPS Example), it lets you assign items you want the player to start with upon spawning. It can also be overridden by different gametypes.

All weapons require a unique item datablock and an image datablock -- all other datablocks can be shared or be unique to the weapon. If your weapon onFire() procedure is different from the generic onFire() then you'll need a unique onFire script for your weapon.

For your specific ammo needs you'll need some way of denoting when a specific sub-type of ammo is in use -- scripted weapon "modes" would handle that for you quite easily, or even simpleryou could duplicate your weapon X number of times for each ammo type and make use of the weapon cycling functionality to change "ammo".
#2
07/09/2010 (3:17 pm)
I figured out how to get the player to mount a weapon at spawn, but I have another more irritating problem. No matter how I edit the ShapeBaseImageData states, the weapon will fire no faster than the swarm gun in the full template (I'm using the empty template). So I've been beating my head against a wall wondering why Torque accepts the mountpoint, projectile, ammo, and mounts the correct mesh to the player, but yet it ignores all of other datablock state information that I copied out of the rocketlauncher.cs file. Here's the datablock I use for my weapon.

datablock ShapeBaseImageData(busterimage)
{
	shapeFile = "art/shapes/gun/buster.dae";
   emap = true;

   mountpoint = 0;
	item = buster;
	ammo = weaponpellet;
	projectileType = Projectile;
	projectile = busterBullet;
	eyeOffset = "0 0 0";
	className = "WeaponImage";
	

	// Images have a state system which controls how the animations
	// are run, which sounds are played, script callbacks, etc. This
	// state system is downloaded to the client so that clients can
	// predict state changes and animate accordingly.  The following
	// system supports basic ready->fire->reload transitions as
	// well as a no-ammo->dryfire idle state.

	// Initial start up state
	stateName[0]                     = "Preactivate";
	stateTransitionOnLoaded[0]       = "Activate";
	stateTransitionOnNoAmmo[0]       = "NoAmmo";

	// Activating the gun.  Called when the weapon is first
	// mounted and there is ammo.
	stateName[1]                     = "Activate";
	stateTransitionOnTimeout[1]      = "Ready";
	stateTimeoutValue[1]             = 0.001;
	stateSequence[1]                 = "Activate";

	// Ready to fire, just waiting for the trigger
	stateName[2]                     = "Ready";
	stateTransitionOnNoAmmo[2]       = "NoAmmo";
	stateTransitionOnTriggerDown[2]  = "Fire";

	// Fire the weapon. Calls the fire script which does 
	// the actual work.
	stateName[3]                     = "Fire";
	stateTransitionOnTimeout[3]      = "Reload";
	stateTimeoutValue[3]             = 0.001;
	stateFire[3]                     = true;
	//stateRecoil[3]                   = LightRecoil;
	stateAllowImageChange[3]         = false;
	stateSequence[3]                 = "Fire";
	stateScript[3]                   = "onFire";

	// Play the relead animation, and transition into
	stateName[4]                     = "Reload";
	stateTransitionOnNoAmmo[4]       = "NoAmmo";
	stateTransitionOnTimeout[4]      = "Ready";
	stateTimeoutValue[4]             = 0.001;
	stateAllowImageChange[4]         = false;
	stateSequence[4]                 = "Reload";
	stateEjectShell[4]               = true;

	// No ammo in the weapon, just idle until something
	// shows up. Play the dry fire sound if the trigger is
	// pulled.
	stateName[5]                     = "NoAmmo";
	stateTransitionOnAmmo[5]         = "Reload";
	stateSequence[5]                 = "NoAmmo";
	stateTransitionOnTriggerDown[5]  = "DryFire";

	// No ammo dry fire
	stateName[6]                     = "DryFire";
	stateTimeoutValue[6]             = 0.001;
	stateTransitionOnTimeout[6]      = "NoAmmo";
};

My goal is to have the weapon fire every time the player hits the fire button, any ideas?
#3
07/09/2010 (4:22 pm)
Too many zeros - use hundredths not thousandths. Also only fire/reload is in real seconds.
change Activate/DryFire to 0.5, and Fire/Reload to 0.01
#4
07/09/2010 (8:45 pm)
I changed it, but it still doesn't respond to any values I enter into the datablock. I put an echo into the mouseFire function, so I know that it's capturing every button press. It also seems to ignore the muzzelvelocity (which is currently 1) on the projectile as well. I echoed the projectile's velocity and it reports that it is set to 40, but the datablock has it set to 1. Why would torque accept the part art assets of the datablock and disregard the rest?
#5
07/09/2010 (9:26 pm)
I figured out what my problem was. I was editing a copy of the datablock script that wasn't getting executed in the datablockexec.cs. That's why it seemed like everything was being overridden. Thanks for the help.

Now that I resolved that, how can I limit the it to one projectile per button press?
#6
07/10/2010 (6:23 am)
lolz - nothing I ain't done before! For semi-automatic the keyword is "WaitForRelease".
//fire state
   stateTransitionOnTimeout[3]      = "WaitForRelease";

//new wait for button release state before the reload state

   stateName[4]="WaitForRelease";
   stateTransitionOnTriggerUp[4]="Reload"; 
//We only move on to the next state when the player releases the trigger
   stateSequence[4]                 = "WaitForRelease";
#7
07/10/2010 (7:21 am)
That works great, thanks! Just one more question: I want this weapon to have regenerating ammo (like the hornet gun in Half-Life), would that best be done in the weapon::onFire function or in a peripheral function called once the weapon is mounted?
#8
07/10/2010 (10:24 am)
How about just commenting out the ammo inventory decrease (decInventory) in onFire? Short of that, have a schedule from onFire that adds a new/recharged bullet in ammo.
#9
07/10/2010 (10:34 am)
Have a function ( ammoReplen() ) that checks for ammo of the current weapon, if it's less than the item max then increase by 1 and schedule this function again a few seconds later. If ammo is maxed out then don't schedule.

Then in your onMount() and onFire() functions make the call to ammoReplen().
#10
07/10/2010 (2:21 pm)
Great tips guys, thanks.