Grenade Code
by Dracola · 01/18/2006 (5:18 pm) · 43 comments
Download Code File
lets make some grenades
>>go to client/config.cs
add
that maps right click to run the console command grenadefire now to make that console command
>>go to server/scripts/commands.cs
add
>>go to games.cs
up top add
in
function GameConnection::createPlayer(%this, %spawnPoint)
under
add
>>go to player.cs
under
add
download the zip I put up and put grenade.cs in server/scripts
now go in and blow kork to smithereens!
ps. I saved a copy of this inside the zip
lets make some grenades
>>go to client/config.cs
add
moveMap.bindCmd(mouse0, "button1", "commandToServer(\'grenadefire\');", "");
that maps right click to run the console command grenadefire now to make that console command
>>go to server/scripts/commands.cs
add
function serverCmdgrenadefire(%client)
{
echo(%client.player.grenade); //echo's the type of grenade we're using
%client.player.grenade.onfire(%client.player,$weaponslot); //runs that grenades onfire function
}>>go to games.cs
up top add
exec("./grenade.cs");in
function GameConnection::createPlayer(%this, %spawnPoint)
under
%player.mountImage(CrossbowImage,0);
add
%player.setInventory(grenade,4); //gives us 4 grenades %player.grenade = "grenade"; //defines the grenades were using as grenade
>>go to player.cs
under
maxInv[HealthKit] = 1;
add
maxInv[grenade] = 4; //lets us hold up to 4 grenades
download the zip I put up and put grenade.cs in server/scripts
now go in and blow kork to smithereens!
ps. I saved a copy of this inside the zip
About the author
Recent Blogs
• Lots of Buildings (with lots of pics)• TimerBar
• AI cinematics
• Dual Wielding
• land mines
#2
01/19/2006 (2:42 pm)
Looks good, will have to try this when I get a chance. Was looking for a better way to implement nades.
#3
01/19/2006 (8:42 pm)
GREAT RESOURCE. Got it working in no time with no errors. Love it!
#4
01/20/2006 (3:36 pm)
glad to know it works so well if there are any questions feel free to ask.
#5
01/20/2006 (11:17 pm)
I have a flying object that is mountable and when you hit right mouse it will crash torque in 1.4 is the best way to stop that from happening is to unbind mouse 1 on mounting in the mounting code and enable it when he dismounts or is there a different way i should approach that?
#6
01/22/2006 (9:37 am)
I have a problem with the ammo/inventory system. If you run out of grenades, then your current weapon ceases to fire and acts empty, at least until I hit the reload button or switch weapons. Why do the ammo of the grenade and the weapon conflict, and do you have any ideas on fixing this?
#7
C2: I don't know why that would be happening I know it didn't happen to me. the two most likely causes for this are.
the grenade is actually depleteing your ammo so you should probably echo how much ammo you have.
or
when you toss a grenade the weapon you are using starts to think that it is using grenade ammo and reloading fixes it.
either way I have no Idea why it is doing this but it does have something to do with your reloading code addition. Are you using the reload resource or your own code?
01/22/2006 (12:29 pm)
greg:well I don't have vehicles yet but you may want to look into why it is crashing? You're solution well work until you want to use the right mouse button for something.C2: I don't know why that would be happening I know it didn't happen to me. the two most likely causes for this are.
the grenade is actually depleteing your ammo so you should probably echo how much ammo you have.
or
when you toss a grenade the weapon you are using starts to think that it is using grenade ammo and reloading fixes it.
either way I have no Idea why it is doing this but it does have something to do with your reloading code addition. Are you using the reload resource or your own code?
#8
And swapping weapons calls a function called "setHud" in the "Activate" state, which has a little code that tells the weapon that yes it does have ammo, and how much.
The problem lies in the ammo system, which I have modified a bit. I doubt anyone'd be able to help unless they saw the weapon.cs and grenade.cs I use. Ah well, I'll look into it myself.
01/22/2006 (12:57 pm)
The reason the reloading makes the weapon able to fire again is because it tells the weapon that yes, there is ammo:function reloadBullets(%image, %obj) {
// 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))
%obj.setImageAmmo(%i,true);
}
%obj.decInventory(%image.clip,1);
%obj.setInventory(%image.ammo,%image.ammo.maxInventory);
%ClipAmmo = %obj.getInventory(%this.clip);
%obj.client.setClipAmountHud(%ClipAmmo);
}And swapping weapons calls a function called "setHud" in the "Activate" state, which has a little code that tells the weapon that yes it does have ammo, and how much.
The problem lies in the ammo system, which I have modified a bit. I doubt anyone'd be able to help unless they saw the weapon.cs and grenade.cs I use. Ah well, I'll look into it myself.
#9
grenade.cs
weapon.cs
Thanks in advance. :)
01/22/2006 (4:04 pm)
In fact, here they are, in case anyone'd be so kind to take a look. Ammo/inventory is not my bag of chips. grenade.cs
weapon.cs
Thanks in advance. :)
#10
function grenade::onInventory(%this,%obj,%amount)
{
%obj.setImageAmmo(%i,%amount != 0);
%GrenadeAmmo = %obj.getInventory(%this);
%obj.client.setGrenadeAmountHud(%GrenadeAmmo);
}
%i is not being defined and this is a strange place to be using a != sign I think you could probably cut out that whole line altogether. it is probably the culprit since it is setting your image's ammo which would not be the grenade since it has no image but your mounted weapon.
edit typo
01/22/2006 (4:48 pm)
this is a little funkyfunction grenade::onInventory(%this,%obj,%amount)
{
%obj.setImageAmmo(%i,%amount != 0);
%GrenadeAmmo = %obj.getInventory(%this);
%obj.client.setGrenadeAmountHud(%GrenadeAmmo);
}
%i is not being defined and this is a strange place to be using a != sign I think you could probably cut out that whole line altogether. it is probably the culprit since it is setting your image's ammo which would not be the grenade since it has no image but your mounted weapon.
edit typo
#11
On another note, would you happen to have any suggestions on how to set the GrenadeAmountHud when you enter the game to 4 rather than 0? In game.cs I have:
But this doesn't work, at least until you kill yourself, and from then on it sets the HUD to 4 when you respawn. I need it to set it correctly the first time. Thanks. :P
01/22/2006 (5:26 pm)
Heh, I stole that from the onInventory function in weapon.cs or something for the heck of it. And indeed it was the problem source. :) Thanks for the un-idiot-ing.On another note, would you happen to have any suggestions on how to set the GrenadeAmountHud when you enter the game to 4 rather than 0? In game.cs I have:
%player.setInventory(grenade,4); //gives us 4 grenades
%player.grenade = "grenade"; //defines the grenades were using as grenade
%player.client.setGrenadeAmountHud("4");But this doesn't work, at least until you kill yourself, and from then on it sets the HUD to 4 when you respawn. I need it to set it correctly the first time. Thanks. :P
#12
01/22/2006 (5:38 pm)
I don't see why that isn't working I would need to see the function setGrenadeAmountHud. If you pulled that directly from http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=1786 which i think you might of it should be working.
#13
01/24/2006 (4:36 pm)
Hah, fixed it, had a dumb piece of code in "OnEnterGame" that set it to 0.
#14
01/24/2006 (4:38 pm)
yep that would do it and be hard to spot. find is always good for that stuff.
#15
01/28/2006 (9:13 am)
Excellent resource. Up and running in no time.
#16
02/02/2006 (3:43 pm)
Edit: Answered
#17
02/02/2006 (4:47 pm)
what is the answer I haven't tested it multiplayer yet?
#18
02/03/2006 (11:53 am)
Well Mr. Pig said explosions called from script like in this resource don't show up in multiplayer. I asked it in the above post but then Mr. Pig answered it in his resource comments.
#19
03/05/2006 (6:16 am)
So if I want to use a grenade with explosions in multiplayer I have to ditch this resource and use a projectile? This resource works great except for the lack of explosion on the client.
#20
03/05/2006 (9:08 am)
Yes I'm quite busy but I may do some engine changes to try and get the explosion on the client. 
Torque Owner Brian Jansen