05.09.2009, 23:57
Hi, in this tutorial, I am going to show you how to have conversations with NPCs in game!
Ok, first of all add the following on top of your script:
Then at the bottom of your script add this:
This will make the script get the closest player whenever you use the function....
Okay!
Now go to your, OnPlayerText function..
Under it put:
Under that:
And:
This will get the closest player, and the player's name.... but wait!
The script still doesn't know wether the player is a bot or not!
Under that, put:
Now, it will get the closest player, and the following script under that will only work with bots!
Lets start off simple, with a "hello!"...So under that just put a start of body "{" and put:
So, the script gets your text, and if your text says "hello!" then the bot will reply.... but the script still doesnt know what the bot will say! No worries...
Put this under that:
So, when you type "hello!", the bot will reply "Hi, how are you?", in a chat bubble!
So, at the end, it should look like this:
Under that, you can just copy the strcmp(text) and add more things that you can say to your bot!
Sorry for poor indentations
HOW TO MAKE EACH NPC REPLY RANDOM
Okay, so.. I dont think you want all your NPCs to reply with the same thing all the time, right?
So lets use the random function!
under OnPlayerText, put:
So, with the number 4, your bot can reply saying 3 different things... You can change the number if you want more...
under your text I explained up above, where it says "hello!", under that, put if(RandR == 1)
Under that, put what you want if the random number returns as 1..
And do that with each number! So it would look something like:
Ok, first of all add the following on top of your script:
Код:
forward GetClosestPlayer(p1);
Код:
public GetClosestPlayer(p1) { new x,Float:dis,Float:dis2,person; person = -1; dis = 99999.99; for (x=0;x<MAX_PLAYERS;x++) { if(IsPlayerConnected(x)) { if(x != p1) { dis2 = GetDistanceBetweenPlayers(x,p1); if(dis2 < dis && dis2 != -1.00) { dis = dis2; person = x; } } } } return person; }
Okay!
Now go to your, OnPlayerText function..
Under it put:
Код:
new bot = GetClosestPlayer(playerid);
Код:
new to_others;
Код:
new name[24]; GetPlayerName(bot, name, 24);
The script still doesn't know wether the player is a bot or not!
Under that, put:
Код:
if(IsPlayerNPC(bot))
Lets start off simple, with a "hello!"...So under that just put a start of body "{" and put:
Код:
if(!strcmp(text,"hello!",true))
Put this under that:
Код:
{ format(to_others,MAX_CHATBUBBLE_LENGTH,"%s: Hi, how are you?",name); SetPlayerChatBubble(bot,to_others,MESSAGE_COLOR,35.0,10000);
So, at the end, it should look like this:
Код:
public OnPlayerText(playerid, text[]) { new bot = GetClosestPlayer(playerid); new name[24]; GetPlayerName(bot, name, 24); if(IsPlayerNPC(bot)) { if(!strcmp(text,"hello",true)) { format(to_others,MAX_CHATBUBBLE_LENGTH,"%s: Hi, how are you?",name); SetPlayerChatBubble(bot,to_others,MESSAGE_COLOR,35.0,10000); } return 1; }
Sorry for poor indentations
HOW TO MAKE EACH NPC REPLY RANDOM
Okay, so.. I dont think you want all your NPCs to reply with the same thing all the time, right?
So lets use the random function!
under OnPlayerText, put:
Код:
new RandR = random(4)
under your text I explained up above, where it says "hello!", under that, put if(RandR == 1)
Under that, put what you want if the random number returns as 1..
And do that with each number! So it would look something like:
Код:
public OnPlayerText(playerid, text[]) { new bot = GetClosestPlayer(playerid); new name[24]; new RandR = random(4); GetPlayerName(bot, name, 24); if(IsPlayerNPC(bot)) { if(!strcmp(text,"hello",true)) { if(RandR == 1) { format(to_others,MAX_CHATBUBBLE_LENGTH,"%s: Hi, how are you?",name); SetPlayerChatBubble(bot,to_others,MESSAGE_COLOR,35.0,10000); } else if(RandR == 2) //And so on return 1; }