[Tutorial] Most Wanted Command [CNR/RP-Related]
#1




In servers where wanted levels are a main factor of the gamemode, it's nice to have a command to see whom is the highest wanted player
within the server at any given time. This can be useful for having a contest to see whom gets the highest wanted level, or even just a
player-used command like so. The great thing about the code I'm providing is that it can be expanded into various uses, not just the command itself!



You will want to start out by creating the command itself that you would like it to be listed as. ( This command is created using the ZCMD command processor. )
Code:
CMD:mostwanted( playerid, params[] ) { //This is how you will cast the command in-game.


	
	return 1; //This ends the command's functionality.

}
You will then want to create the variables that we plan to use inside the command.
Code:
	new
	    iMostWantedPlayer,
	    iMostWanted
	;
Now that we have the command and it's required variables created, we need to go through all the online players and see what their wanted level currently is.
If it's higher than the current highest, it will set the iMostWantedPlayer variable to that player's ID.
Code:
	for (new i = 0; i < MAX_PLAYERS; i++) { //Starts the loops for players.
	
		new
			iLevel = GetPlayerWantedLevel( iMostWantedPlayer ) //Creates a virtual level to compare to others as it loops through player IDs.
		;

		if ( iLevel != 0 && iLevel > iMostWanted ) { //If the current iLevel is higher than the previous iMostWanted level, it will adjust.
		
		    iMostWanted = iLevel; //Sets new highest wanted level.
		    iMostWantedPlayer = i; //Grabs the most wanted player's ID.
		
		}
	
	}
We will need to add some smart checks just to make sure it doesn't spit a message with values that aren't helpful to whomever is casting the command.
Code:
	if ( iMostWanted == 0 || !IsPlayerConnected( iMostWantedPlayer) ) { //If there 0 wanted players online, or if the person whom had the highest wanted level is now offline, it will return this value. (The IsPlayerConnected is just a fail safe and isn't completely necessary. It's a "just-in-case" factor.)

		return SendClientMessage(playerid, -1, "{b20000}[MOST WANTED] {FFFFFF}There aren't any civilians with a wanted level."); //The message that is returned if those checks aren't passed.
	
	}
Lastly, we can submit the most wanted player results to whom casted the command.
Code:
	format( szNormalString, sizeof( szNormalString ), "{b20000}[MOST WANTED] {FFFFFF}%s is the most wanted player with a wanted level of %i! {FF0000}Proceed with caution.", ReturnPlayerName( iMostWantedPlayer ), iMostWanted ); //Formats the message and it's data to be sent into a message.
	SendClientMessage( playerid, -1, szNormalString ); //Responsible for carrying out the client message.
Note: In the formatting of the client message, it uses a 'ReturnPlayerName' function that is fairly useful among a gamemode and filterscripts, though not necessary.
[ You can combine the 'szName' variable into the top of the file for use among the entire file, if using in a gamemode or a large filterscript. ]
Code:
new
	szName[MAX_PLAYER_NAME]
;

stock ReturnPlayerName(playerid) {

    GetPlayerName(playerid, szName, sizeof(szName));

    return szName;

}


In the end, you should have the following code:
Code:
new
	szName[MAX_PLAYER_NAME]
;

CMD:mostwanted( playerid, params[] ) {

	new
	    iMostWantedPlayer,
		iMostWanted
	;

	for (new i = 0; i < MAX_PLAYERS; i++) {
	
		new
			iLevel = GetPlayerWantedLevel( iMostWantedPlayer )
		;

		if ( iLevel != 0 && iLevel > iMostWanted ) {
		
		    iMostWanted = iLevel;
		    iMostWantedPlayer = i;
		
		}
	
	}

	if ( iMostWanted == 0 || !IsPlayerConnected( iMostWantedPlayer) ) {

		return SendClientMessage(playerid, -1, "{b20000}[MOST WANTED] {FFFFFF}There aren't any civilians with a wanted level.");
	
	}

	format( szNormalString, sizeof( szNormalString ), "{b20000}[MOST WANTED] {FFFFFF}%s is the most wanted player with a wanted level of %i! {FF0000}Proceed with caution.", ReturnPlayerName( iMostWantedPlayer ), iMostWanted );
	SendClientMessage( playerid, -1, szNormalString );
	
	return 1;

}

stock ReturnPlayerName(playerid) {

    GetPlayerName(playerid, szName, sizeof(szName));

    return szName;

}
Reply
#2




What is even more triggering for me is this:


Why do you like to write it like this? It's a serious question, trolling above aside I just think it looks very bad when reading it. I get if there's a lot, like a lot lot lot, of variables but one to three, sometimes four, doesn't seem like this makes it look good. Feel free to share your view on it because I'm quite interested.
Reply
#3

Quote:
Originally Posted by Hansrutger
View Post



What is even more triggering for me is this:


Why do you like to write it like this? It's a serious question, trolling above aside I just think it looks very bad when reading it. I get if there's a lot, like a lot lot lot, of variables but one to three, sometimes four, doesn't seem like this makes it look good. Feel free to share your view on it because I'm quite interested.
Do it for organization purposes and also for the fact that if people are using this and plan to add onto it later, it's a simple drop down, while keeping it organized for them.

If you don't like it, simply, backspace a few times and poof.
Reply
#4

Well do however you want, just wanted to know the mindset behind it.
Reply
#5

Nice but can you also tell How to make Most Wanted players that will show every player from the top to bottom.. For example: there are 10 players in the server and show the 10 players according to their wanted level
Reply
#6

Systems Hungarian should die a painful death. It makes code ten times harder to read. Apart from that this tutorial pretty decent, though.
Reply
#7

Quote:
Originally Posted by coool
View Post
Nice but can you also tell How to make Most Wanted players that will show every player from the top to bottom.. For example: there are 10 players in the server and show the 10 players according to their wanted level
So, you're talking about a "Top 10 Most Wanted" dialogue / chat ?
Reply
#8

Quote:
Originally Posted by Vince
View Post
Systems Hungarian should die a painful death. It makes code ten times harder to read. Apart from that this tutorial pretty decent, though.
It's helpful in some sense, but yes, I understand what you're coming from.

It's just mainly to help people identify what a variable's purpose is when looking at an abundance of code and/or people whom didn't actually create it.
Reply
#9

Quote:
Originally Posted by Daymen
View Post
So, you're talking about a "Top 10 Most Wanted" dialogue / chat ?
Yes, the top 10 or the whole players present in server.
Reply
#10

Quote:
Originally Posted by coool
View Post
Yes, the top 10 or the whole players present in server.
You'd have to integrate an array named whichever you like. (Example: 'top10_messages[9]')

It would be 0-9, which is 10 results. You would then have to create a system that then sets the values of the array to be adjusted as it goes through the player list to then be in order from top to bottom.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)