Game Development Community

Weapon Cycling in TGEA

by Shawn Busolits · in Torque Developer Network · 02/28/2009 (5:51 pm) · 11 replies

Does anyone know how to implement weapon cycling in a new project in TGEA? I found this resource (http://www.garagegames.com/community/resources/view/5907) but it didn't work for me... it may be way outdated as the post is from 2004.

#1
02/28/2009 (9:33 pm)
That resource still works.

What kind of errors is it giving you?
#2
03/01/2009 (3:14 pm)
That's what was most troubling about it... it wasn't giving me any errors at all, it just simply didn't work. I added all the code in the places it said it was required, but when I ran the program, hitting 'q' or 'ctrl q' didn't have any effect. I'm not sure if it matters, but I'm using the new project template, the one the wizard creates, that allows you to pick a character and a gun at the beginning of the game, not the fps tutorial.
#3
03/01/2009 (4:42 pm)
Make sure you delete your old prefs and config files. Double check your names for your weapons, sometimes a simple mispelling won't throw any errors but it will cause it not to work.

If that doesn't do it then you'll need to compare the differences between the template scripts and the fps (stronghold) scripts. The files to look into would be weapons.cs & inventory.cs, the weapon cycling depends on certain code in those files already being there. I would think the template scripts would be mostly the same, but I don't use the project generator so I wouldn't know.

#4
03/01/2009 (5:06 pm)
I was curious and just tried the resource in a new project generated by the wizard and it works fine.
#5
03/01/2009 (6:33 pm)
K, I just tried it again with a brand new project, and I'm getting the same results. When I press 'q', in the console I get "Mapping string: cycleWeapon to index: 3", but I get no visible results, and pressing 'q' or 'ctrl q' after this does not get me another output of a similar line. I'm using "Crossbow" and "RocketLauncher" as the weapon names, and I've done it with the first letter of each capitalized and not capitalized. the last poster on the link I gave had the same problem, but his was linked to the wrong weapon name.
#6
03/01/2009 (7:25 pm)
OK, this here makes no sense to me... slot 0 is crossbow, slot 1 is rocket launcher. I start out using the crossbow. At the very end of my cycle weapon function, I have the following:

if ( %newSlot != -1 ) {
echo("Current weapon:");
%curWeapon = %this.getMountedImage($WeaponSlot).item.getName();
echo(%curWeapon);
echo("In slot:");
echo(%slot);
echo("new slot set:");
echo(%newSlot);
%this.use($weaponInSlot[%newSlot]);
echo("New weapon:");
%curWeapon = %this.getMountedImage($WeaponSlot).item.getName();
echo(%curWeapon);
}

Which prints this to the console

Current weapon:
Crossbow
In slot:
0
new slot set:
1
New weapon:
Crossbow

That tells me that I am using the crossbow, which is in slot 0. I am now changing to the weapon in slot 1, which should be the rocket launcher, but somehow I still am equipped with the crossbow right after I change to a new slot!!!! All I can see is that there's something wrong with the %this.use(*) function.

EDIT: I tried again with the Rocket Launcher and I get the same exact output as above, just with the slots switched and "Crossbow" replaced with "RocketLauncher". I tried simply putting "rocketLauncher" into the this.use call, and that wouldn't even get me away from the crossbow.
#7
03/01/2009 (10:21 pm)
That is strange, I added in your echo's just to see what I got. I used only the RocketLauncher and the Crossbow "stock" scripts, starting with the RocketLauncher:
Current weapon:
RocketLauncher
In slot:
1
new slot set:
0
New weapon:
Crossbow
and then the Crossbow
Current weapon:
Crossbow
In slot:
0
new slot set:
1
New weapon:
RocketLauncher
And that shows that is working as expected for me.

Does your array read like this:
$weaponInSlot[0] = "Crossbow";
$weaponInSlot[1] = "RocketLauncher";

$maxWeaponSlot = 1;
And then the if/else if switch:
function ShapeBase::cycleWeapon(%this, %data)
{
    %slot = -1;
    if (%this.getMountedImage($WeaponSlot) != 0)
    {
        %curWeapon = %this.getMountedImage($WeaponSlot).item.getName();
        if(%curWeapon $= "Crossbow")
            %slot = 0;
        else if(%curWeapon $= "RocketLauncher")
            %slot = 1;
    }
    // ...
    // rest of function
    // ...
}
If those two sections are correct then it should work for you also.

#8
03/04/2009 (12:52 pm)
This is somewhat of an embarrassing problem... my player cannot use the other weapon, because he does not have it... i only start him out with one, so in the .use function the if statement resolves as false.

On that note, how can I make it so that my player starts out with multiple weapons?
#9
03/04/2009 (1:04 pm)
WOW I need to apologize for seemingly wasting everyone's time... I could not switch to another weapon because, as it turns out, I only started with one weapon. Yea, sorry about that... You guys have been awesomely helpful, I just hope they still let me post after this lol.
#10
03/04/2009 (1:45 pm)
No problem and no need to apologize. I had actually wondered if you had the second weapon in your inventory, but didn't mention it.

If you want to start with multiple weapons, make sure that all of your other weapons and ammo is add to the "allowable inventory" -- see the end of the player datablock in player.cs for that. Next you'll make a modification to game.cs, specifically function GameConnection::createPlayer(), in the middle of the function where it says this:
// Starting equipment
if ($pref::Player:Weapon $= "Crossbow")
{
    %player.setInventory(Crossbow, 1);
    %player.setInventory(CrossbowAmmo, 10);
    %player.mountImage(CrossbowImage, 0);
}
else
{
    %player.setInventory(RocketLauncher, 1);
    %player.setInventory(RocketLauncherAmmo, 10);
    %player.mountImage(RocketLauncherImage, 0);
}
What that if/else statement is doing is giving the player the weapon chosen in the choose model screen -- the other game examples just give you whatever the starting weapon is. So if you want to give more weapons to a newly spawned player just follow the setInventory examples. For example, if you had these weapons in your game you could do this:
%player.setInventory(Pistol, 1);
%player.setInventory(PistolAmmo, 20);
%player.mountImage(PistolImage, 0); // NOTE:  only mount one Image at a time!

%player.setInventory(Rifle, 1);
%player.setInventory(RifleAmmo, 10);

%player.setInventory(RocketLauncher, 1);
%player.setInventory(RocketLauncherAmmo, 5);

%player.setInventory(MachineGun, 1);
%player.setInventory(MachineGunAmmo, 100);

%player.setInventory(Shotgun, 1);
%player.setInventory(ShotgunAmmo, 10);
#11
03/04/2009 (2:56 pm)
You guys are awesome, thanks for all the help!!!