[Tutorial] Using OnPlayerText To Make Questions
#1

Introduction

Ok, we've all been in a server that asks you simple questions to get your input and use it in the script. How do they do that? Its simple actually, all you have to do is use a few return 0;'s.

Preparation

If you want to have a really nice looking script your going to need a few things. Colors would be important and also a few good questions related to your server/script. Ill be using these for my examples: Lightblue, lightred, white, a new variable, and a few basic questions.

Defining Colors
- In order to define a color you must use its hex code and place it in your script with #define [color] [hex]
- [color] is what we want to name our color in order to use it later
- [hex] is the hexadecimal code of the color we are using

LIGHTBLUE
Код:
#define LIGHTBLUE 0x33CCFFAA
LIGHTRED
Код:
#define LIGHTRED 0xFF6347AA
WHITE
Код:
#define WHITE 0xFFFFFFAA
Defining Variables
- In order to define a new variable you must add it in your script with new [variable]
- [variable] is what we want to name our new variable in order to use later

RegistrationStep
pawn Код:
new RegistrationStep[MAX_PLAYERS];
- [MAX_PLAYERS] states that we want this variable to be created for every player

Creating the Questions
- What is your Age?
- Are you a Male or Female?

Now comes the actual scripting. To create these questions and make them show up for each player that joins the server your going to need to add a few lines in your script. I have chose the OnPlayerSpawn section. So under OnPlayerSpawn your going to need to make a new IF statement.

Код:
public OnPlayerSpawn(playerid)
{
	if(RegistrationStep[playerid] == 0)
	{
- This statement checks to see if our players RegistrationStep variable is equal to 0

Now that we have checked to see if the variable is 0 we need to create what happens next. So, we now want our first question to pop up and ask us to input some text, freeze the player and set our registration step. We do this by using SendClientMessage's, and then setting our RegistrationStep to now become equal to 1 and freezing the player until done.

SendClientMessage
- In order to SendClientMessage we must follow the syntax SendClientMessage(playerid,color,[text])
- playerid is the player to send the message to in this case the player spawning so its fine as just playerid
- color is what color we want to use we use 2 LIGHTBLUE, and WHITE
- [text] is the message we want to type use "quotes" to make the text message "enter message"

Код:
public OnPlayerSpawn(playerid)
{
	if(RegistrationStep[playerid] == 0)
	{
		SendClientMessage(playerid,LIGHTBLUE,"What is your Age?");
		SendClientMessage(playerid,WHITE,"Hint: Enter your 2 digit age");
		TogglePlayerControllable(playerid,0); //Freezes the player
		RegistrationStep[playerid] = 1; //Sets our registrationstep to 1
		return 1;
	}
	//The rest of our on player spawn script goes below this ending in return 1;
	return 1;
}
Ok so now we have our first question popping up and its asking us to enter our 2 digit age. How do we get the input and use it in our script now? This is where you go down to the OnPlayerText section of your script and start making a new section. Starting with an IF statement and ending in return 0; Why return 0? Because we dont want the text they input to be entered into the chat just to the server console and for the script to see.

Код:
public OnPlayerText(playerid, text[])
{
	if(RegistrationStep[playerid] == 1)
	{
Ok now the complex part is making our command. First we must check to see that there is indeed some text entered. We do this by checking the string length(strlen) of the text.
Код:
public OnPlayerText(playerid, text[])
{
	if(RegistrationStep[playerid] == 1)
	{
		if(!strlen(text)) //Checks to make sure there is at least 1 character input
		{
Now, if there is no input we want them to see the initial question again and end the command. To do this we use SendClientMessage and a return 0.
Код:
public OnPlayerText(playerid, text[])
{
	if(RegistrationStep[playerid] == 1)
	{
		if(!strlen(text)) //Check to make sure there is at least 1 character input
		{
			SendClientMessage(playerid,LIGHTBLUE,"What is your Age?");
			SendClientMessage(playerid,WHITE,"Hint: Enter your 2 digit age);
			return 0; //Ends the commands and doesnt display the text
		}
Now in order to move on the player will have to enter a 2 digit age so how do we check to see if they are indeed only entering to digits? We do this checking the string length(strlen) again but this time we make sure they are only allowed to use 2 characters max.
Код:
public OnPlayerText(playerid, text[])
{
	if(RegistrationStep[playerid] == 1)
	{
		if(!strlen(text)) //Check to make sure there is at least 1 character input
		{
			SendClientMessage(playerid,LIGHTBLUE,"What is your Age?");
			SendClientMessage(playerid,WHITE,"Hint: Enter your 2 digit age);
			return 0;
		}
		if(strlen(text)< 2||strlen(text) > 2) //Checks that the input is only 2 characters long
		{
Ok now we need to make it show our error message if they are still stubborn and enter only 1 or more than 2 characters.
Код:
public OnPlayerText(playerid, text[])
{
	if(RegistrationStep[playerid] == 1)
	{
		if(!strlen(text)) //Check to make sure there is at least 1 character input
		{
			SendClientMessage(playerid,LIGHTBLUE,"What is your Age?");
			SendClientMessage(playerid,WHITE,"Hint: Enter your 2 digit age);
			return 0;
		}
		if(strlen(text) < 2||strlen(text) > 2) //Checks that the input is only 2 characters long
		{ 
			SendClientMessage(playerid,LIGHTRED,"ONLY A 2 DIGIT NUMBER WILL WORK!");
			return 0;
		}
Ok finally we have got past all the restrictions of our code and we need to get the input and use it now. We do this by getting the string value(strval) of the text and using it in our script like follows:
Код:
public OnPlayerText(playerid, text[])
{
	if(RegistrationStep[playerid] == 1)
	{
		if(!strlen(text)) //Check to make sure there is at least 1 character input
		{
			SendClientMessage(playerid,LIGHTBLUE,"What is your Age?");
			SendClientMessage(playerid,WHITE,"Hint: Enter your 2 digit age);
			return 0;
		}
		if(strlen(text) < 2||strlen(text) > 2) //Checks that the input is only 2 characters long
		{ 
			SendClientMessage(playerid,LIGHTRED,"ONLY A 2 DIGIT NUMBER WILL WORK!");
			return 0;
		}
		new string[128]; //Creates a new string 128 characters long
		format(string,sizeof(string),"Ok so you are %d years old",strval(text)); //Formats the string
		SendClientMessage(playerid,LIGHTBLUE,string); //Sends new message with the string
		PlayerInfo[playerid][pAge] = strval(text);
		SendClientMessage(playerid,LIGHTBLUE,"What is your Sex?");
		RegistrationStep[playerid] = 2;
		return 0;
	}
Now we have one whole question completed and we have the second one asking us for some more input. I will just summarize this last code snippet and paste it as a whole as it is very similiar to what we have learned from the last question.
Код:
public OnPlayerText(playerid, text[])
{
	if(RegistrationStep[playerid] == 1)
	{
	//blah blah blah we did this already...
	}
	if(RegistrationStep[playerid] == 2)
	{
		if(!strlen(text)) //Check to make sure there is at least 1 character input
		{
			SendClientMessage(playerid,LIGHTBLUE,"What is your Sex?");
			SendClientMessage(playerid,WHITE,"Hint: Enter male or female");
			return 0;
		}
		if((strcmp("male", tmp, true, strlen(tmp)) == 0) && (strlen(tmp) == strlen("male")))
		{//Checks to see if the input matches "male" and if it does continues
			PlayerInfo[playerid][pSex] = 1;
			SendClientMessage(playerid,LIGHTBLUE,"Ok, so you are a Male.");
			SendClientMessage(playerid,WHITE,"Thank you for filling in the information");
			RegistrationStep[playerid] = 3;
			TogglePlayerControllable(playerid,1);//Unfreezes the player and play resumes
			return 0;
		}
		else if((strcmp("female", tmp, true, strlen(tmp)) == 0) && (strlen(tmp) == strlen("female")))
		{//Checks to see if the input matches "female" and if it does continues
			PlayerInfo[playerid][pSex] = 2;
			SendClientMessage(playerid,LIGHTBLUE,"Ok, so you are a Female.");
			SendClientMessage(playerid,WHITE,"Thank you for filling in the information");
			RegistrationStep[playerid] = 3;
			TogglePlayerControllable(playerid,1);//Unfreezes the player and play resumes
			return 0;
		}
	}
	return 1;
}
Conclusion

So there you have it a really indepth tutorial on how to use the onplayertext section to get various input text and use it in your script and how they make it so you dont see what is actually typed. Hope this tutorial has helped some of you guys out. This is my second tutorial and I hope I am getting alot better at writing these for you to understand.

p.s. I used code snippets rather than pawn snippets because the use of return 0's makes the pawn code snippet show up wrong when indeed it actually shows up fine in pawno... Go figure..
Reply


Messages In This Thread
Using OnPlayerText To Make Questions - by SuperS82 - 30.11.2010, 19:43
Re: Using OnPlayerText To Make Questions - by [L3th4l] - 30.11.2010, 22:12
Re: Using OnPlayerText To Make Questions - by Basicz - 01.12.2010, 12:13
Re: Using OnPlayerText To Make Questions - by SuperS82 - 01.12.2010, 20:32
Re: Using OnPlayerText To Make Questions - by Skylar Paul - 22.03.2011, 23:20
Re: Using OnPlayerText To Make Questions - by rubygta - 23.03.2011, 14:55
Re: Using OnPlayerText To Make Questions - by Davz*|*Criss - 23.03.2011, 22:57
Re: Using OnPlayerText To Make Questions - by justsomeguy - 24.03.2011, 07:57
Re: Using OnPlayerText To Make Questions - by [XST]O_x - 24.03.2011, 23:31

Forum Jump:


Users browsing this thread: 1 Guest(s)