wait(ms) or sleep(ms) ?

Started by Kewun, Nov 27, 2016, 07:20 PM

Previous topic - Next topic

Kewun

Does squirrel or vcmp have something like wait(2000) or sleep(2000) like lua has? or i have to use timers?

example:

function onPlayerSpawn(player)
{
  MessagePlayer("You will receive a message in 2 seconds",player)
  wait(2000)
  MessagePlayer("Heres your message",player)
}

.

#1
No it doesn't and even in lua it wouldn't make sense to have them. Suppose you use sleep(2000) whenever a player is killed, because the code is executed synchronously, your server would just pause and stop responding for 2 seconds whenever someone is killed. Obviously that's bad.

The goal here is to take less time with each event to make the server more responsive and not to actually take longer. Especially by doing nothing.

The sleep function is intended for code that runs asynchronously. Why? Because you can yield to other threads that are waiting to get to the CPU. Thus, achieve higher performance by making the most out of your CPU.

However, since Squirrel is not thread safe and therefore cannot be used in a multi-threaded environment, the lack of such function is quite recommended.

PS: Please update your topic title to reflect the question that you asked. Why? Because more people will have this question and they won't be able to find it when they search it. So they'll create duplicate topics.
.

vito

Use timers lol. Sleep is for pause of the code.

KAKAN

If you really want to block your code for a second, use a loop instead.
oh no

Kewun


Mötley

Yeah a loop

I typically use a bool,.

Hear is a simple tiny example.

Sorry this code is from when I first started learning squirrel
time_heal <- array(GetMaxPlayers(), false);
MinuteTimer <- array(GetMaxPlayers(), 0);
SecondTimer <- array(GetMaxPlayers(), 0);

function Second(player)

    if (time_heal[player.ID] == true)
    {
      player.Health ++;
      if (player.Health == 100)
      {
        time_heal[player.ID] = false;
      }
    }
}

function onPlayerJoin(player)
{
  MinuteTimer[ player.ID ] = NewTimer("Minute",60000, 0, player );
  SecondTimer[ player.ID ] = NewTimer("Second",1000, 0, player );
}

function onPlayerPart(player, reason) {

  time_heal[player.ID] = false

  local Minute = MinuteTimer[ player.ID ];
  if ( Minute ) Minute.Delete();

  local Second = SecondTimer[ player.ID ];
  if ( Second ) Second.Delete();

  return true;
}

function onPlayerCommand(player, cmd, text)
{
  if (cmd == "timeheal") {
    if (time_heal[player.ID]) {
      MessagePlayer( "Time heal already activated", player);
      return true;
    }
   
    time_heal[player.ID] = true;
   
    return true;
  }

  return false;
}

So with setting a bool with a loop you can simply ignore what you cant for a given amount of time.

And remember you can alter a pre existing timer.. This is crapy example but should give you the idea
Second_10 <- array(GetMaxPlayers(), 0);

  // For every 10 seconds
  Second_10[player.ID]++;
 
  if (Second_10[player.ID] >= 10)
  {
    //Some code
   
    Second_10[player.ID] = 0;
  }

Xmair

@Mötley your code will simply crash the server because you cannot pass an instance in vcmp plugin's timers.

Credits to Boystang!

VU Full Member | VCDC 6 Coordinator & Scripter | EG A/D Contributor | Developer of VCCNR | Developer of KTB | Ex-Scripter of EAD

Mötley

Quote from: Mötley on Nov 28, 2016, 02:13 PMSorry this code is from when I first started learning squirrel

It was different at that time..

.

Sooo, apparently no one learned anything about why this is not recommended and shouldn't even be attempted?
.

KAKAN

Quote from: . on Nov 28, 2016, 02:25 PMSooo, apparently no one learned anything about why this is not recommended and shouldn't even be attempted?
Kewun didn't try read your big message :p
Everyone knows it( I guess so )
But in some cases, it's useful( like messing a server up if you got access to do so :p )
oh no