SA-MP Forums Archive
function should return a value? But i use return.. - 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)
+--- Thread: function should return a value? But i use return.. (/showthread.php?tid=631011)



function should return a value? But i use return.. - faxxe - 21.03.2017

Ahoy friends.

Thats my function to check if there is only one team left in my gamemode.
I love to use recursive functions so thats it!

Код:
stock TeamsAlive(bool:value)
{
	new i=0,count=0;
	while(i<TEAMSIZE)
	{
		if(GetTeamCount(i) > 0)
		{
			count++;
		}
		i++;	
	}
	if(bool:value == false)
	switch(count)
	{
		case 1:
		{
			switch(TeamsAlive(true))
			{
				case FIRST_TEAM:
				{
					for(new i;i<MAX_PLAYERS;i++)
					{
						SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",FIRST_TEAM_COLOR_TAG);
					}	
				}
				case SECOND_TEAM:
				{
					for(new i;i<MAX_PLAYERS;i++)
					{
						SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",SECOND_TEAM_COLOR_TAG);
					}	
				}
				#if defined TEAMSIZE
				#if TEAMSIZE >= 3
				case THIRD_TEAM:
				{
					for(new i;i<MAX_PLAYERS;i++)
					{
						SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",THIRD_TEAM_COLOR_TAG);
					}	
				}
				#endif
				#endif
				#if defined TEAMSIZE
				#if TEAMSIZE >= 4
				case FOURTH_TEAM:
				{
					for(new i;i<MAX_PLAYERS;i++)
					{
						SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",FOURTH_TEAM_COLOR_TAG);
					}	
				}
				#endif
				#endif
				#if defined TEAMSIZE
				#if TEAMSIZE >= 5
				case FIFTH_TEAM:
				{
					for(new i;i<MAX_PLAYERS;i++)
					{
						SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",FIFTH_TEAM_COLOR_TAG);
					}	
				}
				#endif
				#endif
				#if defined TEAMSIZE
				#if TEAMSIZE == 6
				case SIXTH_TEAM:
				{
					for(new i;i<MAX_PLAYERS;i++)
					{
						SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",SIXTH_TEAM_COLOR_TAG);
					}	
				}
				#endif
				#endif
			}
		}
	}
	if(bool:value == true && count == 1)//Returns the ID of the remaining team
	return i;
	
}
But the compiler says: warning 209: function "TeamsAlive" should return a value
Where is the problem?


Re: function should return a value? But i use return.. - SyS - 21.03.2017

because compiler doesn't know what should that function return if the false statement of last if is executed. In general a function can't be a void and returning one at same time.


Re: function should return a value? But i use return.. - faxxe - 21.03.2017

Ah so what do i have to do in my case to fix this problem?

I never worked with bool before

And btw. which way is better?
I wrote 3 blocks for the same problem


Код:
stock TeamsAlive(bool:value)
{
	new i=0,count=0;
	while(i<TEAMSIZE)
	{
		if(GetTeamCount(i) > 0)
		{
			count++;
		}
		i++;	
	}
	if(bool:value == false)
	switch(count)
	{
		case 1:
		{
			switch(TeamsAlive(true))
			{
				case FIRST_TEAM:
				{
					for(new i;i<MAX_PLAYERS;i++)
					{
						SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",FIRST_TEAM_COLOR_TAG);
					}	
				}
				case SECOND_TEAM:
				{
					for(new i;i<MAX_PLAYERS;i++)
					{
						SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",SECOND_TEAM_COLOR_TAG);
					}	
				}
				#if defined TEAMSIZE
				#if TEAMSIZE >= 3
				case THIRD_TEAM:
				{
					for(new i;i<MAX_PLAYERS;i++)
					{
						SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",THIRD_TEAM_COLOR_TAG);
					}	
				}
				#endif
				#endif
				#if defined TEAMSIZE
				#if TEAMSIZE >= 4
				case FOURTH_TEAM:
				{
					for(new i;i<MAX_PLAYERS;i++)
					{
						SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",FOURTH_TEAM_COLOR_TAG);
					}	
				}
				#endif
				#endif
				#if defined TEAMSIZE
				#if TEAMSIZE >= 5
				case FIFTH_TEAM:
				{
					for(new i;i<MAX_PLAYERS;i++)
					{
						SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",FIFTH_TEAM_COLOR_TAG);
					}	
				}
				#endif
				#endif
				#if defined TEAMSIZE
				#if TEAMSIZE == 6
				case SIXTH_TEAM:
				{
					for(new i;i<MAX_PLAYERS;i++)
					{
						SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",SIXTH_TEAM_COLOR_TAG);
					}	
				}
				#endif
				#endif
			}
		}
	}
	if(bool:value == true && count == 1)//Returns the ID of the remaining team
	return i;
	
}
isEnd() function to call TeamsAlive

Код:
stock TeamsAlive(bool:value)
{
	new i=0,count=0;
	while(i<TEAMSIZE)
	{
		if(GetTeamCount(i) > 0)
		{
			count++;
		}
		i++;	
	}
	if(bool:value == false)//Returns the amount of remaining teams
	return count;
	if(bool:value == true && count == 1)//Returns the ID of the remaining team
	return i;
	
}


stock isEnd()
{
	switch(TeamsAlive(false))
	{
	case 0:
		{
			SendClientMessageToAll(COLOR_WHITE,"SERVER: All teams have been wiped. No one has won the game!");
		}
	case 1:
		{
			switch(TeamsAlive(true))
			{
			case FIRST_TEAM:
				{
					for(new i;i<MAX_PLAYERS;i++)
					{
						SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",FIRST_TEAM_COLOR_TAG);
					}	
				}
			case SECOND_TEAM:
				{
					for(new i;i<MAX_PLAYERS;i++)
					{
						SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",SECOND_TEAM_COLOR_TAG);
					}	
				}
				#if defined TEAMSIZE
				#if TEAMSIZE >= 3
			case THIRD_TEAM:
				{
					for(new i;i<MAX_PLAYERS;i++)
					{
						SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",THIRD_TEAM_COLOR_TAG);
					}	
				}
				#endif
				#endif
				#if defined TEAMSIZE
				#if TEAMSIZE >= 4
			case FOURTH_TEAM:
				{
					for(new i;i<MAX_PLAYERS;i++)
					{
						SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",FOURTH_TEAM_COLOR_TAG);
					}	
				}
				#endif
				#endif
				#if defined TEAMSIZE
				#if TEAMSIZE >= 5
			case FIFTH_TEAM:
				{
					for(new i;i<MAX_PLAYERS;i++)
					{
						SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",FIFTH_TEAM_COLOR_TAG);
					}	
				}
				#endif
				#endif
				#if defined TEAMSIZE
				#if TEAMSIZE == 6
			case SIXTH_TEAM:
				{
					for(new i;i<MAX_PLAYERS;i++)
					{
						SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",SIXTH_TEAM_COLOR_TAG);
					}	
				}
				#endif
				#endif
			}
			printf("Total game time: %d",totaltime);
			SendRconCommand("gmx");
		}
	default:
		{
			
		}
		
	}
	
}
Or massive if statements
Код:
#if defined TEAMSIZE
	#if TEAMSIZE == 2
	if(GetTeamCount(FIRST_TEAM) > 0 && GetTeamCount(SECOND_TEAM) == 0)
	{
		for(new i;i<MAX_PLAYERS;i++)
		{
			SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",FIRST_TEAM_COLOR_TAG);	
		}
		printf("Total game time: %d",totaltime);
		SendRconCommand("gmx");
	}
	if(GetTeamCount(FIRST_TEAM) == 0 && GetTeamCount(SECOND_TEAM) > 0)
	{
		for(new i;i<MAX_PLAYERS;i++)
		{
			SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",SECOND_TEAM_COLOR_TAG);
		}
		printf("Total game time: %d",totaltime);
		SendRconCommand("gmx");
	}
	#endif
	#endif
	
	#if defined TEAMSIZE
	#if TEAMSIZE == 3
	if(GetTeamCount(FIRST_TEAM) > 0 && GetTeamCount(SECOND_TEAM) == 0 && GetTeamCount(THIRD_TEAM) == 0)
	{
		for(new i;i<MAX_PLAYERS;i++)
		{
			SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",FIRST_TEAM_COLOR_TAG);
		}	
		printf("Total game time: %d",totaltime);
		SendRconCommand("gmx");
	}
	if(GetTeamCount(FIRST_TEAM) == 0 && GetTeamCount(SECOND_TEAM) > 0 && GetTeamCount(THIRD_TEAM) == 0)
	{
		for(new i;i<MAX_PLAYERS;i++)
		{
			SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",SECOND_TEAM_COLOR_TAG);
		}
		printf("Total game time: %d",totaltime);
		SendRconCommand("gmx");
	}
	if(GetTeamCount(FIRST_TEAM) == 0 && GetTeamCount(SECOND_TEAM) == 0 && GetTeamCount(THIRD_TEAM) > 0)
	{
		for(new i;i<MAX_PLAYERS;i++)
		{
			SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",THIRD_TEAM_COLOR_TAG);	
		}
		printf("Total game time: %d",totaltime);
		SendRconCommand("gmx");
	}
	#endif
	#endif
	#if defined TEAMSIZE
	#if TEAMSIZE == 4
	if(GetTeamCount(FIRST_TEAM) > 0 && GetTeamCount(SECOND_TEAM) == 0 && GetTeamCount(THIRD_TEAM) == 0 && GetTeamCount(FOURTH_TEAM) == 0)
	{
		for(new i;i<MAX_PLAYERS;i++)
		{
			SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",FIRST_TEAM_COLOR_TAG);
		}
		printf("Total game time: %d",totaltime);
		SendRconCommand("gmx");		
	}
	if(GetTeamCount(FIRST_TEAM) == 0 && GetTeamCount(SECOND_TEAM) > 0 && GetTeamCount(THIRD_TEAM) == 0 && GetTeamCount(FOURTH_TEAM) == 0)
	{
		for(new i;i<MAX_PLAYERS;i++)
		{
			SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",SECOND_TEAM_COLOR_TAG);
		}
		printf("Total game time: %d",totaltime);
		SendRconCommand("gmx");
	}
	if(GetTeamCount(FIRST_TEAM) == 0 && GetTeamCount(SECOND_TEAM) == 0 && GetTeamCount(THIRD_TEAM) > 0 && GetTeamCount(FOURTH_TEAM) == 0)
	{
		for(new i;i<MAX_PLAYERS;i++)
		{
			SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",THIRD_TEAM_COLOR_TAG);
		}
		printf("Total game time: %d",totaltime);
		SendRconCommand("gmx");
	}
	if(GetTeamCount(FIRST_TEAM) == 0 && GetTeamCount(SECOND_TEAM) == 0 && GetTeamCount(THIRD_TEAM) == 0 && GetTeamCount(FOURTH_TEAM) > 0)
	{
		for(new i;i<MAX_PLAYERS;i++)
		{
			SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",FOURTH_TEAM_COLOR_TAG);	
		}
		printf("Total game time: %d",totaltime);
		SendRconCommand("gmx");
	}
	#endif
	#endif
	
	#if defined TEAMSIZE
	#if TEAMSIZE == 5
	if(GetTeamCount(FIRST_TEAM) > 0 && GetTeamCount(SECOND_TEAM) == 0 && GetTeamCount(THIRD_TEAM) == 0 && GetTeamCount(FOURTH_TEAM) == 0 && GetTeamCount(FIFTH_TEAM)== 0)
	{
		for(new i;i<MAX_PLAYERS;i++)
		{
			SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",FIRST_TEAM_COLOR_TAG);	
		}
		printf("Total game time: %d",totaltime);
		SendRconCommand("gmx");
	}
	if(GetTeamCount(FIRST_TEAM) == 0 && GetTeamCount(SECOND_TEAM) > 0 && GetTeamCount(THIRD_TEAM) == 0 && GetTeamCount(FOURTH_TEAM) == 0 && GetTeamCount(FIFTH_TEAM)== 0)
	{	
		for(new i;i<MAX_PLAYERS;i++)
		{
			SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",SECOND_TEAM_COLOR_TAG);
		}
		printf("Total game time: %d",totaltime);
		SendRconCommand("gmx");
	}
	if(GetTeamCount(FIRST_TEAM) == 0 && GetTeamCount(SECOND_TEAM) == 0 && GetTeamCount(THIRD_TEAM) > 0 && GetTeamCount(FOURTH_TEAM) == 0 && GetTeamCount(FIFTH_TEAM)== 0)
	{
		for(new i;i<MAX_PLAYERS;i++)
		{
			SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",THIRD_TEAM_COLOR_TAG);
		}
		printf("Total game time: %d",totaltime);
		SendRconCommand("gmx");
	}
	if(GetTeamCount(FIRST_TEAM) == 0 && GetTeamCount(SECOND_TEAM) == 0 && GetTeamCount(THIRD_TEAM) == 0 && GetTeamCount(FOURTH_TEAM) > 0 && GetTeamCount(FIFTH_TEAM)== 0)
	{
		for(new i;i<MAX_PLAYERS;i++)
		{
			SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",FOURTH_TEAM_COLOR_TAG);	
		}
		printf("Total game time: %d",totaltime);
		SendRconCommand("gmx");
	}
	if(GetTeamCount(FIRST_TEAM) == 0 && GetTeamCount(SECOND_TEAM) == 0 && GetTeamCount(THIRD_TEAM) == 0 && GetTeamCount(FOURTH_TEAM) == 0 && GetTeamCount(FIFTH_TEAM) > 0)
	{
		for(new i;i<MAX_PLAYERS;i++)
		{
			SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",FIFTH_TEAM_COLOR_TAG);	
		}
		printf("Total game time: %d",totaltime);
		SendRconCommand("gmx");
	}
	#endif
	#endif
	
	#if defined TEAMSIZE
	#if TEAMSIZE == 6
	if(GetTeamCount(FIRST_TEAM) > 0 && GetTeamCount(SECOND_TEAM) == 0 && GetTeamCount(THIRD_TEAM) == 0 && GetTeamCount(FOURTH_TEAM) == 0 && GetTeamCount(FIFTH_TEAM) == 0 && GetTeamCount(SIXTH_TEAM) == 0)
	{
		for(new i;i<MAX_PLAYERS;i++)
		{
			SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",FIRST_TEAM_COLOR_TAG);	
		}
		printf("Total game time: %d",totaltime);
		SendRconCommand("gmx");
	}
	if(GetTeamCount(FIRST_TEAM) == 0 && GetTeamCount(SECOND_TEAM) > 0 && GetTeamCount(THIRD_TEAM) == 0 && GetTeamCount(FOURTH_TEAM) == 0 && GetTeamCount(FIFTH_TEAM)== 0 && GetTeamCount(SIXTH_TEAM) == 0)
	{
		for(new i;i<MAX_PLAYERS;i++)
		{
			SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",SECOND_TEAM_COLOR_TAG);
		}
		printf("Total game time: %d",totaltime);
		SendRconCommand("gmx");
	}
	if(GetTeamCount(FIRST_TEAM) == 0 && GetTeamCount(SECOND_TEAM) == 0 && GetTeamCount(THIRD_TEAM) > 0 && GetTeamCount(FOURTH_TEAM) == 0 && GetTeamCount(FIFTH_TEAM)== 0 && GetTeamCount(SIXTH_TEAM) == 0)
	{
		for(new i;i<MAX_PLAYERS;i++)
		{
			SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",THIRD_TEAM_COLOR_TAG);
		}
		printf("Total game time: %d",totaltime);
		SendRconCommand("gmx");
	}
	if(GetTeamCount(FIRST_TEAM) == 0 && GetTeamCount(SECOND_TEAM) == 0 && GetTeamCount(THIRD_TEAM) == 0 && GetTeamCount(FOURTH_TEAM) > 0 && GetTeamCount(FIFTH_TEAM)== 0 && GetTeamCount(SIXTH_TEAM) == 0)
	{
		for(new i;i<MAX_PLAYERS;i++)
		{
			SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",FOURTH_TEAM_COLOR_TAG);	
		}
		printf("Total game time: %d",totaltime);
		SendRconCommand("gmx");
	}
	if(GetTeamCount(FIRST_TEAM) == 0 && GetTeamCount(SECOND_TEAM) == 0 && GetTeamCount(THIRD_TEAM) == 0 && GetTeamCount(FOURTH_TEAM) == 0 && GetTeamCount(FIFTH_TEAM) > 0 && GetTeamCount(SIXTH_TEAM) == 0)
	{
		for(new i;i<MAX_PLAYERS;i++)
		{
			SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",FIFTH_TEAM_COLOR_TAG);	
		}
		printf("Total game time: %d",totaltime);
		SendRconCommand("gmx");
	}
	if(GetTeamCount(FIRST_TEAM) == 0 && GetTeamCount(SECOND_TEAM) == 0 && GetTeamCount(THIRD_TEAM) == 0 && GetTeamCount(FOURTH_TEAM) == 0 && GetTeamCount(FIFTH_TEAM) == 0 && GetTeamCount(SIXTH_TEAM) > 0)
	{
		for(new i;i<MAX_PLAYERS;i++)
		{
			SendClientMessageEx(i,COLOR_WHITE,"SERVER: All remaining teams have been wiped. Team %s {FFFFFF}has won the game!",SIXTH_TEAM_COLOR_TAG);
		}	
		printf("Total game time: %d",totaltime);
		SendRconCommand("gmx");
	}
	#endif
	#endif



Re: function should return a value? But i use return.. - SyS - 21.03.2017

depends upon on your intentions but I wont recommend last one as in case you want to work in that code after 2 months or later the readability can be a pain.


Re: function should return a value? But i use return.. - OneDay - 21.03.2017

Quote:
Originally Posted by faxxe
Посмотреть сообщение
Код:
	if(bool:value == true && count == 1)//Returns the ID of the remaining team
	return i;
}
means:

Код:
	if(bool:value == true && count == 1)
	{
		return i;
	}
	// no return if false
}
if value is false or count is not 1 there is no return