Optimization
#1

Hello,

First, sorry for my bad English, it isn't my first language.

I've a question about optimization in my script. Which of the following proposals is the best ?

Код:
public OnPlayerConnect(playerid)
{
        SetTimerEx("Timer", 1000, false, "i", playerid);
        return 1;
}

public Timer(playerid)
{
       SendClientMessage(playerid, -1, "Example message");
}
OR



Код:
public OnGameModeInit()
{
        SetTimer("Timer", 1000, false);
        return 1;
}

public Timer()
{
       for(new i=0; i<MAX_PLAYERS; i++)
       {
              if(IsPlayerConnected(i))
              {
                   SendClientMessage(i, -1, "Example message");
              }
       }
}
Actually, I'd have said the second one is such better than the first because he has less timer of course but I'd like to have a confirmation.

Thank you for your help.
Etolas
Reply
#2

The first code you sending message to a player when he connect..
The second code you sending to all players when the gamemode is loading.
Reply
#3

Yes I know but don't take care about the SendClientMessage, it's just to give you an example, but I would like to know if I put another action (Like AntiCheat) if the first or the second one is better.
Reply
#4

You're doing 2 different things, in the first code you send a message to the player, where in the second you send a message to 500 player ID's.
You should use SendClientMessageToAll instead of a loop, but still it's useless, as people aren't on when you have just started the gamemode.
Reply
#5

The differents between this two timers that in the second code you using loop to send all players.
better thing to do is
Код:
for(new i=0; i < MAX_PLAYERS+1; i++)
{
      if(IsPlayerConnected(i))
      {
            //Sending the message
      }
}
THis code is sending the message to conntected players..making your script more optimization.
Reply
#6

Quote:
Originally Posted by CalvinC
Посмотреть сообщение
You're doing 2 different things, in the first code you send a message to the player, where in the second you send a message to 500 player ID's.
You should use SendClientMessageToAll instead of a loop, but still it's useless, as people aren't on when you have just started the gamemode.
You haven't understood my question. Thank you anyway.

Quote:
Originally Posted by Kenway
Посмотреть сообщение
The differents between this two timers that in the second code you using loop to send all players.
better thing to do is
Код:
for(new i=0; i < MAX_PLAYERS+1; i++)
{
      if(IsPlayerConnected(i))
      {
            //Sending the message
      }
}
THis code is sending the message to conntected players..making your script more optimization.
Thanks for your answer it was everything i wanted to know.
Reply
#7

The best way is to use SendClientMessageToAll.
Reply
#8

Either I am misunderstanding the actual question or you are all concentrating on the wrong part of his question/code. As far as my understanding goes, he is not requesting how to efficiently send a message to players, he is asking which of the two timers is more efficient in any situation.

SetTimer is a global timer meaning that it is not meant to be for a specific player. Whereas SetTimerEx is a timer which is not meant to be for all players. With a few exception though.
It really depends on what you are going to use the timer for.
Reply
#9

Instead of using a loop that loops through 500 ID's, and check if a player is connected in that ID, you should rather use foreach.
It's more efficient by only looping through connected players.

But in that example you gave, there's no reason to use a loop at all.
Reply
#10

From what I've learned, using loops in timers is actually worse than having "lots of player-based" timers.
Since It'll take a longer time for the timer to 'execute'(well, finish), and will cause desynces
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)