David Grace: Avoiding multiple simultaneous schedules?
by Teck Lee Tan · in Torque Game Builder · 09/16/2005 (12:38 pm) · 5 replies
I really hate to do this, since I know the information is here on the forums somewhere, but search as I might, I can't seem to turn up the thread.
Basically I've got a similar problem that David had mentioned some time ago, whereby a rapid firing weapon was set to schedule the repeated shots. However, he initially had an issue where the player could trick the schedule into firing multiple times, thereby effectively tripling the supposed max ROF. I'm experiencing that exact problem right now, and I can't for the life of me find his solution. :(
Here's my current code (don't mind the weird angles and whatnot, I'm using placeholders that aren't aligned properly... also there's some conefire implemented):
Any help would be greatly appreciated. :)
Basically I've got a similar problem that David had mentioned some time ago, whereby a rapid firing weapon was set to schedule the repeated shots. However, he initially had an issue where the player could trick the schedule into firing multiple times, thereby effectively tripling the supposed max ROF. I'm experiencing that exact problem right now, and I can't for the life of me find his solution. :(
Here's my current code (don't mind the weird angles and whatnot, I'm using placeholders that aren't aligned properly... also there's some conefire implemented):
function fxSceneObject2D::shoot(%this, %target) //consider feeding other params like recoil, recovery, etc
{
if($mouse0)
{
$shotcount += 5;
%sourcePos = %this.getPosition();
%targetPos = %target.getPosition();
// %angle = angleBetween2D(%sourcePos, %targetPos);
%angle = %this.getRotation() + 90 + (getRandom($shotcount) - $shotcount/2);
// %distance = vectorLength2D(vectorDistance2D(%sourcePos, %targetPos));
%bullet = new fxStaticSprite2D() { sceneGraph = t2dSceneGraph; };
%bullet.setImageMap(dummy);
%bullet.setRotation(%angle - 90);
%bullet.setSize("4 1");
%bullet.setPosition(%sourcePos);
%bullet.setLinearVelocityPolar(%angle, 250);
%this.schedule(67, "shoot", %target);
}
}
function fxSceneWindow2D::onMouseDown()
{
$mouse0 = true;
$player.shoot($reticule);
}
function fxSceneWindow2D::onMouseUp()
{
$mouse0 = false;
if($player.shoot) cancel($player.shoot);
}Any help would be greatly appreciated. :)
#2
09/18/2005 (9:15 am)
Interestingly enough, it seems I didn't really have a problem in code as far as the schedule cancelling I had built in. What seems to be happening is that when I use the left-click emulation on my tablet's touchpad (ie, tap the pad), it appears to be sending a slightly different signal from hitting the physical touchpad LMB. If I use the hardware LMB with the original code, I can't get the shoot function to schedule more than once simultaneously, though I can do so using the tapping action on the touchpad. Bizarre, that. o_O
#3
Thats a pretty nice solution. Is there any way to alter it so that theres no way the player can shoot more rapidly by pounding on the fire key though? Lets say that I want to fire a shot every 800ms....if the player depresses the fire key, the schedule is reset and the player can shoot once again.
09/30/2005 (5:22 pm)
Hey David, Thats a pretty nice solution. Is there any way to alter it so that theres no way the player can shoot more rapidly by pounding on the fire key though? Lets say that I want to fire a shot every 800ms....if the player depresses the fire key, the schedule is reset and the player can shoot once again.
#4
I'd also like an answer to Chris's question above if anyone is able to supply one. Holding down fire gives me a nice steady fire rate, but hammering on it makes a trail of bullets that even Neo would struggle to avoid!
01/03/2006 (6:30 am)
Wow, time to resurrect an old post :)I'd also like an answer to Chris's question above if anyone is able to supply one. Holding down fire gives me a nice steady fire rate, but hammering on it makes a trail of bullets that even Neo would struggle to avoid!
#5
01/03/2006 (10:32 am)
Perhaps there needs to be a schedule in there that tracks a 'reload' time-frame, and while the schedule is running prevents fire from being triggered again? Something like:function fireWeapon()
{
if (!isEventPending($reloadSchedule))
{
doShoot();
$reloadSchedule = schedule(500, 0, "reloadWeapon");
}
}
function reloadWeapon()
{
// Doesn't really have to do anything, but could play a sound or make a graphic indicator, etc.
}
Torque Owner Fenrir Wolf
Basically, I did the same thing for my shooter, but I had the firing function called by multiple keypresses.
What I wasn't checking, however, was to make sure the fire schedule was already invoked. Therefore, if you hit Spacebar to fire, AND hit Alt to fire, you would invoke TWO firing events that would continuously schedule and therefore double the player's firepower. That isn't good. :)
I simply modified my code as such for the firing event:
function playerFire() { if ( !isEventPending($playerFireSchedule) ) { // do fire stuff } }and then to stop:
function playerFireStop() { if ( isEventPending($playerFireSchedule) ) cancel( $playerFireSchedule ); }Straight out of the shooter demo that comes with Torque, except modified so the fire event won't invoke another schedule if it's already been created.
A check in your shoot event to make sure the event isn't already pending should nix that behavior.