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

Solved Random Player Help


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Ravrob
Junior Member
Join Date: Oct 2019
Location: The Ice
Old 07-07-2020 , 22:11   Random Player Help
Reply With Quote #1

So I've been trying to choose a random player using
Code:
stock Action GetRandomPlayer()
{
	int clients[MaxClients + 1];
	int clientCount;
	for (int i = 1; i <= MaxClients; i++)
	{
		if (IsClientInGame(i) && IsPlayerAlive(i))
		{
			clients[clientCount++] = i;
			
			clients[GetRandomInt(0, clientCount + -1)];
		}
	}
	
	return (clientCount == 0) ? -1 : clients[GetRandomInt(0, clientCount-1)];
}
but every time I try to compile I get this error and I genuinely don't understand how to fix this
Code:
error 161: brackets after variable name indicate a fixed-size array, but a dynamic size was given - did you mean to use 'new int[size]' syntax?
If anyone could help clear my confusion I would be very thankful.

Last edited by Ravrob; 07-08-2020 at 02:00.
Ravrob is offline
Balimbanana
Member
Join Date: Jan 2017
Old 07-07-2020 , 22:26   Re: Random Player Help
Reply With Quote #2

It is due to this:
Code:
int clients[MaxClients + 1];
The issue is that MaxClients is a dynamic variable that can't be used to specify fixed array sizes.
You may be able to create the dynamic int array with just [] Also, you may want to adjust the for loop to do < MaxClients+1 as <= MaxClients will most likely skip the last client if the server is full (as less than or equal to):
Code:
stock Action GetRandomPlayer()
{
	int clients[];
	int clientCount;
	for (int i = 1; i < MaxClients+1; i++)
	{
		if (IsClientInGame(i) && IsPlayerAlive(i))
		{
			clients[clientCount++] = i;
			
			clients[GetRandomInt(0, clientCount + -1)];
		}
	}
	
	return (clientCount == 0) ? -1 : clients[GetRandomInt(0, clientCount-1)];
}
If the dynamic array doesn't work, you could workaround it with:
Code:
int MaxCL = MaxClients+1;
int clients[MaxCL];
Balimbanana is offline
Ravrob
Junior Member
Join Date: Oct 2019
Location: The Ice
Old 07-07-2020 , 22:40   Re: Random Player Help
Reply With Quote #3

So when I use
Code:
int clients[];
It outputs these two errors:
Code:
error 036: empty statement
error 009: invalid array size (negative, zero or out of bounds)
and creates a whole gaggle of errors in
Code:
clients[clientCount++] = i;
I also tried using MaxCL and it had the same issue as the original code.

Last edited by Ravrob; 07-07-2020 at 22:41.
Ravrob is offline
Balimbanana
Member
Join Date: Jan 2017
Old 07-07-2020 , 22:48   Re: Random Player Help
Reply With Quote #4

What version of SourceMod is it compiled on?
Balimbanana is offline
Ravrob
Junior Member
Join Date: Oct 2019
Location: The Ice
Old 07-07-2020 , 22:56   Re: Random Player Help
Reply With Quote #5

I believe it's compiled on 1.9.
Ravrob is offline
Balimbanana
Member
Join Date: Jan 2017
Old 07-07-2020 , 23:28   Re: Random Player Help
Reply With Quote #6

Most of the time people use the MAXPLAYERS+1 variable instead. It is just a static define for 64 unless you compile SourceMod with a different value. It won't be too much more to initialize the array with 65 indexes instead.
Balimbanana is offline
Ravrob
Junior Member
Join Date: Oct 2019
Location: The Ice
Old 07-08-2020 , 00:29   Re: Random Player Help
Reply With Quote #7

It compiled just fine using MAXPLAYERS but unfortunately now I'm getting an error on the server console which I'm pretty sure is related to MAXPLAYERS where the console is telling me over and over again that "Client Index 25 is invalid".
Ravrob is offline
Balimbanana
Member
Join Date: Jan 2017
Old 07-08-2020 , 00:51   Re: Random Player Help
Reply With Quote #8

For that part, you need to check IsValidEntity(i) before the other client checks:
Code:
stock Action GetRandomPlayer()
{
	int clients[MAXPLAYERS+1];
	int clientCount;
	for (int i = 1; i <= MaxClients; i++)
	{
		if (IsValidEntity(i))
		{
			if (IsClientInGame(i) && IsPlayerAlive(i))
			{
				clients[clientCount++] = i;
				
				clients[GetRandomInt(0, clientCount + -1)];
			}
		}
	}
	
	return (clientCount == 0) ? -1 : clients[GetRandomInt(0, clientCount-1)];
}
Balimbanana is offline
Ravrob
Junior Member
Join Date: Oct 2019
Location: The Ice
Old 07-08-2020 , 01:14   Re: Random Player Help
Reply With Quote #9

I figured out that error was because I was using MAXPLAYERS in the for statement rather than MaxClients, oops! The last thing I'm not so sure about is how to call the function, could I just do something like this?
Code:
int ranplayer = GetRandomPlayer();
if(blah blah)
{
do stuff with ranplayer
}
Ravrob is offline
PC Gamer
Veteran Member
Join Date: Mar 2014
Old 07-08-2020 , 01:25   Re: Random Player Help
Reply With Quote #10

I went flashy with my TF2 random player selection:


Here's the core random selection part:
PHP Code:
{
    new 
iClients[MaxClients+1], iNumClients;
    for(new 
i=1i<=MaxClientsi++)
    {
        if(
IsClientInGame(i) && IsPlayerAlive(i) && !IsFakeClient(i) && GetClientTeam(i) > 0)
        {
            
iClients[iNumClients++] = i;
        }
    }
    new 
iRandomClient iClients[GetRandomInt(0iNumClients-1)];

    
PrintToChatAll("Fate Chose:  %N",iRandomClient);
    
PrintToServer("Fate Chose:  %N",iRandomClient);

Something I did that you may want to build into your plugin.... If I use sm_randomplayer it will find a random human player among those that are ingame and alive. If I use sm_randomplayer <target> it appears as though the command is random but the target I selected is chosen. Useful for those times when you only want to give the appearance of random.
PC Gamer is offline
Reply


Thread Tools
Display Modes

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 22:31.


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