Las Venturas Playground
Looping - Printable Version

+- Las Venturas Playground (https://forum.sa-mp.nl)
+-- Forum: Main Talk (https://forum.sa-mp.nl/forum-3.html)
+--- Forum: Development (https://forum.sa-mp.nl/forum-16.html)
+--- Thread: Looping (/thread-29885.html)



Looping - Jay - 09-24-2012

I think we should include Y_Less' foreach library for looping purposes:
http://forum.sa-mp.com/showthread.php?t=92679

It's pretty flexible and efficient. I'm sure we'd greatly benefit from it.


Re: Looping - Russell - 09-24-2012

We already have an efficient way for looping through online player Ids:

Code:
for (new playerId = 0; playerId <= PlayerManager->highestPlayerId(); ++playerId) {
    if (Player(playerId)->isConnected() == false)
        continue;
    doMyProcessing();
}

This does roughly the same as what Y_Less implemented, just more verbose. In the old LVP 3 code we had a separate player iterator, which can be found in "gamemode\tags\lvp-3.0.0-pre-cleanup\Sources\Entities\PlayerIterator.pwn". We could update the syntax to allow looping like..

Code:
for (new playerId = PlayerIterator->first(); playerId != PlayerIterator::InvalidId; playerId = PlayerIterator->next()) {
    doMyProcessing();
}

Do you think this would address your use-case? That would be even faster than Y_Less' implementation, and more readable as well in terms of predictability. The PlayerGroupIterator.pwn file in the same directory is an implementation of a generalized way of doing this, but still specific to player Ids.

I'd like to understand what you're trying to do, that will make it easier to find the right solution. If that is importing Y_Less' script, we can do that :).


Re: Looping - Jay - 10-02-2012

Thanks for the reply. I wasn't aware any looping system was already in place. I stumbled across some code in the NewsController class which just used the old basic MAX_PLAYERS style iteration. I've updated the code.

Whilst I'm sure it would be great to implement a system that indeed iterates only through connected players, is it really necessary? SA-MP only assigns a new higher ID to a player when there are no lower IDs free. For what it's worth, it would probably only avoid a few iterations and would not really make much difference.


Re: Looping - Russell - 10-02-2012

Before I did some optimizations a month ago, over 50% of the gamemode's CPU time was spend in calling the IsPlayerConnected native. Yes -- changing the way some very hot loops work has had a very significant impact on the gamemode's performance. In order to fix this, I (a) changed iterators to iterate until the highest Id, and (b) replaced the IsPlayerConnected call with "Player(playerId)->isConnected()", which directly accesses memory in Pawn instead of calling out to native.