Raised This Month: $ Target: $400
 0% 

Solved Questions about optimization and cs_set_user_team()


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Author Message
frio
Junior Member
Join Date: May 2020
Old 06-02-2020 , 21:25   Questions about optimization and cs_set_user_team()
Reply With Quote #1

I want to assign everyone to a random team. This following code loops through all the players and assigns them to a random team if the team is not maxed out, otherwise it will assign them to the other team.
PHP Code:
    for (0numberOfPlayersi++) {
        
rand random_num(01)
        switch(
rand) {
            case 
1: {
                if (
ctCount maxPlayersPerTeam) {
                    
cs_set_user_team(players[i], CS_TEAM_CT)
                    
ctCount++
                } else {
                    
cs_set_user_team(players[i], CS_TEAM_T)
                    
tCount++
                }
            }
            default: {
                if (
tCount maxPlayersPerTeam) {
                    
cs_set_user_team(players[i], CS_TEAM_T)
                    
tCount++
                } else {
                    
cs_set_user_team(players[i], CS_TEAM_CT)
                    
ctCount++
                }
            }
        }
    } 
It works as intended, but I have two questions about optimization:
  1. I know you are not supposed to re-index arrays, but is the above code fine given that I believe you are only ever accessing players[i] once per for-loop?
  2. It is possible that the above code encounters someone on a team and the random variable will have them "transferred" over to the same team. Would it be more optimal to have a check before transferring? Something like:
    PHP Code:
    if (cs_get_user_team(players[i]) != CS_TEAM_CTcs_set_user_team(players[i], CS_TEAM_CT
    I know the above would re-index the array, I would change that afterwards if this is the better way to go.

Edit: One bonus 3rd question: Why do I always see variables in this forum start with "sz"? Like szVariableName?

Last edited by frio; 06-02-2020 at 22:28. Reason: Marked as solved.
frio is offline
 



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

Forum Jump


All times are GMT -4. The time now is 16:56.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode