Game Development Community

Scripting - Interrupt/Stop/Cancel a Function with Another Function?

by Steve Acaster · in Torque 3D Professional · 10/17/2009 (11:28 am) · 8 replies

Still somewhat have "modeling head" on, so apologies if I'm overlooking something obvious.

Two things I'm attempting to do via default.bind.cs - both of which are basically canceling a keybind function with another.


1. Dynamically change player speed.

When aiming, I'm trying to half the player's speed. Using $movementSpeed = 0.5; linked to $Zoomon in function ToggleZoom() works fine if the player is first stationary, but if the player is already moving, nothing happens until it stops - there is no "interruption" of the current speed/movement.

I also tried using a commonToServer but it has the same effect, no change until the current movement changes/stops. So if I'm running forwards, I have to release the "w" key before the new speed function kicks in.
function ClientCmdhalfspeed(%client,$movementSpeed)
{
        $movementSpeed = 0.5;
}

function ServerCmdhalfspeed(%client)
{
	if(%client.player.getState() $= "Dead")
	return;
        $movementSpeed = 0.5;
	commandToClient(%client,'halfspeed',%client,$movementSpeed);
}

2. Dynamically stop/cancel a toggled $mvTriggerCount#++;, with another function called via a seperate keybind.
Edited to save my embarressment for such an elementary syntax error.
//And the answer is:
function Trigger1(%val)
{
   $mvTriggerCount1++;
   $mvTriggerCount2 = false;
}

function Trigger2(%val)
{
   $mvTriggerCount2++;
   $mvTriggerCount1 = false;
}

Anyone any thoughts? Am I barking up the wrong tree here?

#1
10/17/2009 (12:28 pm)
I had a few similar things that I needed to do (this references TGEA). One was to see if the player was drowning. Basically, what I did was go into the updateMove() function in the Player class and inserted a hook in the proper place (which varies of course depending on what you're doing) to call the script function, which then did all the checks to see if the player was under water, and if so, if they deserved to either lose endurance points, or health points.

This way, the engine took care of all the variables and I got to control it from script. It would be the same thing here- just create a script function called from the updateMove() function and passed that aiming variable, and let the engine call the function when it needs to be.

That's the only reliable way I've used to consistently check variables and execute script functions accordingly.
#2
10/17/2009 (5:17 pm)
Thanks for the pointers, Ted. Yeah, updatemove() seems to be were the magic happens. (though to be honest most of it is gobbledigook to me!)

I did however sort out my $mvTrigger# issue at point #2 though. That was a nice easy one, all that was required was a glass of merlot. (I'd forgot the most basic syntax, like a dummy --- doh!)

function jump(%val)
{
   $mvTriggerCount2++;
   $mvTriggerCount3=false;
   $mvTriggerCount4=false;
}

And thus "jump" immediately cancels crouch and prone.
#3
10/17/2009 (5:36 pm)
In updateMove() you can use:
F32 temp = (Con::getBoolVariable("$Zoomon",0)==true) ? 0.5f : 1;
moveSpeed *=temp;

just after moveSpeed is set.
#4
10/17/2009 (9:48 pm)
That works fine, Picasso. I can see what the code does, though I'd not have got it myself. Cheers.
#5
10/19/2009 (2:33 pm)
Ah found an issue with the above snippit. It causes the change in speed to affect all the players/AIplayers together rather than just the client.
#6
10/21/2009 (2:48 am)
Seems like what you want Steve is a per-Player (not per PlayerData) speed scalar.

This is where being able to hack things into the Player C++ is handy.
#7
10/22/2009 (4:18 pm)
Well,the code becomes:
F32 temp = (Con::getBoolVariable("$Zoomon",0)==true) ? 0.5f : 1;  
if(!dStricmp(getClassName(), "Player")) moveSpeed *=temp;

Tom is right,this can be done within a class member and a datablock member for each object.
#8
10/22/2009 (7:37 pm)
Thanks for that, Picasso. Works great.

Would that work in MP - not that I'm actually doing mulitplayer, but just wondering - or would it cause all human players to have their speed reset?