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

Well I don't know what's the problem with my code.
If I'm the only one on my server, I hear the audio file which I suppose to hear (Audio Plugin).
As soon as somebody else connects to my server, I can't hear anything.
This is my function which I call on my cmd:
Код:
forward Siren(playerid);
public Siren(playerid)
{
        new Float: pPos[3][2];
        GetPlayerPos(playerid, pPos[0][0], pPos[1][0], pPos[2][0]);
    	for(new i=0; i<MAX_PLAYERS; i++)
    	{
    	    if(IsPlayerConnected(i) && !IsPlayerNPC(i))
    	    {
					if(siren[playerid] == 0)
					{
    	                //GetPlayerPos(i, pPos[0][1], pPos[1][1], pPos[2][1]);
    	                pPos[0][1] = -1935.770019;
    	                pPos[1][1] = 228.789993;
    	                pPos[2][1] = 34.156250;
            		    soundID_siren[i] = Audio_Play(i, 1, false, true, false);
            		    //Audio_SetVolume(i, soundID_siren[i], 0);
            		    siren[playerid] = 1;
            		    SendClientMessage(i,0xFFFFFFAA,"Siren started volumu 0");
            		    
            		}
            		else if(siren[playerid] == 1)
					{
                        for(new j=0; j<MAX_PLAYERS; j++)
    	                {
                            if(IsPlayerConnected(j) && !IsPlayerNPC(j))
    	                    {
						        Audio_Stop(j,soundID_siren[j]);
						        SendClientMessage(j,0xFFFFFFAA,"Siren stopped for you");
							}
						}
						siren[playerid] = 0;
                    }
    	    }
    	}
    	return 1;
}
And this is a timer which getting called each 0.1 seconds:
Код:
forward AudioUpdate();
public AudioUpdate()
{
    new Float: pPos[3][2];
    new string[256];
    for(new j=0; j<MAX_PLAYERS; j++)
    {
		if(siren[j] == 1) // check if soembody has his siren On
		{
            pPos[0][0] = -1935.770019;
            pPos[1][0] = 228.789993;
            pPos[2][0] = 34.156250;
            //GetPlayerPos(j, pPos[0][0], pPos[1][0], pPos[2][0]);
            for(new i=0; i<MAX_PLAYERS; i++)//
            {
        	    if(IsPlayerConnected(i) && !IsPlayerNPC(i))
        	    {
        	        if(IsPlayerInRangeOfPoint(i, 100.0, pPos[0][0], pPos[1][0], pPos[2][0]))
        	        {
        	            GetPlayerPos(i, pPos[0][1], pPos[1][1], pPos[2][1]);
        	            new distance = 100 - floatround(floatsqroot(floatpower(floatabs(pPos[0][1] - pPos[0][0]), 2) + floatpower(floatabs(pPos[1][1] - pPos[1][0]), 2) + floatpower(floatabs(pPos[2][1] - pPos[2][0]), 2)), floatround_ceil);
        	            format(string, sizeof(string), "Distance: %d", distance);
        	            SendClientMessage(i,0xFFFFFFAA,string);
        				Audio_SetVolume(i, soundID_siren[i], distance);
        				Audio_Resume(i, soundID_siren[i]);
        				return 1;
        	        }
        	        else
        	        {
                       Audio_SetVolume(i, soundID_siren[i], 0);
                       Audio_Pause(i, soundID_siren[i]);
                       return 1;
        	        }
        	    }
        	}
		}
    }
    return 1;//the value what function returning
}
If there is another player in my server, it prints automaticly after "Siren started volume 0", "Siren stopped for you".

I posted it here because I don't think the problem is with the plugin.
Does anybody knows what's the problem here?
Reply
#2

Second page bump.. anyone?
Reply
#3

well first of all you dont need the two for loops you can just use one.,and the playerid param is being called using the for loop.... mabey thats your problem try this....



First script:

pawn Код:
forward Siren();
public Siren()
{
        new Float: pPos[3][2];
        GetPlayerPos(playerid, pPos[0][0], pPos[1][0], pPos[2][0]);
        for(new i=0; i<MAX_PLAYERS; i++)
        {
            if(IsPlayerConnected(i) && !IsPlayerNPC(i))
            {
                    if(siren[playerid] == 0)
                    {
                        //GetPlayerPos(i, pPos[0][1], pPos[1][1], pPos[2][1]);
                        pPos[0][1] = -1935.770019;
                        pPos[1][1] = 228.789993;
                        pPos[2][1] = 34.156250;
                        soundID_siren[i] = Audio_Play(i, 1, false, true, false);
                        //Audio_SetVolume(i, soundID_siren[i], 0);
                        siren[playerid] = 1;
                        SendClientMessage(i,0xFFFFFFAA,"Siren started volumu 0");
                       
                    }
                    else if(siren[playerid] == 1)
                    {
                            if(IsPlayerConnected(j) && !IsPlayerNPC(j))
                            {
                                Audio_Stop(j,soundID_siren[j]);
                                SendClientMessage(j,0xFFFFFFAA,"Siren stopped for you");
                            }
                        }
                        siren[playerid] = 0;
            }
        }
        return 1;
}
Reply
#4

Well, I do need two loops. The first one is to set the player's audio status to true, and the second one is an auto 0.1 sec timer witch checks if any player should hear anything. The second timer get called every 0.1 seconds, while the first one is a normal function.
//Even if you are right, your code is not working, there is no "playerid" in that public.
Reply
#5

Third page.. Bump again.
Anyone? I really need this to be fixed, would you take a look at the code please?
Reply
#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
#7

I will try using your code, but take a look at that:
Siren Public: (getting called on each /siren cmd
Код:
public Siren(playerid)
{
        new Float: pPos[3][2];
        GetPlayerPos(playerid, pPos[0][0], pPos[1][0], pPos[2][0]);
        pPos[0][1] = -1935.770019;
    	pPos[1][1] = 228.789993;
    	pPos[2][1] = 34.156250;
    	new tempSiren = 0;
    	if(siren[playerid] == 0)
		{
    	    for(new i=0; i<MAX_PLAYERS; i++)
    	    {
        	    if(IsPlayerConnected(i) && !IsPlayerNPC(i))
        	    {
   	                //GetPlayerPos(i, pPos[0][1], pPos[1][1], pPos[2][1]);
           		    soundID_siren[i] = Audio_Play(i, 1, false, true, false);
           		    //Audio_SetVolume(i, soundID_siren[i], 0);
           		    SendClientMessage(i,0xFFFFFFAA,"Siren started volumu 0");
                }
		    }
		    tempSiren = 1;
		    //return 1;
		}
        else if(siren[playerid] == 1)
		{
		    for(new i=0; i<MAX_PLAYERS; i++)
    	    {
        	    if(IsPlayerConnected(i) && !IsPlayerNPC(i))
        	    {
   	                Audio_Stop(i,soundID_siren[i]);
					SendClientMessage(i,0xFFFFFFAA,"Siren stopped for you");
                }
		    }
		    tempSiren = 0;
		    //return 1;
		}
		siren[playerid] = tempSiren;
    	return 1;
}
The next public getting called each 0.1 seconds. It should set the level of each audio(for now there is only one audio(audioID_Siren), but there should be more.
Код:
public AudioUpdate()
{
    new Float: pPos[3][2];
    new string[256];
    for(new j=0; j<MAX_PLAYERS; j++)
    {
		if(siren[j] == 1) // check if soembody has his siren On
		{
            pPos[0][0] = -1935.770019;
            pPos[1][0] = 228.789993;
            pPos[2][0] = 34.156250;
            //GetPlayerPos(j, pPos[0][0], pPos[1][0], pPos[2][0]);
            for(new i=0; i<MAX_PLAYERS; i++)//
            {
        	    if(IsPlayerConnected(i) && !IsPlayerNPC(i))
        	    {
        	        if(IsPlayerInRangeOfPoint(i, 100.0, pPos[0][0], pPos[1][0], pPos[2][0]))
        	        {
        	            GetPlayerPos(i, pPos[0][1], pPos[1][1], pPos[2][1]);
        	            new distance = 100 - floatround(floatsqroot(floatpower(floatabs(pPos[0][1] - pPos[0][0]), 2) + floatpower(floatabs(pPos[1][1] - pPos[1][0]), 2) + floatpower(floatabs(pPos[2][1] - pPos[2][0]), 2)), floatround_ceil);
        	            format(string, sizeof(string), "Distance: %d", distance);
        	            SendClientMessage(i,0xFFFFFFAA,string);
        				Audio_SetVolume(i, soundID_siren[i], distance);
        				//Audio_Resume(i, soundID_siren[i]);
        				//return 1;
        	        }
        	        else
        	        {
                       Audio_SetVolume(i, soundID_siren[i], 0);
                       //Audio_Pause(i, soundID_siren[i]);
                       //return 1;
        	        }
        	    }
        	}
		}
    }
    return 1;//the value what function returning
}
Maybe you misunderstood what should the code do.
The siren should be heard in the positions:
Код:
pPos[0][0] = -1935.770019;
            pPos[1][0] = 228.789993;
            pPos[2][0] = 34.156250;
siren[playerid] = 0; On player connect.
Reply
#8

Edit: I've just tested your code, and I don't get soemthing
I've modefided it alittle bit, and that's what I got:
Код:
forward AudioUpdate();public AudioUpdate()
{
	CheckPlayers=0;
	new string[256];
	new MaxPlayers=GetMaxPlayers();
	new Float:X[2],Float:Y[2],Float:Z[2];
	//GetPlayerPos(playerid,X[0],Y[0],Z[0];
	X[0] = -1935.770019;
    Y[0] = 228.789993;
    Z[0] = 34.156250;
	for(new ID=0;ID<MaxPlayers;ID++)//loop1: collect all connected players
	{
		if(IsPlayerConnected(ID) && !IsPlayerNPC(ID))
		{
			CheckPlayerID[CheckPlayers]=ID;
			CheckPlayers++;
		}
	}
	ListenPlayers=0;
	ShutUpPlayers=0;
	for(new ID=0;ID<CheckPlayers;ID++)//loop2: estimate the distance
	{
		if(IsPlayerInRangeOfPoint(CheckPlayerID[ID],100.0,X[0],Y[0],Z[0])==1)
		{
			ListenID[ListenPlayers]=CheckPlayerID[ID];
			ListenPlayers++;
		}
		else
		{
			ShutUpID[ShutUpPlayers]=CheckPlayerID[ID];
			ShutUpPlayers++;
		}
	}
	for(new ID=0;ID<ListenPlayers;ID++)//loop3a: ListenID[] array gets played
	{
		                GetPlayerPos(ID,X[1],Y[1],Z[1]);
        	            new distance = 100 - floatround(floatsqroot(floatpower(floatabs(X[1] - X[0]), 2) + floatpower(floatabs(Y[1] - Y[0]), 2) + floatpower(floatabs(Z[1] - Z[0]), 2)), floatround_ceil);
        	            format(string, sizeof(string), "Distance: %d", distance);
        	            SendClientMessage(ID,0xFFFFFFAA,string);
        				Audio_SetVolume(ID, soundID_siren[ID], distance);
	}
	for(new ID=0;ID<ShutUpPlayers;ID++)//loop3b: ShutUpID[] array gets stopped indeed ^^
	{
		Audio_SetVolume(ID, soundID_siren[ID], 0);
	}
	return 1;
}
How can I check if any player turned on his siren? (Siren Public whitch changes the siren[playerid] to 0/1)?

I don't understand how should I use it.


Edit: I tried to use this function which checks if any player has his siren on, still doesn't work:
Код:
forward AudioUpdate();public AudioUpdate()
{
	CheckPlayers=0;
	new string[256];
	new MaxPlayers=GetMaxPlayers();
	new Float:X[2],Float:Y[2],Float:Z[2];
	for(new ID=0;ID<MaxPlayers;ID++)//loop1: collect all connected players
	{
		if(IsPlayerConnected(ID) && !IsPlayerNPC(ID))
		{
			CheckPlayerID[CheckPlayers]=ID;
			CheckPlayers++;
		}
	}
	for(new tID=0;tID<CheckPlayers;tID++)
	{
		if(siren[CheckPlayerID[tID]] == 1)
		{
        	//GetPlayerPos(playerid,X[0],Y[0],Z[0];
        	X[0] = -1935.770019;
            Y[0] = 228.789993;
            Z[0] = 34.156250;
            
            ListenPlayers=0;
        	ShutUpPlayers=0;
        	for(new ID=0;ID<CheckPlayers;ID++)//loop2: estimate the distance
        	{
        		if(IsPlayerInRangeOfPoint(CheckPlayerID[ID],100.0,X[0],Y[0],Z[0])==1)
        		{
        			ListenID[ListenPlayers]=CheckPlayerID[ID];
        			ListenPlayers++;
        		}
        		else
        		{
        			ShutUpID[ShutUpPlayers]=CheckPlayerID[ID];
        			ShutUpPlayers++;
        		}
        	}
        	for(new ID=0;ID<ListenPlayers;ID++)//loop3a: ListenID[] array gets played
        	{
        		                GetPlayerPos(ID,X[1],Y[1],Z[1]);
                	            new distance = 100 - floatround(floatsqroot(floatpower(floatabs(X[1] - X[0]), 2) + floatpower(floatabs(Y[1] - Y[0]), 2) + floatpower(floatabs(Z[1] - Z[0]), 2)), floatround_ceil);
                	            format(string, sizeof(string), "Distance: %d", distance);
                	            SendClientMessage(ID,0xFFFFFFAA,string);
                				Audio_SetVolume(ID, soundID_siren[ID], distance);
        	}
        	for(new ID=0;ID<ShutUpPlayers;ID++)//loop3b: ShutUpID[] array gets stopped indeed ^^
        	{
        		Audio_SetVolume(ID, soundID_siren[ID], 0);
        	}
		}
	}
	///////////
	
	////////////////
	return 1;
}

Anyone?
Reply


Forum Jump:


Users browsing this thread: 6 Guest(s)