SetImageTrigger or How to Fire Weapons From Script?
by J.C. Smith · in Torque Game Engine · 05/18/2005 (10:53 am) · 6 replies
Hey there guys and gals. I had a bit of a problem here that I haven't been able to solve all night. I'm heading to bed but figured I'd post it on here and hopefully someone has had this problem before.
I have a grenade throwing weapon in game, but basically all this weapon does is toss a grenade projectile. What I am trying to do is call this routine:
function serverCmdThrowGrenade(%client)
{
if (%client.player.getInventory(GrenadeLauncherAmmo))
{
// We have grenades
%currentweapon = %client.player.getMountedImage(0).item.getName();
echo ("We've got grenades, old weapon is: " @ %currentweapon);
%client.getControlObject().use(GrenadeLauncher); // wield grenade launcher
%client.player.MountImage(GrenadeLauncherImage,0);
%client.player.setImageTrigger(0,true); // Throws one grenade
%client.player.setImageTrigger(0,false);
%client.getControlObject().use(%currentweapon); // go back to old weapon
}
}
What it's supposed to do is check to make sure that a player has grenades, and if they do it switches to the grenade launcher... launches one shot and then changes back to their previous weapon.
Everythign is working as far as the ammo check and the switches of the weapons, but the grenade never fires. It fires if you do it manually using the weapon but I can't get it to fire from script at all. I also in my experimenting changed the lower area after the setimagetrigger to true to put in a schedule to a second routine which would then set imagetrigger back to false and switch weapon. That brings up another problem which I coudn't figure out a workaround for (I admittedly haven't used schedule too much) but in this case it isn't the cause of the problem is just an additional problem.
Basically in the other case I commented out teh SetImageTrigger to false and the control object back to old weapon and called that instead in a function like so:
Schedule (1000,0,"SwitchBackWeapon",%client,%currentweapon);
but in that function the %client and %currentweapon were never being sent. The function would get called but those variables would be empty.
Sorry for two questions in one night. I'm going to sleep off the misery of tonights coding and maybe something will come to me in the morning. =P Any help would be appreciated if anyone has any ideas here.
I have a grenade throwing weapon in game, but basically all this weapon does is toss a grenade projectile. What I am trying to do is call this routine:
function serverCmdThrowGrenade(%client)
{
if (%client.player.getInventory(GrenadeLauncherAmmo))
{
// We have grenades
%currentweapon = %client.player.getMountedImage(0).item.getName();
echo ("We've got grenades, old weapon is: " @ %currentweapon);
%client.getControlObject().use(GrenadeLauncher); // wield grenade launcher
%client.player.MountImage(GrenadeLauncherImage,0);
%client.player.setImageTrigger(0,true); // Throws one grenade
%client.player.setImageTrigger(0,false);
%client.getControlObject().use(%currentweapon); // go back to old weapon
}
}
What it's supposed to do is check to make sure that a player has grenades, and if they do it switches to the grenade launcher... launches one shot and then changes back to their previous weapon.
Everythign is working as far as the ammo check and the switches of the weapons, but the grenade never fires. It fires if you do it manually using the weapon but I can't get it to fire from script at all. I also in my experimenting changed the lower area after the setimagetrigger to true to put in a schedule to a second routine which would then set imagetrigger back to false and switch weapon. That brings up another problem which I coudn't figure out a workaround for (I admittedly haven't used schedule too much) but in this case it isn't the cause of the problem is just an additional problem.
Basically in the other case I commented out teh SetImageTrigger to false and the control object back to old weapon and called that instead in a function like so:
Schedule (1000,0,"SwitchBackWeapon",%client,%currentweapon);
but in that function the %client and %currentweapon were never being sent. The function would get called but those variables would be empty.
Sorry for two questions in one night. I'm going to sleep off the misery of tonights coding and maybe something will come to me in the morning. =P Any help would be appreciated if anyone has any ideas here.
#2
function serverCmdThrowGrenade(%client)
{
if (%client.player.getInventory(GrenadeLauncherAmmo))
{
// We have grenades
%currentweapon = %client.player.getMountedImage(0).item.getName();
echo ("We've got grenades, old weapon is: " @ %currentweapon);
%client.getControlObject().use(GrenadeLauncher); // wield grenade launcher
%client.player.MountImage(GrenadeLauncherImage,0);
%client.player.setImageTrigger(0,true); // Throws one grenade
%client.schedule(1000, "switchBackWeapon", %currentweapon);
}
}
function GameConnection::SwitchBackWeapon(%client, %currentweapon)
{
echo ("Switching Back Weapon Client: " @ %client @ " Current Weapon: " @ %currentweapon);
%client.player.setImageTrigger(0,false);
%client.getControlObject().use(%currentweapon); // go back to old weapon
}
With this routine it switches weapons, turns on grenade fire.... waits one second, then switches back to the old weapon. The grenade never fires though. My grenade function works fine though when throwing manually. Oh and adding to that, the SetImageTrigger function is not broken, I haven't modified the code for it at all, and it works for my bots. (Shrug)
Any ideas?
05/18/2005 (9:39 pm)
Thanks for the quick response Ben. The schedule format there had me scratching my head before, now it makes sense. I'm still having hte problem though of my weapon not firing. SetImageTrigger don't seem to be working. function serverCmdThrowGrenade(%client)
{
if (%client.player.getInventory(GrenadeLauncherAmmo))
{
// We have grenades
%currentweapon = %client.player.getMountedImage(0).item.getName();
echo ("We've got grenades, old weapon is: " @ %currentweapon);
%client.getControlObject().use(GrenadeLauncher); // wield grenade launcher
%client.player.MountImage(GrenadeLauncherImage,0);
%client.player.setImageTrigger(0,true); // Throws one grenade
%client.schedule(1000, "switchBackWeapon", %currentweapon);
}
}
function GameConnection::SwitchBackWeapon(%client, %currentweapon)
{
echo ("Switching Back Weapon Client: " @ %client @ " Current Weapon: " @ %currentweapon);
%client.player.setImageTrigger(0,false);
%client.getControlObject().use(%currentweapon); // go back to old weapon
}
With this routine it switches weapons, turns on grenade fire.... waits one second, then switches back to the old weapon. The grenade never fires though. My grenade function works fine though when throwing manually. Oh and adding to that, the SetImageTrigger function is not broken, I haven't modified the code for it at all, and it works for my bots. (Shrug)
Any ideas?
#3
Also... Might have to wait a tick after you set the image before you can properly set the trigger?
05/20/2005 (12:07 pm)
Can you run with trace(1) on and see if you're getting any console errors?Also... Might have to wait a tick after you set the image before you can properly set the trigger?
#4
05/21/2005 (4:23 am)
Aha! That was the culprit. I had to put in a short delay before firing the grenade. It works like a charm now. Thanks a bunch Ben.
#5
05/21/2005 (1:11 pm)
Glad you got it working!
#6
01/19/2007 (7:19 pm)
Thanks Ben, I had the same problem with my weapon firing.
Associate Ben Garney