View Single Post
Dr. Greg House
Professional Troll,
Part-Time Asshole
Join Date: Jun 2010
Old 08-08-2012 , 12:02   Re: Dynamic Arrays in SourcePawn
Reply With Quote #27

Quote:
Originally Posted by konichiwa View Post
I would like to suggest something similar to Proposal #3 but in a different approach, by introducing a generic reference type instead of limiting them to array only.

Code:
// & as the reference operator
new &g_Players[];
MakePlayerList() {
    new players[];
    for (new i = 1; i <= MaxClients; i++) {
        if (IsClientInGame(i))
            players += [i];
    }
    // players (dynamic array) had to be allocated in heap and garbage collected
    // g_players (references) can be locally scoped
g_Players = players;
}
This would have few interesting implications.

Code:
// foreach loop with in-place update possible
foreach (new i:&p in players) {
    if (IsClientTrolling(i)) {
        KickClient(i)
        p = 0
    }
}
dv would most likely kill me for giving him more questions than answers though.
I like that idea very very much, especially the foreach with in-place update.
Dr. Greg House is offline