#1

Hi everyone!
I just want to know why and in which situations should I use loops, for example:

Код:
for(new i = 0; i < MAX_PLAYERS; i++)
{
// something
return 1;
}
How does it work?
Reply
#2

For example, let's say you have a variable called:
PHP код:
InEvent[MAX_PLAYERS]; 
Then when the player joins the event it becomes:
PHP код:
InEvent[playerid] = 1
Then you can use the loop to send a message to everyone in the event.
PHP код:
for(new 0MAX_PLAYERSi++)
{
  if(
InEvent[i] == 1)
  {
     
SendClientMessage(i, -1"Player joined the event");
  }
  return 
1;

Where ever you would use playerid in a scenario would change to i in the loop.
Examples:
PHP код:
GivePlayerWeapon(i24100);
SetPlayerHealth(i100.0);
So on so fort
Reply
#3

And, if I want to create a timer for my speed meter, it is better to create a new timer for a single player or do it in loop?
Reply
#4

Quote:
Originally Posted by KopeK
Посмотреть сообщение
And, if I want to create a timer for my speed meter, it is better to create a new timer for a single player or do it in loop?
How ever you wish although, I would personally do it for a single player - preferably when he enters the vehicle the speedometer will turn on.
Reply
#5

Quote:
Originally Posted by KopeK
Посмотреть сообщение
And, if I want to create a timer for my speed meter, it is better to create a new timer for a single player or do it in loop?
Create a global timer for a second or mintue detection, and looping over the valid vehicle that's better than a timer on every single player.
Reply
#6

No need to use MAX_PLAYERS even, SA-MP offers a native GetPlayerPoolSize() function. Say you have 100 player slots open, your highest player ID is 50. This means you loop through literally half of that, and is dynamic depending on the count in-game. Another way of looping is also through y_iterate and using "iters", which comes with its own pros and cons. See YSI if you're interested in learning about that.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)