AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   get_players (https://forums.alliedmods.net/showthread.php?t=190072)

GhostMan 07-14-2012 13:58

get_players
 
v1
PHP Code:

new players[32], numtid
get_players
(playersnum"a")

for( new 
0numi++ )
{
    switch(
cs_get_user_team(tid))
    {
        case 
CS_TEAM_T:
        {
            
set_user_health(tid100)
        }
        
        case 
CS_TEAM_CT:
        {
            
set_user_health(tid100)
        }
    }


v2

PHP Code:

new players[32], numtid
get_players
(playersnum"a")

for( new 
0numi++ )
{
    
tid players[i]
    
set_user_health(tid100)


Lets say i want to set 100 health for both teams, so witch one is better or it has no matter?

<VeCo> 07-14-2012 14:18

Re: get_players
 
Obviously the second one. You don't need to check anything if you execute the same code for all cases.

ConnorMcLeod 07-14-2012 14:33

Re: get_players
 
PHP Code:

new players[32], numtid
get_players
(playersnum"a")

for( new 
0numi++ )
{
    
tid players[i]
    
set_user_health(tid100)


->
PHP Code:

new players[32], num
get_players
(playersnum"a")

for( --
numnum >= 0num-- )
{
    
set_user_health(players[num], 100)



GhostMan 07-14-2012 15:01

Re: get_players
 
Connor, all "for" cicles must be started like that

v1
PHP Code:

new g_max_clients get_maxplayers()

new 
players[32], numtid
get_players
(playersnum"a")

for( new 
0num <= g_max_clientsi++ )
{
    
tid players[i]
    
set_user_health(tid100)


in case of avoiding late warnings?

At the moment when i use for cicle so i do it like that

v2
PHP Code:

new players[32], numtid
get_players
(playersnum"a")

for( new 
0numi++ )
{
    
tid players[i]
    
set_user_health(tid100)


You want to say v1 is more secure?

YamiKaitou 07-14-2012 17:46

Re: get_players
 
Connor already posted the most efficient one, why do you keep trying to use different version?

Quote:

Originally Posted by ConnorMcLeod (Post 1750304)
PHP Code:

new players[32], num
get_players
(playersnum"a")

for( --
numnum >= 0num-- )
{
    
set_user_health(players[num], 100)




Liverwiz 07-14-2012 18:37

Re: get_players
 
Quote:

Originally Posted by GhostMan (Post 1750328)
Connor, all "for" cicles must be started like that

No they don't. A for loop requires syntax like this....

counter; condition; increment

you DO NOT need to define the counter in the header. The condition doesn't have to be related to the counter, and the increment is just a way of manipulating the data after the iteration. It is used most commonly as you have discribed, but it can be done in many different ways. It is pretty much a glorified while loop, with a counter.
Use connor's code. it relieves a native call, 3 variables, and is actually more easy to understand. This is because instead of filling an array, then starting from zero at that array and working to the end. it fills the array, takes the count of that array, and works from the top down. Also, you don't need to add your tid = players[i] because you're only calling players once. This is just redundant in this manner. You should only use that statement when you're going to be indexing an array more than once in your loop.

EDIT: but to answer your question....v2 would be the best way OF THOSE SELECTIONS. This is because num will ALWAYS be <= maxPlayers, because its the number if ids indexed into the players array.

GhostMan 07-14-2012 19:07

Re: get_players
 
YamiKaitou - just trying to find out that's the different between "this" or "that" code.

<VeCo> 07-15-2012 06:36

Re: get_players
 
Quote:

Originally Posted by GhostMan (Post 1750496)
YamiKaitou - just trying to find out that's the different between "this" or "that" code.

Connor's code doesn't use additional "i" variable, it uses directly the "num" variable from the native.


All times are GMT -4. The time now is 15:06.

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