Raised This Month: $51 Target: $400
 12% 

Question about ids and loops


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Depresie
Veteran Member
Join Date: Nov 2013
Old 07-07-2016 , 05:57   Question about ids and loops
Reply With Quote #1

For what we know, generally if id = 0 it is the server's index, not a real player's index

But in a case like this, if id = 0 it should be a real player's index right?

PHP Code:
new iPlayers[32], iNumid
    get_players
iPlayers iNum "h" )
    
    for ( new 
iNum i++ )
    {    
        
id iPlayers];
    } 
__________________
Depresie is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 07-07-2016 , 06:22   Re: Question about ids and loops
Reply With Quote #2

id is never 0, a player index is always between 1 and max players.
0 is worldspawn.

iPlayers is an array that holds the player index. As any array it is indexed from 0 to size - 1.

If you print the array, you could get something like(for 7 players):
PHP Code:
iPlayers[0] = 1
iPlayers
[1] = 2
iPlayers
[2] = 3
iPlayers
[3] = 4
iPlayers
[4] = 5
iPlayers
[5] = 6
iPlayers
[6] = 
__________________

Last edited by HamletEagle; 07-07-2016 at 06:25.
HamletEagle is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 07-07-2016 , 06:26   Re: Question about ids and loops
Reply With Quote #3

Yes, it's getting sorted players into an array.

For example you get players with flag "ac" which means it should get alive,connected and non bot players.
Let's suppose there are only 3 players which satisfies these conditions. And for example players IDs are 13, 5 and 9

This function saves these ids in players[] array starting from 0 and returns players number.
So it will be:
players[0] = 5
players[1] = 9
players[2] = 13

then sets num to 3 because there are 3 sorted players.

This saves IDs to array so don't confuse it with looping actual players.
I mean players[1] won't really equal 1 , it can be any number up to maxplayers.
siriusmd99 is offline
Depresie
Veteran Member
Join Date: Nov 2013
Old 07-07-2016 , 06:52   Re: Question about ids and loops
Reply With Quote #4

Oh, i understood, thanks alot

One more question tho, what is wrong here?
PHP Code:
function(id)
{
       switch(
id)
       {
                case 
IsEven(id): do this
                
case IsOdd(id): do this
        
}
}

stock bool:IsEven(num)
{
    return (
num 1) == 0;
}

stock bool:IsOdd(num)
{
    return (
num 1) == 1;

__________________

Last edited by Depresie; 07-07-2016 at 06:53.
Depresie is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 07-07-2016 , 07:00   Re: Question about ids and loops
Reply With Quote #5

Quote:
Originally Posted by Depresie View Post
Oh, i understood, thanks alot

One more question tho, what is wrong here?
PHP Code:
function(id)
{
       switch(
id)
       {
                case 
IsEven(id): do this
                
case IsOdd(id): do this
        
}
}

stock bool:IsEven(num)
{
    return (
num 1) == 0;
}

stock bool:IsOdd(num)
{
    return (
num 1) == 1;

Why?

Just use return (num & 1) not == 0 or == 1

It will return true if num has 1 and false if it doesnt have.

Like this :
PHP Code:
function(id)
{
       switch(
id)
       {
                case 
IsEven(id): do this
                
case IsOdd(id): do this
        
}
}

stock bool:IsEven(num)
{
    return (
num 1);
}

stock bool:IsOdd(num)
{
    return (
num 1);

siriusmd99 is offline
Depresie
Veteran Member
Join Date: Nov 2013
Old 07-07-2016 , 07:07   Re: Question about ids and loops
Reply With Quote #6

The problem is i get error on compilation on the switch cases
__________________
Depresie is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 07-07-2016 , 07:35   Re: Question about ids and loops
Reply With Quote #7

PHP Code:
switch(id
       { 
                case 
IsEven(id): do this 
                
case IsOdd(id): do this 
        

Use if not switch like :
if(Iseven(id))
do this
if(IsOdd(id))
do this

When you use switch you provide cases when id is equal to some value.
So case IsEven(id): will be available when id is equal to value which is_even returns.

P.S. Also show full code , let me try to compile and understand the error.

Last edited by siriusmd99; 07-07-2016 at 07:36.
siriusmd99 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 07-07-2016 , 08:28   Re: Question about ids and loops
Reply With Quote #8

You can have only constant expressions as cases.
klippy is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 07-09-2016 , 10:26   Re: Question about ids and loops
Reply With Quote #9

A switch is typically used when there are more than 2 possible outcomes. If there are only 2 possible outcomes then it makes more sense to use an if/else. As KliPPy said, the cases within the switch must be constant (cannot be variable during run-time).
PHP Code:
if ( id )
{
    
//id is odd
}
else
{
    
//id is even

If you did want to use a switch, it could be accomplished this way:
PHP Code:
switch ( id )
{
    case 
1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31:
    {
        
    }
    case 
2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32:
    {
        
    }

__________________
Bugsy is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 07-09-2016 , 10:46   Re: Question about ids and loops
Reply With Quote #10

Can u do this instead of switch?
PHP Code:
switch ( id )
{
    case 
1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31:
    {
        
    }
    case 
2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32:
    {
        
    }



PHP Code:
if(id 33)
{
        if(
id 2)
        {
           
//case 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31: 
        
}else{
           
//case 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32: 
        
}       


Last edited by siriusmd99; 07-09-2016 at 10:51.
siriusmd99 is offline
Reply



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 05:25.


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