AlliedModders

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

EFFx 10-16-2017 19:16

get_players() returning wrong value
 
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

fysiks 10-16-2017 22:15

Re: get_players() returning wrong value
 
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.

EFFx 10-16-2017 22:24

Re: get_players() returning wrong value
 
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)



fysiks 10-16-2017 23:19

Re: get_players() returning wrong value
 
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 (Post 2554939)
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 (Post 2554939)
'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 (Post 2554939)
Code:

console_print(0, "%d", get_players(iPlayers, iNum, "e", "SPECTATOR"))

There really isn't any reason to do this.

EFFx 10-17-2017 00:02

Re: get_players() returning wrong value
 
Quote:

Originally Posted by fysiks (Post 2554944)
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 (Post 2554944)
There really isn't any reason to do this.

If it returns 1 it's format is correct, am I wrong?

fysiks 10-17-2017 00:33

Re: get_players() returning wrong value
 
Quote:

Originally Posted by EFFx (Post 2554945)
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).

Airkish 10-17-2017 01:55

Re: get_players() returning wrong value
 
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.

PRoSToTeM@ 10-17-2017 02:43

Re: get_players() returning wrong value
 
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.

klippy 10-17-2017 04:29

Re: get_players() returning wrong value
 
Quote:

Originally Posted by Airkish (Post 2554956)
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@ (Post 2554962)
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.

Natsheh 10-17-2017 06:27

Re: get_players() returning wrong value
 
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...


All times are GMT -4. The time now is 14:27.

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