define/variable
#1

how can i make define to return variable?
Reply
#2

?? if you define something its not a variable its a constant what are you trying to do
Reply
#3

Defines are incapable of returning anything, as they are simply text replacements.
Reply
#4

i mean something like this
pawn Код:
#define MAX_PLAYERS               PlayersOnline()
and PlayersOnline() returns the number of players on server
Reply
#5

can i see the PlayersOnline() function
Reply
#6

//top of the script:
new PlrsOnline = 0;

//on...connect:
PlrsOnline++;

//on...disconnect:
PlrsOnline--;


//Bottom of my gamemode:
PlayersOnline()
{
return PlrsOnline;
}
Reply
#7

try just using PlayersOnline() instead of MAX_PLAYERS in you functions
Reply
#8

Quote:
Originally Posted by DowNlOaD_
i mean something like this
pawn Код:
#define MAX_PLAYERS               PlayersOnline()
and PlayersOnline() returns the number of players on server
You cant really do it like that due to one thing: Arrays. Functions can not be used outside of callbacks, so if any MAX_PLAYER arrays are declared globally, it'll crash the compiler.

You could go like:

Код:
#if defined MAX_PLAYERS
	#undef MAX_PLAYERS
#endif

#define MAX_PLAYERS 150
Which would make loops a lot faster, and would cut down on ram ussage (providing you dont use pvars). But thats as far as it goes tbh.


EDIT: Ohh i see what you meant, i thought you meant redefine MAX_PLAYERS to the GetMaxPlayers() value. My fault lol.


You dont need that function btw, just use the variable itself for what ever your doing.
Reply
#9

Quote:
Originally Posted by Kyosaur!!
Quote:
Originally Posted by DowNlOaD_
i mean something like this
pawn Код:
#define MAX_PLAYERS               PlayersOnline()
and PlayersOnline() returns the number of players on server
You cant really do it like that due to one thing: Arrays. Functions can not be used outside of callbacks, so if any MAX_PLAYER arrays are declared globally, it'll crash the compiler.

You could go like:

Код:
#if defined MAX_PLAYERS
	#undef MAX_PLAYERS
#endif

#define MAX_PLAYERS 150
Which would make loops a lot faster, and would cut down on ram ussage (providing you dont use pvars). But thats as far as it goes tbh.


EDIT: Ohh i see what you meant, i thought you meant redefine MAX_PLAYERS to the GetMaxPlayers() value. My fault lol.
ok thanks everybody, and i knew about that #define MAX_PLAYERS 150

and at least PlayersOnline() works in loops

PS: when i'm doing something like that: new VAR[PlayersOnline()]; it crashes my pawn compiler
Reply
#10

Quote:
Originally Posted by DowNlOaD_
Quote:
Originally Posted by Kyosaur!!
Quote:
Originally Posted by DowNlOaD_
i mean something like this
pawn Код:
#define MAX_PLAYERS               PlayersOnline()
and PlayersOnline() returns the number of players on server
You cant really do it like that due to one thing: Arrays. Functions can not be used outside of callbacks, so if any MAX_PLAYER arrays are declared globally, it'll crash the compiler.

You could go like:

Код:
#if defined MAX_PLAYERS
	#undef MAX_PLAYERS
#endif

#define MAX_PLAYERS 150
Which would make loops a lot faster, and would cut down on ram ussage (providing you dont use pvars). But thats as far as it goes tbh.


EDIT: Ohh i see what you meant, i thought you meant redefine MAX_PLAYERS to the GetMaxPlayers() value. My fault lol.
ok thanks everybody, and i knew about that #define MAX_PLAYERS 150

and at least PlayersOnline() works in loops

PS: when i'm doing something like that: new VAR[PlayersOnline()]; it crashes my pawn compiler
You CAN NOT use your PlayersOnline for a loop, the reason being:

Lets say you HAD 50 players... 0-46 disconnect for some unkown reason. You are left with players 47,48, and 49. You're variable says there are 3 players online (which is true) but when you loop, it'll go 1..2..3 and what ever your doing for the players in that loop WILL NOT reach the connected players.

The best way for efficient looping is a redefinition of MAX_PLAYERS like the one above, but that can get to be a pain due to having to edit when ever you switch you max players value in your server.cfg

Код:
for(new i=0, p=GetMaxPlayers(); i<p; i++)
{
  //stuff
}
Код:
thats the way to do it, if your looking for the most efficient dynamic way.



OFF TOPIC:

As for your "new VAR[PlayersOnline()];" statement, i told you: You CANT use functions outside of callbacks.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)