Game Development Community

Need help with scripting.

by Stephen · in Torque Game Engine · 01/31/2006 (3:47 pm) · 11 replies

Hello,

I have added Ammo Hud Tutorial Resource to my project and I need some help.

I have two different weapons, Machinegun and a Rocket launcher. You fire the Machinegun with the first mouse button and fire the rocket launcher with the second mouse button. I need some help displaying both ammo for each weapon using the Ammo hud tut. resource.

Thanks

#1
01/31/2006 (5:48 pm)
I just copied this from the resource and modified it. you should put it ontop of your current one. tell me if it works or not.
p.s. you need to define $weaponslot whcih should be at the top of weapon.cs by default

To add an secondaryammo hud display do the following.

In C:\GarageGames\torque\example\fps\server\scripts\weapon.cs:

Add at the end of the file:


//-----------------------------------------------------------------------------
//Ammo Display
//-----------------------------------------------------------------------------

function GameConnection::setsecondaryAmmoAmountHud(%client, %amount)
{
commandToClient(%client, 'SetsecondaryAmmoAmountHud', %amount);
}



then change the body of function WeaponImage::onMount(%this,%obj,%slot) to look like this:


// Images assume a false ammo state on load. We need to
// set the state according to the current inventory.

if (%obj.getInventory(%this.ammo)) {

%obj.setImageAmmo(%slot,true);
%currentAmmo = %obj.getInventory(%this.ammo);

} else {
%currentAmmo = 0;
}
if(%slot $= $WeaponSlot)
%obj.client.setAmmoAmountHud(%currentAmmo);
else
%obj.client.setsecondaryAmmoAmountHud(%currentAmmo);



Next add this to C:\GarageGames\torque\example\fps\server\scripts\rifle.cs in the
RifleImage::onFire(%this, %obj, %slot) function after %obj.decInventory(%this.ammo,1);:


%currentAmmo = %obj.getInventory(%this.ammo);
if(%slot $= $WeaponSlot)
%obj.client.setAmmoAmountHud(%currentAmmo);
else
%obj.client.setsecondaryAmmoAmountHud(%currentAmmo);



Next add this to C:\GarageGames\torque\example\fps\client\scripts\PlayGui.cs:


function clientCmdSetsecondaryAmmoAmountHud(%value) // time is specified in seconds
{
secondaryAmmoAmount.setText("Ammo: " @ %value);
}



***UPDATED***
Forgot about updating the Hud on Ammo pick up...do the following: BUG IS FIXED.

In C:\GarageGames\torque\example\fps\server\scripts\weapon.cs change the function Ammo::onInventory(%this,%obj,%amount) to this:


function Ammo::onInventory(%this,%obj,%amount)
{
// The ammo inventory state has changed, we need to update any
// mounted images using this ammo to reflect the new state.
for (%i = 0; %i < 8; %i++) {
if ((%image = %obj.getMountedImage(%i)) > 0)
if (isObject(%image.ammo) && %image.ammo.getId() == %this.getId()) {
%obj.setImageAmmo(%i,%amount != 0);
%currentAmmo = %obj.getInventory(%this);
if(%i $= $WeaponSlot)
%obj.client.setAmmoAmountHud(%currentAmmo);
else
%obj.client.setsecondaryAmmoAmountHud(%currentAmmo);
}
}
}



***END UPDATE***

***UPDATED AGAIN***
I just realized 2 things that were missing. reseting the HUD to 0 on Spawning in the map (curently if you leave the game without leaving torque and come back in game it will be set to what you had when you left) and on Dying.

both changes are made in fps/server/scripts/game.cs

First in function GameConnection::onClientEnterGame(%this) Add this to the end:

//reset secondaryAmmoAmountHud
%this.setsecondaryAmmoAmountHud("0");



then in function GameConnection::onDeath( add to the beginning:

//reset AmmoAmountHud
%this.setsecondaryAmmoAmountHud("0");

***END UPDATE***

Last thing to do is add the GuiTextCtrl to the game.
Go into a map and press F10 for the GUI editor. (this should automatically bring up the playGui.gui)

Add a new GuiTextCtrl and then set its name to secondaryAmmoAmount, then set the profile to AmmoPrintProfile

and dont forget to save the PLayGui.gui.
#2
01/31/2006 (6:42 pm)
Now it shows the different ammo for the weapons but it switches back and forward on both GuiTextCtrl. Any Ideas?
#3
01/31/2006 (6:49 pm)
I'm having a hard time visualizing that but I did miss a spot

Next add this to C:\GarageGames\torque\example\fps\server\scripts\rifle.cs in the
RifleImage::onFire(%this, %obj, %slot) function after %obj.decInventory(%this.ammo,1);:


%currentAmmo = %obj.getInventory(%this.ammo);
if(%slot $= $WeaponSlot)
%obj.client.setAmmoAmountHud(%currentAmmo);
else
%obj.client.setsecondaryAmmoAmountHud(%currentAmmo);

other than that it looks fine can you explain the problem a bit more in depth?
#4
01/31/2006 (8:25 pm)
Okay, Well I made some progress but at fully. I have two Ammo Displays, One for the primary and the second one for the secondary. When I start the game it displays fine but when I fire the primary both displays show the primary but when I fire the secondary it shows up fine.
#5
01/31/2006 (8:50 pm)
This is strange so I'll take it step by step

make sure you did the above change if so when you do your on fire it should run this

%currentAmmo = %obj.getInventory(%this.ammo);//gets ammo
if(%slot $= $WeaponSlot)//checks if it is mounted into the primary slot
%obj.client.setAmmoAmountHud(%currentAmmo);//if it is than we set ammo hud for primary
else//if it isn't than we assume that it is a secondary weapon
%obj.client.setsecondaryAmmoAmountHud(%currentAmmo);//sets the ammo hud for secondary

make sure that looks right since the primary isn't working I'll assume that it is running that

function GameConnection::setAmmoAmountHud(%client, %amount)
{
commandToClient(%client, 'SetAmmoAmountHud', %amount);
}
this then goes here

function clientCmdSetAmmoAmountHud(%value) // time is specified in seconds
{
AmmoAmount.setText("Ammo: " @ %value); //then sets the gui text you hopefully made
}
that all looks good you should double check what I showed here and both versions of the tutorial (mine and Tim Newell's)

make sure your clientCmdSetAmmoAmountHud DOES NOT look like this because having this would cause the problem you are describing.
function clientCmdSetAmmoAmountHud(%value) // time is specified in seconds
{
AmmoAmount.setText("Ammo: " @ %value);
secondaryAmmoAmount.setText("Ammo: " @ %value);
}
#6
02/01/2006 (8:31 am)
I went through the scripts and I'm pretty sure I did everything but its still not working. So here are my scripts, see what I'm doing wrong. Here's the link to my scripts.

Sorry for being a pain.
But Thanks for the help so far.
#7
02/01/2006 (3:34 pm)
At the top of weapon.cs find $WeaponSlot = 2; change that to $WeaponSlot = 0; because that is the slot you are mounting your primary to.
#8
02/01/2006 (5:02 pm)
So are the scripts correct? I still having the switching problem and it hard to explain what the problem is. So I have made a small video of the problem. Here's the link.

Oh I have also changed the $WeaponSlot from 2 to 0.

Thanks
#9
02/01/2006 (6:36 pm)
Found it %slot in ammo::onInventory is undefined instead we use %i making it

function Ammo::onInventory(%this,%obj,%amount)
{
// The ammo inventory state has changed, we need to update any
// mounted images using this ammo to reflect the new state.
for (%i = 0; %i < 8; %i++) {
if ((%image = %obj.getMountedImage(%i)) > 0)
if (isObject(%image.ammo) && %image.ammo.getId() == %this.getId()) {
%obj.setImageAmmo(%i,%amount != 0);
%currentAmmo = %obj.getInventory(%this);
if(%i $= $WeaponSlot)//this line changed
%obj.client.setAmmoAmountHud(%currentAmmo);
else
%obj.client.setsecondaryAmmoAmountHud(%currentAmmo);
}
}
}

that would cause it because it would always return false. check your console because I think that returns an error looking something like this

error line 145 always returns false
#10
02/01/2006 (7:51 pm)
VERY COOL!! It works like a charm now! Thankyou so much for your time!
#11
02/01/2006 (9:02 pm)
That was a pretty clutzy error on my part pretty much what I did is make a new version of the old resource and gave everything different names.