Remember, you can make it save "for each loop iteration" by simply declaring
the variable outside of the loop.

- Andrew

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Yorg Kuijs
Sent: Monday, 2 March 2009 7:41 AM
To: Discussion of Half-Life Programming
Subject: Re: [hlcoders] can this work/is it efficient?

Looking good, thanks for the correction.
Guess I also forgot static variable would also be saved between function 
calls, besides just saving the value for each loop iteration.

Tony Paloma wrote:
> The second loop is unnecessary. You can keep track of the winner player in
> the first loop and then just print the message after. Also, pull out
> iHighestScore from the loop and don't make it static. Making it static
saves
> the value across function calls which isn't what you want.
>
> void CHL2MPRules::DecideRoundWinner2( void )
> {
> #ifndef CLIENT_DLL
>     if ( g_fGameOver )
>       return;
>
>     CBasePlayer *pWinner = NULL;
>     int iHighestScore = 0;
>
>     // count players, leave out spectators
>     for ( int i = 0; i < MAX_PLAYERS; i++ )
>     {
>         CBasePlayer *pCurrentPlayer = UTIL_PlayerByIndex( i );
>
>         if ( !pCurrentPlayer || pCurrentPlayer->GetTeamNumber() ==
> TEAM_SPECTATOR  )
>             continue;
>
>         // if this player's score is higher than the previous highest or
no
> winner is set yet then set current player as the winner
>         if( !pWinner || pWinner->GetPlayerScore() > iHighestScore )
>         {
>             // change the highest score so it can be compared again next
> time this loops
>             iHighestScore = pCurrentPlayer->GetPlayerScore();
>             pWinner = pCurrentPlayer;
>         }
>     }
>
>     if(pWinner)
>     {
>         pWinner->SetRoundWinner( true );
>         UTIL_ClientPrintAll( HUD_PRINTCENTERCONSOLE, "Player '%s' Wins",
> pWinner->GetPlayerName() );
>     }
> #endif
> }
>   

_______________________________________________
To unsubscribe, edit your list preferences, or view the list archives,
please visit:
http://list.valvesoftware.com/mailman/listinfo/hlcoders



_______________________________________________
To unsubscribe, edit your list preferences, or view the list archives, please 
visit:
http://list.valvesoftware.com/mailman/listinfo/hlcoders

Reply via email to