Game Development Community

How would I make it so a AIplayer would drop an item or weapon when they died

by Jason Luong · in Torque Game Engine · 10/09/2009 (2:37 am) · 11 replies

I am tasked with trying to code an AI player dropping an item or weapon when they die. I'm currently using AIplayer.cs script I found on torque as the AI bot. I can't find much on what to do, do i have to write an item script for the item being dropped? What do I have to add specifically and where? Thank you.


About the author

Recent Threads


#1
10/09/2009 (4:41 am)
Check Armor::onDisabled in Stronghold/server/scripts/player.cs
#2
10/09/2009 (8:28 am)
But excatly how do i code it so that the item/weapons falls out in the same spot where the player dies. Does torque have like a drop item function I can just call at the end of that Armor::onDisabled function.
#3
10/09/2009 (9:14 am)
I don't know how the TGE engine works very well but here goes. I modded an old AI spawning system where I place nodes and then call the function to spawn them at the nodes, entirely out of script.

What you would need to do is make your item as a rigid shape or static shape (I prefer rigid because it falls to the ground) and then in the AIPlayer::onDeath function,

function AIPlayer::onDeath( %this )
{
createWeapon( %this );
}

then in the createWeapon( %AI )
function createWeapon( %AI )
{
   %weapon = new weapon;
   placeItem(%weapon, %AI);
   MissionCleanup.Add(%bot);
}

And finally the placeItem()
function placeItem( %drop, %AI )
{
   %drop.setTransform(%AI.getTransform());
}

EDIT: You could also get random items to drop, all this would require is that you set up an array. It would work like this:

function getItemName()
{
   %itemName[0] = 3; // must be the Item count in the array
   %itemName[1] = "healthPotion";
   %itemName[2] = "AK-47";
   %itemName[3] = "AMMO";
   %name = GetRandom(1, %names[0]);
   return %itemName[%name];
}

Now that would return a random name of an item but to get it to work when you create an item change createItem(); to this
function createWeapon( %AI )
{
   %weapon = getItemName();
   new %weapon
   placeItem(%weapon, %AI);
   MissionCleanup.Add(%bot);
}

Hope all of this was helpful :)
#4
10/09/2009 (10:41 am)
There is already scripting in TGE to throw an item or weapon currently on the Player or AIPlayer.

%this.throw(datablock);

%this is is the AIPlayer object, datablock is the datablock name or ID for the item/weapon you want to throw.
You can also change the force of the throw. Simply set the "throwForce" field on the AIPlayer object;
%this.throwForce = 1;
That will cause the item to drop at the AIPlayer's position rather than be thrown away from it.
#5
10/09/2009 (12:55 pm)
Here's what I use and it works great too.........Donnie

http://www.garagegames.com/community/resources/view/3889

Oh ya, if their throwing the weapon and ammo to far, go into your inventory.cs file and look for the throw force setting.....I think mine set at 10 or something like that.....Hope it helps
#6
10/10/2009 (2:01 am)
Hey thanks for all the responds everyone. I tried that

%this.throw(datablock);

I fear I might be syntaxing it wrong.

function Armor::onDisabled(%this,%obj,%state)
{
%obj.throw(%image.sword);
}

Is that the correct way? or is it just %obj.throw(sword);
#7
10/10/2009 (3:14 am)
1. %image = %obj.getMountedImage($WeaponSlot);
2. %amount = %obj.getInventory(%image.ammo);
3. %obj.throw(%image.ammo,%amount);
4. %obj.throw(%image.item);


so...i tried this piece of code, it worked halfway!!! ammo dropped out of my Aiplayer died, but not the custom melee sword I added to him and my avatar player. any clue guys? thanks for the help, I'm such a newbie.
#8
10/10/2009 (2:14 pm)
hi guys, i am trying this, but im getting a parse error on the

function createWeapon( %AI )
{
%weapon = getItemName();
new %weapon // its erroring here
placeItem(%weapon, %AI);
MissionCleanup.Add(%bot);
}

now i put in a ; so it looks like new %weapon;

but its still erroring.. any thoughts?
#9
10/10/2009 (2:50 pm)
Ed...just like it says in adams resource, I have this in all player.cs files and it works great. Every time I kill a Ai guy he drops his ammo and weapon perfect, my only problem is how to make him drop a health kit also......Donnie

1) First open fps/server/scripts/player.cs
2) Find the function: function Armor::onDisabled(%this,%obj,%state)
3) add this to the top of that function:

%image = %obj.getMountedImage($WeaponSlot);
%amount = %obj.getInventory(%image.ammo);
%obj.throw(%image.ammo,%amount);
%obj.throw(%image.item);

Thats all there is to the death throw. Simple but effective in the gamming world. On an added note, I lowered my throw velocity so it lands closer to the person dieing. You can adjust that however. Its in the onThrow function in the inventory.cs file.
#10
10/11/2009 (7:19 pm)
@Edward, I have no idea to be honest I Was just making that code up to show one way it could be done, and it seems that the throw is easier to get working, but if you wanted to use my code change that line to,
ItemData::Create(%weapon);
#11
10/11/2009 (8:16 pm)
hmm ill give that a shot, thanks bryce