Raised This Month: $32 Target: $400
 8% 

get_players() returning wrong value


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 10-16-2017 , 19:16   get_players() returning wrong value
Reply With Quote #1

Sup, I'm trying to kick a random spec, but iNum (code below), is returning 0:

PHP Code:
#include <amxmodx>
#include <amxmisc>

#define PLUGIN "New Plug-In"
#define VERSION "1.0"
#define AUTHOR "author"

public plugin_init() 
{
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
set_task(2.0"CheckSpec", .flags "b")
}

public 
CheckSpec()
{
    static 
iPlayers[MAX_PLAYERS]
    new 
iNumi
    get_players
(iPlayersiNum)
    
    for(
0iNumi++)
    {
        if(
<= get_user_team(iPlayers[i]) <= 2)
        {
            
kickRandomSpec()
        }
    }
}

kickRandomSpec()
{
    static 
iPlayers[MAX_PLAYERS], iNum
    get_players
(iPlayersiNum"e""SPECTATOR")
    
    
console_print(0"%d"iNum// it prints 0, why?
    
    
new iRandomPlayer iPlayers[random(iNum)] // here

I added:

PHP Code:
console_print(0"%d"get_players(iPlayersiNum"e""SPECTATOR")) 
And it's returning 1, why is iNum returning 0 and giving me index out of bounds?

AMXX: AMX Mod X 1.8.3-dev+5106
__________________
• Ranking System • AutoMix 5vs5 System
• Web Ban System • Plugins for free

____________________________________________
For private works:
• Discord: EFFEXo#8850 • Steam: EFFEXo

Last edited by EFFx; 10-16-2017 at 20:32.
EFFx is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 10-16-2017 , 22:15   Re: get_players() returning wrong value
Reply With Quote #2

You aren't printing iNum (in your other console_print), you are printing the return value of get_players() (which is irrelevant).

I don't see anything in your code that could give an index out of bounds error.

Also, your CheckSpec() function makes no sense.
__________________
fysiks is offline
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 10-16-2017 , 22:24   Re: get_players() returning wrong value
Reply With Quote #3

I printed get_players() for check if it's format is correct, and it's printed 1 (its correct). Abou the sense, it's an example. I could not show the whole plugin ('cos it's 4k lines besides be private). But the code does not change anything "fatal" to return the iNum 0. CT and TERRORIST are returning correct, only SPECTATOR that isn't, idk why.

I did that to pick a random spec:

PHP Code:
new g_iSpecNumg_iIsSpec[MAX_PLAYERS 1
PHP Code:
getRandomSpec()
{
    new 
iPlayers[MAX_PLAYERS], iNum
    get_players
(iPlayersiNum)
    for(new 
idi;iNum;i++)
    {
        
id iPlayers[i]
        
        if(!
isTeam(id))
        {
            
g_iIsSpec[g_iSpecNum] = id
            g_iSpecNum
++
        }
    }
    return 
g_iIsSpec[random(g_iSpecNum)]

And reset after kick someone. But i really wanna know what's the issue to turn back to the get_players()

The index out of bounds is 'cos the iNum returns 0:

iRandomPlayer = iPlayers[random(iNum)] - It'll be 0 aways, and prints that:

Code:
L 10/17/2017 - 00:00:01: [AMXX] Run time error 4: index out of bounds
'Bout I did not actually printed the iNum, what do you mean? Didn't I do it here?:

PHP Code:
kickRandomSpec()
{
    static 
iPlayers[MAX_PLAYERS], iNum
    get_players
(iPlayersiNum"e""SPECTATOR")
    
    
console_print(0"%d"iNum)

__________________
• Ranking System • AutoMix 5vs5 System
• Web Ban System • Plugins for free

____________________________________________
For private works:
• Discord: EFFEXo#8850 • Steam: EFFEXo

Last edited by EFFx; 10-16-2017 at 22:32.
EFFx is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 10-16-2017 , 23:19   Re: get_players() returning wrong value
Reply With Quote #4

In the original documentation for version 1.8.2, flags were stated as being deprecated for get_players() . Did they change that in 1.8.3-dev?

Quote:
Originally Posted by EFFx View Post
The index out of bounds is 'cos the iNum returns 0:

iRandomPlayer = iPlayers[random(iNum)] - It'll be 0 aways, and prints that:

Code:
L 10/17/2017 - 00:00:01: [AMXX] Run time error 4: index out of bounds
Zero (0) is a valid index and will never cause an "index out of bounds" error.

Quote:
Originally Posted by EFFx View Post
'Bout I did not actually printed the iNum, what do you mean? Didn't I do it here?:

PHP Code:
kickRandomSpec()
{
    static 
iPlayers[MAX_PLAYERS], iNum
    get_players
(iPlayersiNum"e""SPECTATOR")
    
    
console_print(0"%d"iNum)

I was referring to this:

Quote:
Originally Posted by EFFx View Post
Code:
console_print(0, "%d", get_players(iPlayers, iNum, "e", "SPECTATOR"))
There really isn't any reason to do this.
__________________

Last edited by fysiks; 10-16-2017 at 23:21.
fysiks is offline
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 10-17-2017 , 00:02   Re: get_players() returning wrong value
Reply With Quote #5

Quote:
Originally Posted by fysiks View Post
Zero (0) is a valid index and will never cause an "index out of bounds" error.
I actually don't know why, the line's log error is it:

PHP Code:
iRandomPlayer iPlayers[random(iNum)] 
Quote:
Originally Posted by fysiks View Post
There really isn't any reason to do this.
If it returns 1 it's format is correct, am I wrong?
__________________
• Ranking System • AutoMix 5vs5 System
• Web Ban System • Plugins for free

____________________________________________
For private works:
• Discord: EFFEXo#8850 • Steam: EFFEXo
EFFx is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 10-17-2017 , 00:33   Re: get_players() returning wrong value
Reply With Quote #6

Quote:
Originally Posted by EFFx View Post
If it returns 1 it's format is correct, am I wrong?
Not according to the documentation here and here. It says it has no return value (which means that any value that it does return is irrelevant i.e. it doesn't mean anything).
__________________
fysiks is offline
Airkish
AlliedModders Donor
Join Date: Apr 2016
Location: Lithuania
Old 10-17-2017 , 01:55   Re: get_players() returning wrong value
Reply With Quote #7

I just noticed that you have 2 same static variables in 2 functions
PHP Code:
static iPlayers[MAX_PLAYERS
(IIRC you can't use static variable anywhere but in the function you declared it).

Not sure tho if this is what causes your problem.
__________________
Airkish is offline
PRoSToTeM@
Veteran Member
Join Date: Jan 2010
Location: Russia, Ivanovo
Old 10-17-2017 , 02:43   Re: get_players() returning wrong value
Reply With Quote #8

Imagine you have 1 player on the server and he is in CT team. So, get_players in CheckSpec will return 1 player, then you check if him is CT or TR, if so you call kickRandomSpec and its get_players return 0 players, because you have only 1 CT which isn't a spectator.

random(0) is like random_num(0, -1), so its invalid and looks like its returning negative value.
__________________
PRoSToTeM@ is offline
Send a message via ICQ to PRoSToTeM@ Send a message via Skype™ to PRoSToTeM@
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 10-17-2017 , 04:29   Re: get_players() returning wrong value
Reply With Quote #9

Quote:
Originally Posted by Airkish View Post
I just noticed that you have 2 same static variables in 2 functions
PHP Code:
static iPlayers[MAX_PLAYERS
(IIRC you can't use static variable anywhere but in the function you declared it).

Not sure tho if this is what causes your problem.
Those are two completely different variables in different scopes.

Quote:
Originally Posted by PRoSToTeM@ View Post
random(0) is like random_num(0, -1), so its invalid and looks like its returning negative value.
IIRC random() produces a garbage value if you give it anything below 1.

Last edited by klippy; 10-17-2017 at 04:30.
klippy is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 10-17-2017 , 06:27   Re: get_players() returning wrong value
Reply With Quote #10

You can easily check if theres spec in one function i really dont understand why do you need two functions to do this

PHP Code:
public plugin_init() 
{
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
set_task(2.0"CheckSpec", .flags "b")
}

public 
CheckSpec()
{
    new 
iPlayers[MAX_PLAYERS], iNum;
    
get_players(iPlayersiNum"e""SPECTATOR")
    if(!
iNum) return;
    new 
randomID iPlayers[random(iNum)];


You can just do this...
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 10-17-2017 at 06:28.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
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 01:43.


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