Game Development Community

Issue swapping weapons

by Brad McGrath · in Game Design and Creative Issues · 02/03/2010 (10:45 pm) · 11 replies

I have two functional weapons in my game. 1 is the basic crossbow, the other is a staff that fires lightning bolts. I've used several resources but I've not been able to get the swapping to work. I tried doing a cycle system as well as just assigning the weapons to hotkeys but its not working right. The funny thing is that if I start with the lightning staff, I can switch to the crossbow just fine, but not the other way around. I cant swap TO the lightning weapon at all, but it works fine otherwise.

Here's the ones I used.

Weapon Cycling
http://www.torquepowered.com/community/resources/view/5907
...which is pretty much the same as...
http://www.torquepowered.com/community/forums/viewthread/6979

I also tried this.
http://www.torquepowered.com/community/forums/viewthread/20476

I did just as the code instructed, basically copy and paste. I get no error messages in my console log.

For the new weapon, I simply copied the original crossbow.cs file and renamed it. I then went through and updated every function name from crossbow to lightning as well as the sound effects and visual models/textures.

I'm really not sure what else to try here. I've been stuck on this for almost a week now.

I'm using TGE 1.5 if that helps

#1
02/04/2010 (2:32 am)
Are you sure that the player.cs has a maxInventory setting of both the staff and it's ammo? You can mount a weapon on player at spawn/start, but you can't swap to it in-game if it's not allowed in the player's inventory.

Also make sure that you are giving the player the both the staff and ammo for it with setInventory at spawn.
#2
02/04/2010 (3:26 pm)
I tried several things. I tried starting with no weapons and picking both up. I did this several times. Tried picking up staff, then staff ammo, then crossbow, then crossbow ammo. Different variations of that between which weapon I picked up first and whether I had ammo available first or not. I also tried loading with each weapon, and then picking the other up. I also tried loading both weapons. Each time I loaded with a weapon, I made sure to load ammo too. Just to be sure I'd even run and pickup the max number of ammo packs in the mission just to make sure.

No matter the combination, I can always swap TO the crossbow, but NEVER swap to the staff weapon. Like I mentioned above, the weapon works fine by itself, but even without the crossbow or ammo picked up, I can never swap to the lightning. I even tested once by loading the staff in inventory but not mounting it. I then tried to mount it using my cycle command and the hotkeys but it didn't work. I know my cycle command works at least somewhat because I can use it to swap to the crossbow.

Is there something I may have missed in another file? I did add the weapon and ammo to the player.cs file. Here's how I did it.

maxInv[Crossbow] = 1;
maxInv[CrossbowAmmo] = 50;

maxInv[Lightning] = 1;
maxInv[LightningAmmo] = 50;

I made sure in game.cs to exec my new weapon file...
exec("./magicLightning.cs");
//this is just a copy of Crossbow.cs with everything renamed and updated to use the new sound effects and models

Here's where I set the initial player weapon/ammo in game.cs

// Starting equipment
%player.setInventory(Lightning,1);
%player.setInventory(LightningAmmo,25);
%player.mountImage(LightningImage,0);

Again, the weapon works fine when loaded this way.


here's the key binding too for the weapon that doesn't work. The same one for the crossbow does work
In default.bind.cs...
moveMap.bindCmd(keyboard, "1", "commandToServer('use',"Lightning");", "");
which in the options file looks like this
moveMap.bindCmd(keyboard, "1", "commandToServer(\'use\',\"Lightning\");", "");
#3
02/04/2010 (5:20 pm)
Did you make sure to delete config.cs(.dso) after changing default.bind? Silly question, but that's tripped me up many a time!
#4
02/04/2010 (7:04 pm)
Yeah I run the DeleteDSOs and DeletePrefs each time I make a change. That clears that file.
#5
02/04/2010 (8:06 pm)
This sounds like it's most likely a logic bug caused by a copy/paste error. Have you added any echoes where it makes the mount call to see if it actually gets there?
#6
02/04/2010 (10:41 pm)
I added the echo statements like you suggested Scott. It seems that the functions are always being called correctly when I press the hot keys. However, regardless of whether or not I select previous it always calls the crossbow load, never the lightning, even when loading with the crossbow and then picking up the lightning. If I select "next" it now locks up the game. I'm not sure where to go from here. I'm going to delete every thing 1 more time and start over with the newest script I could find.

This one is from 2004
http://www.torquepowered.com/community/resources/view/5907

The other was from 2002.

Is there anyone here who wouldn't mind testing this themselves on their TGE 1.5 to see if it works? Its late so I'm gonna have to wait and start over tomorrow.
#7
02/05/2010 (10:47 am)
I'd set a break point in the function that toggles the active weapon. It looks like you're passing "Lightning" as an arg to that function so make sure "Lightning" is actually getting to the function. If it is, set a break point at the very beginning of the function "use" and F11 thru it to see where "Lightning" is getting lost because it sounds like no matter what arg you pass, the function thinks you still want to use the crossbow. Also, try setting a break point at the very beginning of whatever function(s) "previous" and "next" call to make sure they're calling the the right functions with the right args. Either way, it sounds like the wrong function is being called at the wrong time or the wrong args are being passed.

P.S. DON'T DELETE ANYTHING!!! COMMENT-OUT THE WHOLE THING IF YOU HAVE TO AS YOU MAY FIND THAT THE MISTAKE IS A SMALL ONE AND THE CODEING IS FINE. WOULD SUCK EGGS IF YOU RECODE THE ENTIRE THING JUST TO FIND OUT THAT YOU'VE CODED THE EXACT SAME FUNCTION(S) YOU HAD INITIALLY, MINUS A MISPLACED VAR OR ARG!!!! (yes...I've done this MANY times in the past)

#8
02/05/2010 (2:41 pm)
Well it's all copy and paste so I can easily add it back in. That's why I'm curious if this 6 year old resource still works.
#9
02/05/2010 (3:14 pm)
It should work just fine. The inventory and weapon scripts changed very little over the life of TGE. I've still got my money on it being an error introduced via copy/paste as is usually the case in these instances.
#10
02/12/2010 (2:53 pm)
Did you get this to work?

If not, can you post your cycle function so we can see whats going on?

#11
02/19/2010 (4:13 pm)
No I never got it working. I ended up removing the code completely and just going on without it. I did have someone send me a working copy of some code that has the cycle function working but I've not had time to go over it yet. It's really a minor issue and we made a design decision to just create 3 varieties of orcs to carry the weapons.