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

Solved Problem with some kind of "variables disaster"?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
NWayne
Junior Member
Join Date: Aug 2017
Location: Warsaw, Europe
Old 11-14-2018 , 16:30   Problem with some kind of "variables disaster"?
Reply With Quote #1

Hello Guys,

I've got some kind of problem with my stock, but i cannot understand what and where is problem?
I am glad if you help me <3

Code:
stock int GetRandomPlayerFromTT()
{
    int clients[MaxClients + 1];
    int clientCount;
    for (int i = 1; i <= MaxClients; i++)
    	if (IsClientInGame(i) && (GetClientTeam(i) == CS_TEAM_T))
      	  clients[clientCount++] = i;
    return (clientCount == 0) ? -1 : clients[GetRandomInt(0, clientCount - 1)];
}
Quote:
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?

Last edited by NWayne; 11-14-2018 at 16:41.
NWayne is offline
backwards
AlliedModders Donor
Join Date: Feb 2014
Location: USA
Old 11-14-2018 , 16:34   Re: Problem with some kind of "variables disaster"?
Reply With Quote #2

You have to replace "MaxClients" with "MAXPLAYERS". MaxClients is a variable that isn't defined until a server is booted up. MAXPLAYERS is predefined as 65 or 64. This way it has a static value that isn't changing. Since the compiler compiles code before its loaded it can't have a dynamic value define the array size.
__________________
I highly recommend joining the SourceMod Discord Server for real time support.
backwards is offline
NWayne
Junior Member
Join Date: Aug 2017
Location: Warsaw, Europe
Old 11-14-2018 , 16:41   Re: Problem with some kind of "variables disaster"?
Reply With Quote #3

Thank you the topic is solved.
NWayne is offline
xerox8521
Senior Member
Join Date: Sep 2011
Old 11-14-2018 , 19:05   Re: Problem with some kind of "variables disaster"?
Reply With Quote #4

It is actually possible to declare variables with a dynamic size as of 1.7

https://wiki.alliedmods.net/Introduc...#Declaration_2
xerox8521 is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 11-15-2018 , 01:39   Re: Problem with some kind of "variables disaster"?
Reply With Quote #5

Quote:
Originally Posted by xerox8521 View Post
It is actually possible to declare variables with a dynamic size as of 1.7

https://wiki.alliedmods.net/Introduc...#Declaration_2
You could create dynamic arrays before 1.7. The old syntax for doing so was replaced in 1.7 and made an error (the one in the original post).
Fyren is offline
ThatOneGuy
Veteran Member
Join Date: Jul 2012
Location: Oregon, USA
Old 11-15-2018 , 03:43   Re: Problem with some kind of "variables disaster"?
Reply With Quote #6

I'd suggest using an ADT array ("arraylist" in new syntax) for this. Push the client user ID's to an array, get the size of the array, then get a random int from 0 to (size - 1). Convert the user IDs back to client index.
__________________
ThatOneGuy is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 11-15-2018 , 04:31   Re: Problem with some kind of "variables disaster"?
Reply With Quote #7

Quote:
Originally Posted by ThatOneGuy View Post
I'd suggest using an ADT array ("arraylist" in new syntax) for this. Push the client user ID's to an array, get the size of the array, then get a random int from 0 to (size - 1). Convert the user IDs back to client index.
Terrible idea to sell out up to 31 cells and buy lag.
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334
eyal282 is offline
xerox8521
Senior Member
Join Date: Sep 2011
Old 11-15-2018 , 08:47   Re: Problem with some kind of "variables disaster"?
Reply With Quote #8

Quote:
Originally Posted by Fyren View Post
You could create dynamic arrays before 1.7. The old syntax for doing so was replaced in 1.7 and made an error (the one in the original post).
Didn't know about that. But we are not talking about CreateArray right ?
xerox8521 is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 11-15-2018 , 15:02   Re: Problem with some kind of "variables disaster"?
Reply With Quote #9

Quote:
Originally Posted by xerox8521 View Post
Didn't know about that. But we are not talking about CreateArray right ?
I am not.
Fyren 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 10:00.


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