Holding Buttons
by Jeff Raab · 12/07/2007 (1:22 pm) · 2 comments
This is a network friendly implementation for a time-based hold/tap context button.
The example code will show you how to have a function that will make a player sprint if held for more than 1 second, and if tapped, toggle between jogging and walking.
First, in your default.binds.cs
Next, in your server/scripts/game.cs(Or whever the player object is spawned)
Find:
And add in:
before the };
You'll have to figure the particulars(how the player's sprinting is setup, etc. But this gives you the basic functionality for doing a button that when held does one thing, and when tapped can do another.
You could easily drop the toggling functionality if you just want a button that does something when held.
Hope this helps someone out there.
The example code will show you how to have a function that will make a player sprint if held for more than 1 second, and if tapped, toggle between jogging and walking.
First, in your default.binds.cs
function sprint(%val)
{
//first get our player object
%client = ServerConnection;
%player = %client.getControlObject();
//double check that our 'sprint timer' is clear, unless we're keeping track of the button being held.
if(%player.startSprint != 0)
%sprintTime = getSimTime() - %player.startSprint;
else
%sprintTime = 0;
//Button is being pressed, do our 'active' stuff!
if(%val)
{
//Do a check to make sure the player is even allowed to sprint(You'd have a function that would check
//stamina, if they're jumping, etc
if%player.canSprint())
{
//Check our time on how long the button has been held
//1000 = 1 second
if(%sprintTime >= 1000)
{
//We can sprint, so do stuff!
//What we're doing here, is getting our current speed so when we stop sprinting, we can go back
//to it
if(%player.getMovement() !$= "Sprint")
%player.lastSpeed = %player.getMovement();
//if we're not sprinting yet, make it so
if(%player.getMovement() !$= "Sprint")
{
//Tell the server we're sprinting now
//Note: In my engine build, i have a string-based movement speed functionality
//So replace this with your sprint code
commandToServer('setMovement', "Sprint");
//Get our current weapon
%weapon = %player.getMountedImage($WeaponSlot);
//And do a sprint animation on the arms
%player.setArmThread(%weapon.sprintAnim);
}
}
// If our Sprint timer is new(we just now pressed the button) then we need to clear it for sure, and
// Mark our starting time and set the client's 'SprintLoop'
if(%player.startSprint == 0)
%player.startSprint = getSimTime();
//This calls this function again in 50ms so we can keep tabs on if we're still holding the button or not
%player.sprintLoop = schedule( 50, 0, sprint, 1);
}
else
{
//We actually aren't allowed to sprint, so check if we can do the next best(jogging)
if(%player.canJog())
//If we where jogging when we suddenly can't anymore(out of stamina, etc) Get our last speed
if(%player.getMovement() $= "Sprint")
commandToServer('setMovement', %player.lastSpeed);
else
//If we can't jog, We'll walk.
commandToServer('setMovement', "walk");
}
}
//We stopped pressing the button!
else
{
// if we haven't pressed long enough to have warrented sprinting
//we toggle our movement speed
if(%sprintTime < 1000)
{
//first, drop the sprint step as a precaution
if(%player.getMovement() $= "Sprint")
%player.setMovement(%player.lastSpeed);
//then do the toggle checking
if(%player.getMovement() $= "jog")
commandToServer('setMovement', "walk");
else
commandToServer('setMovement', "jog");
}
//We where sprinting, but aren't anymore.
else
{
//get our weapon.
%weapon = %player.getMountedImage($WeaponSlot);
//Set the look animation
%player.setArmThread(%weapon.customLookAnim);
//make sure we're not too tired to jog
if(%player.getMovement() $= "Sprint")
{
if(%player.lastSpeed $= "jog" && %player.canJog())
CommandtoServer('SetMovement', "jog");
else
CommandtoServer('SetMovement', "walk");
}
}
//clean the timer
cancel(%player.sprintLoop);
%player.startSprint = 0;
}
}Next, in your server/scripts/game.cs(Or whever the player object is spawned)
Find:
%player = new Player()
{
dataBlock = PlayerBody;
client = %this;
};And add in:
//The starting time for our sprint timer startSprint = 0; //Our initial speed(assumes it's Jog) lastSpeed = "jog"; //Our sprint Loop object sprintLoop = 0;
before the };
You'll have to figure the particulars(how the player's sprinting is setup, etc. But this gives you the basic functionality for doing a button that when held does one thing, and when tapped can do another.
You could easily drop the toggling functionality if you just want a button that does something when held.
Hope this helps someone out there.
About the author

Torque Owner Glenn Thomas