PDA

View Full Version : Players Alive counter doesn't go down OnPlayerDisconnect


Andre02
17/08/2015, 03:26 AM
Hello, with my project partner we have been working on an alive counter, i've been working on it since 02:19 and it's already 05:23
We are doing the Race DM mode of our Multi Mode
and we have been the players alive counter go down and up when a player dies, spawns, is spectating, gets in a vehicle etc...
There was many bugs, and made it to fix all of them (yay)
The problem comes when the player disconnects, i want to make the counter go down when a player on the Race DM mode disconnects, the problem is that it doesn't, it stays at the same place!

There is no errors nor warnings when i compile and everything seems to be fine, so i came here asking for help ^^

Here is my code:

public OnPlayerDisconnect(playerid, reason)
{
// Login Register system, and leaving messages here!
// From here is the alive counter!
new alive[3];
for(new i = 0; i < MAX_PLAYERS; i++)
{
if (pInfo[playerid][DM] && pInfo[i][DM] == 1)
{
SetPlayerHealth(playerid, 0);
AlivePlayers-=1;
if (AlivePlayers < 10)
{
format(alive, sizeof(alive), "0%i", AlivePlayers);
}
else if (AlivePlayers > 9)
{
format(alive, sizeof(alive), "%i", AlivePlayers);
}
TextDrawSetString(numberal, alive);
TextDrawShowForPlayer(i, albox1);
TextDrawShowForPlayer(i, albox2);
TextDrawShowForPlayer(i, Alive);
TextDrawShowForPlayer(i, numberal);
}
}
return 1;
}

UnDetectable
17/08/2015, 03:55 AM
I don't really get it but I doubt that you have created
pInfo[playerid][DM] = 0;
in OnPlayerDisconnect before your timer

That means when the player disconnects, the DM info sets to 0
before your alive counter starts

So when the counter checks the player, it won't work because you are checking if the DM info sets to 1
pInfo[playerid][DM] && pInfo[i][DM] == 1
Which is not

Just guessing xd

And btw, why you are setting the health of the player to 0 when he disconnects? xD

Andre02
17/08/2015, 04:30 AM
I don't really get it but I doubt that you have created
pInfo[playerid][DM] = 0;
in OnPlayerDisconnect before your timer

That means when the player disconnects, the DM info sets to 0
before your alive counter starts

So when the counter checks the player, it won't work because you are checking if the DM info sets to 1
pInfo[playerid][DM] && pInfo[i][DM] == 1
Which is not

Just guessing xd

And btw, why you are setting the health of the player to 0 when he disconnects? xD

pInfo[playerid][DM] = 1; is used to check if the player is in the DM mode or not when the player leave it automatically sets it to 0

and i'm setting the hp to 0
cuz when the player dies it removes 1 from the alive counter, so i thought that would work, but it didn't, and i forgot to remove it!

[KHK]Khalid
17/08/2015, 06:07 AM
Check this!


/*
~Changelog~

- You had everything done inside the loop which caused some problems. Now everything related to 'playerid' (who disconnects) is done outside the loop.
- After moving code outside the for loop, I realized that the loop was only used for those TextDrawShowForPlayer which I don't think you need at all.
So I completely deleted the loop code because it wasn't needed at all. You're just wasting RPCs. Just show the textdraws once they enter DM mode.
- Fixed string size and used some trick instead of that if/elseif statement.
- Removed SetPlayerHealth because it wasn't needed at all.

*/

public OnPlayerDisconnect(playerid, reason)
{
if(pInfo[playerid][DM]) // If this player is in DM mode
{
new alive[4]; // Size needs to be 4 not 3. You probably forgot to count the null terminator: '\0'
AlivePlayers -=1; // Decrease the alive players count
/*
the '%02d' trick will do what you wanted to do with those if/elseif statements
i.e. 01, 02, 03, ... 10, 11 ...
*/
format(alive, sizeof(alive), "%02d", AlivePlayers);
TextDrawSetString(numberal, alive); // This will update the textdraw for whoever it was shown to, therefore you don't need to keep showing it over and over.
}
return 1;
}

Andre02
17/08/2015, 06:17 AM
@Khalid

That didn't work, it still doesn't lower it :/

MarvinPWN
17/08/2015, 08:13 AM
Please, try this:
public OnPlayerDisconnect(playerid,reason)
{
// Login Register system, and leaving messages here!
// From here is the alive counter!
new alive[3];
if(pInfo[playerid][DM] > 0)
{
AlivePlayers --;
if(AlivePlayers < 10)format(alive,sizeof alive,"0%i",AlivePlayers);
else if(AlivePlayers > 9)format(alive,sizeof alive,"%i",AlivePlayers);
TextDrawSetString(numberal,alive);
}
return 1;
}
Be sure that the textdraws are showing before you disconnected. It can only set the string when it is showing. ;)

Beckett
17/08/2015, 01:10 PM
Perhaps your textdraws are bugged, have you tried debugging it?


public OnPlayerDisconnect(playerid, reason)
{
new alive[3];
for(new i = 0, j = GetPlayerPoolSize(); i <= j; i++)
{
printf("I: %i",i);
if (pInfo[playerid][DM] && pInfo[i][DM] == 1)
{
printf("[playerid][DM]: %i || [i][DM]: %i || Alive Players: %i",pInfo[playerid][DM],pInfo[i][DM],AlivePlayers);
AlivePlayers -= 1;
printf("AlivePlayers - 1: %i",AlivePlayers);
format(alive, sizeof(alive), "%02d", AlivePlayers);
TextDrawSetString(numberal, alive);
TextDrawShowForPlayer(i, albox1);
TextDrawShowForPlayer(i, albox2);
TextDrawShowForPlayer(i, Alive);
TextDrawShowForPlayer(i, numberal);
}
}
printf("Highest Player ID: %i",j);
return 1;
}


Post the results please.

Please, try this:

Be sure that the textdraws are showing before you disconnected. It can only set the string when it is showing. ;)

That's irrelevant, he's changing the TextDraw String for the other player, not the disconnected player.

PS: You don't need to Hide/Show the TextDraw when you set it's text.

Andre02
22/08/2015, 08:53 PM
Im sorry for the big delay, i've been abit busy this days
I've done what you sent me @Danice
and here are the results according to the code you gave me:

[22:47:24] [chat] [Andre]: u want to leave or i do ?
[22:47:42] I: 0
[22:47:42] Highest Player ID: 1
[22:47:42] I: 1
[22:47:42] Highest Player ID: 1
[22:47:42] [part] Jonny has left the server (1:1)


and it still doesn't remove 1 when the player leaves

also it's weird because it didn't print the:
printf("[playerid][DM]: %i || [i][DM]: %i || Alive Players: %i",pInfo[playerid][DM],pInfo[i][DM],AlivePlayers);
nor
printf("AlivePlayers - 1: %i",AlivePlayers);

but
printf("I: %i",i);
printf("Highest Player ID: %i",j);

twice

here is my code:

new alive[3];
for(new i = 0, j = GetPlayerPoolSize(); i <= j; i++)
{
printf("I: %i",i);
if (pInfo[playerid][DM] && pInfo[i][DM] == 1)
{
printf("[playerid][DM]: %i || [i][DM]: %i || Alive Players: %i",pInfo[playerid][DM],pInfo[i][DM],AlivePlayers);
AlivePlayers -= 1;
printf("AlivePlayers - 1: %i",AlivePlayers);
format(alive, sizeof(alive), "%02d", AlivePlayers);
TextDrawSetString(numberal, alive);
TextDrawShowForPlayer(i, albox1);
TextDrawShowForPlayer(i, albox2);
TextDrawShowForPlayer(i, Alive);
TextDrawShowForPlayer(i, numberal);
}
printf("Highest Player ID: %i",j);
}
return 1;
}

Roberto80
22/08/2015, 10:39 PM
Try
AlivePlayers--;
Replace this with it
AlivePlayers-=1;

Andre02
22/08/2015, 10:51 PM
Try
AlivePlayers--;
Replace this with it
AlivePlayers-=1;

That is exactly the same thing..

Jefff
22/08/2015, 11:42 PM
This must work, if not, problem is not in OPD
public OnPlayerDisconnect(playerid, reason)
{
if(pInfo[playerid][DM] == 1)
{
new alive[3];
--AlivePlayers;
valstr(str,AlivePlayers);
pInfo[playerid][DM] = 0;
TextDrawSetString(numberal, alive);

for(new i = GetPlayerPoolSize(); i >= 0; i--)
if(pInfo[i][DM] == 1)
{
TextDrawShowForPlayer(i, albox1);
TextDrawShowForPlayer(i, albox2);
TextDrawShowForPlayer(i, Alive);
TextDrawShowForPlayer(i, numberal);
}
}
return 1;
}

Andre02
23/08/2015, 02:14 PM
This must work, if not, problem is not in OPD
public OnPlayerDisconnect(playerid, reason)
{
if(pInfo[playerid][DM] == 1)
{
new alive[3];
--AlivePlayers;
valstr(str,AlivePlayers);
pInfo[playerid][DM] = 0;
TextDrawSetString(numberal, alive);

for(new i = GetPlayerPoolSize(); i >= 0; i--)
if(pInfo[i][DM] == 1)
{
TextDrawShowForPlayer(i, albox1);
TextDrawShowForPlayer(i, albox2);
TextDrawShowForPlayer(i, Alive);
TextDrawShowForPlayer(i, numberal);
}
}
return 1;
}

It didn't work ;/
I've also made a printf before and after the code, to check the alive players before and after he leaves, but it didn't even print anything :/

Logofero
23/08/2015, 02:48 PM
forward OnPlayerDisconnectEx(playerid, reason);
public OnPlayerDisconnectEx(playerid, reason)
{
new
alive,
str[256]
;
for (new p = GetPlayerPoolSize(); p >= 0; --p) {
if (pInfo[p][DM]) alive++;
}
if (alive < 10) format(str, sizeof(str), "0%i", alive);
else if (alive > 9) format(str, sizeof(str), "%i", alive);
TextDrawSetString(numberal, str);
TextDrawShowForAll(albox1);
TextDrawShowForAll(albox2);
TextDrawShowForAll(Alive);
TextDrawShowForAll(numberal);
return true;
}

public OnPlayerDisconnect(playerid, reason)
{

if (pInfo[playerid][DM]) SetPlayerHealth(playerid, 0);

SetTimerEx("OnPlayerDisconnectEx", 100, 0, "ii", playerid, reason);
return 1;
}

Beckett
23/08/2015, 02:56 PM
Just realized you shouldn't reduce the AlivePlayers inside the loop, because that will cause it to decrease by the number of connected players, however your "IF" statement is wrong as the printf's under it are not appearing. (Assuming you didn't post them)


new alive[3];
AlivePlayers -= 1;
for(new i = 0, j = GetPlayerPoolSize(); i <= j; i++)
{
printf("I: %i",i);
printf("[playerid][DM]: %i || [i][DM]: %i ",pInfo[playerid][DM],pInfo[i][DM]);
if (pInfo[playerid][DM] && pInfo[i][DM] == 1)
{


printf("AlivePlayers - 1: %i",AlivePlayers);
format(alive, sizeof(alive), "%02d", AlivePlayers);
TextDrawSetString(numberal, alive);
TextDrawShowForPlayer(i, albox1);
TextDrawShowForPlayer(i, albox2);
TextDrawShowForPlayer(i, Alive);
TextDrawShowForPlayer(i, numberal);
}
printf("Highest Player ID: %i",j);
}


Try this out and post the results again.

Andre02
23/08/2015, 04:14 PM
Just realized you shouldn't reduce the AlivePlayers inside the loop, because that will cause it to decrease by the number of connected players, however your "IF" statement is wrong as the printf's under it are not appearing. (Assuming you didn't post them)


new alive[3];
AlivePlayers -= 1;
for(new i = 0, j = GetPlayerPoolSize(); i <= j; i++)
{
printf("I: %i",i);
printf("[playerid][DM]: %i || [i][DM]: %i ",pInfo[playerid][DM],pInfo[i][DM]);
if (pInfo[playerid][DM] && pInfo[i][DM] == 1)
{


printf("AlivePlayers - 1: %i",AlivePlayers);
format(alive, sizeof(alive), "%02d", AlivePlayers);
TextDrawSetString(numberal, alive);
TextDrawShowForPlayer(i, albox1);
TextDrawShowForPlayer(i, albox2);
TextDrawShowForPlayer(i, Alive);
TextDrawShowForPlayer(i, numberal);
}
printf("Highest Player ID: %i",j);
}


Try this out and post the results again.

It didn't remove again :/

here is the log:

[18:08:50] I: 0
[18:08:50] [playerid][DM]: 0 || [i][DM]: 0
[18:08:50] Highest Player ID: 1
[18:08:50] I: 1
[18:08:50] [playerid][DM]: 0 || [i][DM]: 1
[18:08:50] Highest Player ID: 1

Beckett
23/08/2015, 04:45 PM
It doesn't remove because the playerid DM variable does not match the opposite player's DM variable. I recommend you to delete that code, think twice and write it again.

Andre02
23/08/2015, 05:05 PM
Alright i will see, Thank you everyone for the time helping me ^^