[HELP] - Total players in the teams
#1

Good morning guys, Today I made a command that shows the total number of players connected in teams, and each time the command is entered the number is added.

Example: Team 1 is with 2 players online when you enter the command it seems that the team is with 2 players online, plus if you enter the command again the result get 3 players and so on.

Code:
dcmd_totalteams(playerid, params[])
{
	#pragma unused params

	GetteamPlayers();

	new String[500];
	format(String, sizeof(String), "{FFFFFF}Players team 1:%d\n\
	{FFFFFF}Players team 2: %d", Totalteam1, Totalteam2);
	ShowPlayerDialog(playerid, 96251, DIALOG_STYLE_MSGBOX, "Total Players dos teams", String, "Ok", "");
	return 1;
}

stock GetteamPlayers()
{
    for(new i, g = GetMaxPlayers(); i < g; i++)
	{
		if(IsPlayerConnected(i))
		{
			if(Player[i][pteam1] == 1)
			{
				Totalteam1++;
				return 1;
			}
			if(Player[i][pteam2] == 1)
			{
				Totalteam2++;
				return 1;
			}
		}
	}
	return 1;
}
Reply
#2

Quote:
Originally Posted by Ralfie
View Post
No, this is not how it works. There are basically two ways for a function to return; either by value or by reference. You can ****** the detailed difference, but to return more than one value, the function must return them by reference; meaning that whatever variables passed to this function are modified internally, they are modified where you called it and passed them too.

A fixed code of yours may look like:
Code:
dcmd_totalteams(playerid, params[])
{
	#pragma unused params
	new 
		Totalteam1,
		Totalteam2
	;
	GetteamPlayers(Totalteam1, Totalteam2);

	new String[500];
	format(String, sizeof(String), 
	"{FFFFFF}Players team 1:%d\n\
	{FFFFFF}Players team 2: %d", 
	Totalteam1, Totalteam2);

	ShowPlayerDialog(playerid, 96251, DIALOG_STYLE_MSGBOX, "Total Players dos teams", String, "Ok", "");
	return 1;
}

GetteamPlayers(Totalteam1, Totalteam2)
{
    for (new i, g = GetMaxPlayers(); i < g; i++)
	{
		if (IsPlayerConnected(i))
		{
			if (Player[i][pteam1] == 1)
				Totalteam1++;

			if (Player[i][pteam2] == 1)
				Totalteam2++;
		}
	}
}
Not to mention that you are storing player teams in a wrong way. I would recommend having named teams (defined as macros) and having a unified variable to store player teams.
That would be something like:

Code:
#define NoTeam  (-1)
#define Team1 	(0)
#define Team2 	(1)
#define Team3 	(2)

new
	PlayerTeam[MAX_PLAYERS];

dcmd_totalteams(playerid, params[])
{
	#pragma unused params
	new 
		Totalteam1,
		Totalteam2
	;
	GetteamPlayers(Totalteam1, Totalteam2);

	new String[500];
	format(String, sizeof(String), 
	"{FFFFFF}Players team 1:%d\n\
	{FFFFFF}Players team 2: %d", 
	Totalteam1, Totalteam2);

	ShowPlayerDialog(playerid, 96251, DIALOG_STYLE_MSGBOX, "Total Players dos teams", String, "Ok", "");
	return 1;
}

GetteamPlayers(Totalteam1, Totalteam2)
{
    for (new i, g = GetMaxPlayers(); i < g; i++)
	{
		if (!IsPlayerConnected(i))
			continue;

		switch (PlayerTeam[i])
		{
			case Team1:
				Totalteam1++;
			
			case Team2:
				Totalteam2++;	

			// ...					
		}
	}
}
Setting a player team would simply be: PlayerTeam[playerid] = Team1;
Resetting would be: PlayerTeam[playerid] = NoTeam;

I used all the Codes and did not work and also I set the player with the variable pteam1 and PlayerTeam and it did not work

Result: http://i.imgur.com/FllXcYz.png
Reply
#3

Someone to help?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)