Problem with my code.. Maybe somebody knows better
#6

iam sorry to say, but your loops are only looking excellent, as long you intend to crash/cause major lag in your server:
Код:
for(new j=0; i<MAX_PLAYERS; j++)
{
	for(new i=0; i<MAX_PLAYERS; i++)
	{
		
	}
	siren[playerid] = 0; //playerid didnt change in the whole callback, but its called 499 times too often, its in a loop.
}
thats the power of MAX_PLAYERS, its a bad idea to nest 2 loops this way, its raising up too fast (MAX_PLAYERSІ)
for 1 player, 1І=1.
for 10 players, 10І=100.
for 100 players, 100І=10,000.
for 500 players, 500І=250,000.
the only way to fix that: get rid of the double MAX_PLAYERS loop.
ah theres a 10 ms timer? no way ^^ even OnPlayerUpdate() wont run that fast, its ms is 1000/FPS per player, like if you play @ 60 fps, then its 1000/60 = 100/6 = 16.666 ms then. if your game doesnt lag at all.
so, my suggestion: rewrite that thing from scratch, with a few optimizations:

loop1: collect all connected players into an array (size MAX_PLAYERS indeed),
loop2: use that array (playerid's connected) to estimate the distance for each player (checking for if that player should/-n't hear the sound), and assign their id to another double-array (like Listen[] and ShutUp[]), this will sort them aswell)
loop3a: players in the ListenID[] array gets played a sound, and...
loop3b: the ShutUpID[] will get the sound stopped.

even if my suggestion is not representing your scripts result (or "behavior"?), its easy to modify it. you will have access too all playerid's close/away to/from you, and since you will use global variables to be shared by more callbacks, its not neccesary to check for them being connected in more loops. in the case a player disconnects after his ID got colleced, and then the ID gets used by the timer, it will simply not do anything to the player. the audio plugin wont crash i hope ^^

ok, my fingers starts bleeding soon, so i will give you a simple example from scratch..
script initialisation:
Код:
new CheckPlayers;
new CheckPlayerID[MAX_PLAYERS];
new ListenPlayers;
new ListenID[MAX_PLAYERS];
new ShutUpPlayers;
new ShutUpID[MAX_PLAYERS];
Код:
forward Siren(playerid);public Siren(playerid)
{
	CheckPlayers=0;
	new MaxPlayers=GetMaxPlayers();
	new Float:X[2],Float:Y[2],Float:Z[2];
	GetPlayerPos(playerid,X[0],Y[0],Z[0];
	for(new ID=0;ID<MaxPlayers;ID++)//loop1: collect all connected players
	{
		if(IsPlayerConnected(ID) && !IsPlayerNPC(ID))
		{
			CheckPlayerID[CheckPlayers]=ID;
			CheckPlayers++;
		}
	}
	//the CheckPlayerID array is huge (500), but i doubt that its filled -> saving a lot of time later...

	ListenPlayers=0;
	ShutUpPlayers=0;
	//                          v pay attention here, the loop is already shrinking!
	for(new ID=0;ID<CheckPlayers;ID++)//loop2: estimate the distance
	{
		if(IsPlayerInRangeOfPoint(CheckPlayerID[ID],Range,YourX,YourY,YourZ)==1)
		{
			ListenID[ListenPlayers]=CheckPlayerID[ID];
			ListenPlayers++;
		}
		else
		{
			ShutUpID[ShutUpPlayers]=CheckPlayerID[ID];
			ShutUpPlayers++;
		}
	}
	//now you got 2 arrays, for those players who will hear the sound and the others.
	
	//since the 2 arrays for Listen And ShutUp is filled with players which already are checked for being in range, theres nothing more required. simply loop through them and play the sound.
	for(new ID=0;ID<ListenPlayers;ID++)//loop3a: ListenID[] array gets played
	{
		//play sound(ListenID[ID]);
	}
	for(new ID=0;ID<ShutUpPlayers;ID++)//loop3b: ShutUpID[] array gets stopped indeed ^^
	{
		//stop sound(ShutUpID[ID]);
	}
	return 1;
}
btw i forgot to include your Siren[playerid] thing, and sadly idk how to use the audio plugin yet..
anyways, u know what i want to tell you, and this script will run a bit faster maybe, and its the solution for one player only, you will need to improve it a lot.

edit: ah the timer is 100ms, nvm me /crying about that, iam using a short timer aswell...
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 6 Guest(s)