Author Topic: Keybind Help  (Read 1141 times)

I thought I had keybinds down, but apparently not.
Can someone help me make it so that it stops showing the ping after the button is hit the second time?
Code: [Select]
if(!$PingLoopBind)
{
$remapDivision[$remapCount] = "Ping";
$remapName[$remapCount] = "Toggle";
$remapCmd[$remapCount] = "TogglePingLoop";
$remapCount++;
$PingLoopBind = true;
}

function togglePingLoop(%val)
{
if(!%val) return;
$pingLoop = !$pingLoop;
if(!$pingLoop)
{
//nothing!
}
else
{
pingLoop();
}
}
function pingLoop()
{
%ping = serverConnection.getPing();
if($pingLoop = true)
{
clientCMDbottomPrint("\c6Ping: "@%ping,1,1);
schedule(50,0,pingLoop);
}
}

I KNOW THERE IS ALREADY CTRL N FOR THIS. I'm just trying to do an experiment for learning purposes

I fixed it myself. Still asking for any other ways to improve it
Code: [Select]
if(!$PingLoopBind)
{
$remapDivision[$remapCount] = "Ping";
$remapName[$remapCount] = "Toggle";
$remapCmd[$remapCount] = "TogglePingLoop";
$remapCount++;
$PingLoopBind = true;
}

function togglePingLoop(%val)
{
if(!%val) return;
$togglePingLoop = !$togglePingLoop;
if(!$togglePingLoop)
{
cancel($pingLoop);
clientCMDbottomPrint("");
}
else
{
pingLoop();
}
}
function pingLoop()
{
%ping = serverConnection.getPing();
clientCMDbottomPrint("\c6Ping: "@%ping,1,1);
$pingLoop = schedule(50,0,pingLoop);
}

Well the problem with your first code was this line:

if($pingLoop = true)

You're setting $pingLoop to true here. You most likely meant if($pingLoop == true) which can be simplified to if($pingLoop)

-snips-
I'm going to fix a few coding issues, and some things that will help you improve your coding skills. In one of your if statements, you use "=". That character is used to define values - for checking values, you can use "$=", "==", or if it's just a 0 or a 1 (false or true, disabled or enabled, etc), you can just use the variable itself (eg if($ping) ). I fixed a bunch of things. If you need any help, or have any questions, just ask.


if(!$PingLoopBind)
{
   $remapDivision[$remapCount] = "Ping";
   $remapName[$remapCount] = "Toggle";
   $remapCmd[$remapCount] = "togglePingLoop";
   $remapCount++;
   $PingLoopBind = 1;
}

function togglePingLoop(%a)
{
   if(!%a) return;
   $pingLoop = !$pingLoop;
   if($pingLoop)
      pingLoop();
   else
      cancel($pingLoopSched);
}

function pingLoop()
{
   clientCmdBottomPrint("\c6Ping:" SPC serverConnection.getPing(), "1", "1");
   $pingLoopSched = schedule(50, 0, pingLoop);
}

I'll explain all of the things you messed up on later.
   

   


   $pingLoop = !$pingLoop;
   if($pingLoop)
      pingLoop();
   else
      cancel($pingLoopSched);


You could take this part further and change it to this


if(!$PingLoopBind)
{
   $RemapDivision[$RemapCount] = "Ping";
   $RemapName[$RemapCount] = "Toggle";
   $RemapCmd[$RemapCount] = "togglePingLoop";
   $RemapCount++;

   $PingLoopBind = 1;
}

function togglePingLoop(%b)
{
   if(!%b)
      return;

   if(isEventPending($PingLoopSched))
      cancel($PingLoopSched);
   else
      pingLoop();
}

function pingLoop()
{
   clientCmdBottomPrint("\c6Ping:" SPC serverConnection.getPing(), 1, 1);

   $PingLoopSched = schedule(50, 0, pingLoop);
}



schedule returns an index that can be used to cancel a schedule or see if it is still running, using isEventPending. You can shorten this toggle function, if your ping loop is running then cancel it, if it isn't then start it. Now you need only one global variable for the loop, holding your schedule index.

You also don't have to clear the bottom print when canceling it as you already set it to disappear after 1 second.

-snip-
Welcome back!
Anyways, holy stuff. I thought isEventPending() was for events (like events created with the wrench), thanks!
« Last Edit: July 08, 2014, 08:19:26 PM by Ninjaman 4 »