SA-MP Forums Archive
define/variable - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: define/variable (/showthread.php?tid=155046)



define/variable - DowNlOaD_ - 16.06.2010

how can i make define to return variable?


Re: define/variable - (SF)Noobanatior - 16.06.2010

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


Re: define/variable - Kyosaur - 16.06.2010

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


Re: define/variable - DowNlOaD_ - 16.06.2010

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


Re: define/variable - (SF)Noobanatior - 16.06.2010

can i see the PlayersOnline() function


Re: define/variable - DowNlOaD_ - 16.06.2010

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

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

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


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


Re: define/variable - (SF)Noobanatior - 16.06.2010

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


Re: define/variable - Kyosaur - 16.06.2010

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.


Re: define/variable - DowNlOaD_ - 16.06.2010

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


Re: define/variable - Kyosaur - 16.06.2010

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.