Vice City: Multiplayer

Off-Topic => Off-Topic General => Topic started by: Kewun on Nov 27, 2016, 07:20 PM

Title: wait(ms) or sleep(ms) ?
Post by: Kewun on Nov 27, 2016, 07:20 PM
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)
}
Title: Re: Super little question
Post by: . on Nov 27, 2016, 08:59 PM
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.
Title: Re: Super little question
Post by: vito on Nov 27, 2016, 10:06 PM
Use timers lol. Sleep is for pause of the code.
Title: Re: Super little question
Post by: KAKAN on Nov 28, 2016, 03:56 AM
If you really want to block your code for a second, use a loop instead.
Title: Re: wait(ms) or sleep(ms) ?
Post by: Kewun on Nov 28, 2016, 06:36 AM
ok thx
Title: Re: wait(ms) or sleep(ms) ?
Post by: Mötley on Nov 28, 2016, 02:13 PM
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;
  }
Title: Re: wait(ms) or sleep(ms) ?
Post by: Xmair on Nov 28, 2016, 02:18 PM
@Mötley your code will simply crash the server because you cannot pass an instance in vcmp plugin's timers.
Title: Re: wait(ms) or sleep(ms) ?
Post by: Mötley on Nov 28, 2016, 02:22 PM
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..
Title: Re: wait(ms) or sleep(ms) ?
Post by: . on Nov 28, 2016, 02:25 PM
Sooo, apparently no one learned anything about why this is not recommended and shouldn't even be attempted?
Title: Re: wait(ms) or sleep(ms) ?
Post by: KAKAN on Nov 28, 2016, 04:18 PM
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 )