SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 01/05/2019, 08:24 AM   #1
Viggo
Little Clucker
 
Join Date: May 2013
Posts: 43
Reputation: 2
Default Looping with foreach

Hello everybody,

I have a little question about some zone turfing system, first let me tell you how it works:

Whenever someone enters a zone, his variable p_InZone[ playerid ] is set to the zone he had entered, and.. when he he enters the zone, foreach loop is called to loop over the players Iterator to count the total gang members in the zone he had entered. So, here's the code that is called whenever someone enters a zone:


pawn Code:
public OnPlayerEnterDynCheckpoint( playerid, checkpointid ) {
      foreach( new i : Player )
      {
            if( p_GangID[ i ] == p_GangID[ playerid ] && p_InZone[ i ] == p_InZone[ playerid ] ) // If Player is in same zone and in same gang then count it.
                  count++;
      }
      return count;
}

Now, my question is.. is this fine to do? Because like.. imagine I have 50 players online, and whenever someone of those 50 enters a new zone,that foreach loops all over the players.. So like.. wouldn't this make the server laggy? Or the server can easily handle these loops?
Viggo is offline   Reply With Quote
Old 01/05/2019, 08:56 AM   #2
TMapping
Big Clucker
 
Join Date: Nov 2016
Posts: 56
Reputation: 0
Default Re: Looping with foreach

Code:
public OnPlayerEnterDynCheckpoint( playerid, checkpointid ) {
      foreach( new i : Player )
      {
            if( p_GangID[ i ] == p_GangID[ playerid ] || p_InZone[ i ] == p_InZone[ playerid ] ) count++; // If Player is in same zone and in same gang then count it.
                  
      }
      return 1;
}
Really did not understand what you meant in mind maybe so?
TMapping is offline   Reply With Quote
Old 01/05/2019, 09:13 AM   #3
Viggo
Little Clucker
 
Join Date: May 2013
Posts: 43
Reputation: 2
Default Re: Looping with foreach

Quote:
Originally Posted by TMapping View Post
Code:
public OnPlayerEnterDynCheckpoint( playerid, checkpointid ) {
      foreach( new i : Player )
      {
            if( p_GangID[ i ] == p_GangID[ playerid ] || p_InZone[ i ] == p_InZone[ playerid ] ) count++; // If Player is in same zone and in same gang then count it.
                  
      }
      return 1;
}
Really did not understand what you meant in mind maybe so?
Forget whatever I wrote up there, and answer this question:

Is it fine to loop over all players whenever any player enters a dynamic CP (zone)? Or it makes the server laggy?
Viggo is offline   Reply With Quote
Old 01/05/2019, 03:10 PM   #4
Threshold
High-roller
 
Threshold's Avatar
 
Join Date: Nov 2012
Location: Australia
Posts: 3,101
Reputation: 521
Default Re: Looping with foreach

If you can avoid it, do it. You'd be better off keeping count of players in the zone instead.

The server will more than likely be able to handle it with only 50 players, but again it's not good practice. If you can avoid unnecessary loops, you should.
__________________

Kar's Cops and Robbers 2019
Threshold is offline   Reply With Quote
Old 01/05/2019, 10:28 PM   #5
Viggo
Little Clucker
 
Join Date: May 2013
Posts: 43
Reputation: 2
Default Re: Looping with foreach

Quote:
Originally Posted by Threshold View Post
If you can avoid it, do it. You'd be better off keeping count of players in the zone instead.

The server will more than likely be able to handle it with only 50 players, but again it's not good practice. If you can avoid unnecessary loops, you should.
If I make a new iterator: ZonePlayers[ MAX_ZONES ] < MAX_PLAYERS >

So the new Iterator holds the players that are in the specified zone only, like ZonePlayers[ 2 ] has the players that are in zone ID 2.

Now if I loop over (for example) on ZonePlayers[ 2 ] instead of looping over all connected players, will that be better? or much better? or no difference?
Viggo is offline   Reply With Quote
Old 02/05/2019, 12:32 AM   #6
raydx
Huge Clucker
 
raydx's Avatar
 
Join Date: Feb 2017
Posts: 205
Reputation: 73
Default Re: Looping with foreach

It won't lag, looping for 50 indexes is nothing.
I was looping for 3000 every time someone enter/exit a door, with 200+ players and it was fine.
raydx is offline   Reply With Quote
Old 02/05/2019, 01:06 AM   #7
Threshold
High-roller
 
Threshold's Avatar
 
Join Date: Nov 2012
Location: Australia
Posts: 3,101
Reputation: 521
Default Re: Looping with foreach

Quote:
Originally Posted by Viggo View Post
If I make a new iterator: ZonePlayers[ MAX_ZONES ] < MAX_PLAYERS >

So the new Iterator holds the players that are in the specified zone only, like ZonePlayers[ 2 ] has the players that are in zone ID 2.

Now if I loop over (for example) on ZonePlayers[ 2 ] instead of looping over all connected players, will that be better? or much better? or no difference?
Yes, exactly. Definitely preferable compared to looping over every player in your server.
__________________

Kar's Cops and Robbers 2019
Threshold is offline   Reply With Quote
Old 02/05/2019, 01:27 PM   #8
Viggo
Little Clucker
 
Join Date: May 2013
Posts: 43
Reputation: 2
Default Re: Looping with foreach

Quote:
Originally Posted by Threshold View Post
Yes, exactly. Definitely preferable compared to looping over every player in your server.
But like.. I have 50 zones, and let's say 150 max players.

So the iterator will be: Iterator: playerInZones [ 50 ] [ 150 ], isnt that very huge?
Viggo is offline   Reply With Quote
Old 02/05/2019, 02:39 PM   #9
raydx
Huge Clucker
 
raydx's Avatar
 
Join Date: Feb 2017
Posts: 205
Reputation: 73
Default Re: Looping with foreach

Again - no. Those numbers are small.
Making that iter is a trash idea by the way.
raydx is offline   Reply With Quote
Old 02/05/2019, 06:26 PM   #10
Viggo
Little Clucker
 
Join Date: May 2013
Posts: 43
Reputation: 2
Default Re: Looping with foreach

Quote:
Originally Posted by raydx View Post
Again - no. Those numbers are small.
Making that iter is a trash idea by the way.
I will listen to you, because the way it is right now is never gonna make a single bug, 100% success, but making iterators may create bugs somehow.

Thank you both for your help!
Viggo is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Looping through vehicles with Foreach ross8839 Scripting Help 4 07/06/2017 05:52 PM
Looping Darkwood17 Scripting Help 10 11/07/2015 07:54 AM
Looping all vehicles using foreach Patrick Scripting Help 4 19/02/2013 01:05 PM
Foreach (Looping)? Reklez Scripting Help 2 29/03/2012 05:17 AM


All times are GMT. The time now is 05:36 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.