SA-MP Forums Archive
[Tutorial] [TUT] How to have conversations with NPCs! - 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)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] [TUT] How to have conversations with NPCs! (/showthread.php?tid=96030)



[TUT] How to have conversations with NPCs! - gemadon - 05.09.2009

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:

Код:
forward GetClosestPlayer(p1);
Then at the bottom of your script add this:

Код:
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;
}
This will make the script get the closest player whenever you use the function....

Okay!

Now go to your, OnPlayerText function..

Under it put:
Код:
new bot = GetClosestPlayer(playerid);
Under that:
Код:
new to_others;
And:
Код:
	new name[24];
	GetPlayerName(bot, name, 24);
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:
Код:
if(IsPlayerNPC(bot))
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:
Код:
	if(!strcmp(text,"hello!",true))
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:
Код:
{
	format(to_others,MAX_CHATBUBBLE_LENGTH,"%s: Hi, how are you?",name);
	SetPlayerChatBubble(bot,to_others,MESSAGE_COLOR,35.0,10000);
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:

Код:
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;
}
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:
Код:
new RandR = random(4)
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:
Код:
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;
}



Re: [TUT] How to have conversations with NPCs! - [mad]MLK - 06.09.2009

lol dude this is kick ass


Re: [TUT] How to have conversations with NPCs! - Infamous - 06.09.2009

I like this, this tutorial has certainly helped me. Good job.


Re: [TUT] How to have conversations with NPCs! - Gamer_Z - 06.09.2009

very usefull to make a taxi driver.. etc drive me to lv and a playback with lv recording will start :P
this topic should be sticked


Re: [TUT] How to have conversations with NPCs! - Mive - 06.09.2009

Omg this is really nice


Re: [TUT] How to have conversations with NPCs! - [mad]MLK - 07.09.2009

i get these errors with that script:

Код:
C:\Users\user\Desktop\samp03svr_RC3-2_win32\npcmodes\welcomebot.pwn(9) : error 004: function "GetClosestPlayer" is not implemented
C:\Users\user\Desktop\samp03svr_RC3-2_win32\npcmodes\welcomebot.pwn(11) : warning 217: loose indentation
C:\Users\user\Desktop\samp03svr_RC3-2_win32\npcmodes\welcomebot.pwn(12) : warning 217: loose indentation
C:\Users\user\Desktop\samp03svr_RC3-2_win32\npcmodes\welcomebot.pwn(12) : error 017: undefined symbol "IsPlayerNPC"
C:\Users\user\Desktop\samp03svr_RC3-2_win32\npcmodes\welcomebot.pwn(16) : error 017: undefined symbol "to_others"
C:\Users\user\Desktop\samp03svr_RC3-2_win32\npcmodes\welcomebot.pwn(17) : error 017: undefined symbol "SetPlayerChatBubble"
C:\Users\user\Desktop\samp03svr_RC3-2_win32\npcmodes\welcomebot.pwn(19) : warning 217: loose indentation
C:\Users\user\Desktop\samp03svr_RC3-2_win32\npcmodes\welcomebot.pwn(22) : error 029: invalid expression, assumed zero
C:\Users\user\Desktop\samp03svr_RC3-2_win32\npcmodes\welcomebot.pwn(22) : error 004: function "GetClosestPlayer" is not implemented
C:\Users\user\Desktop\samp03svr_RC3-2_win32\npcmodes\welcomebot.pwn(31) : error 017: undefined symbol "p1"
C:\Users\user\Desktop\samp03svr_RC3-2_win32\npcmodes\welcomebot.pwn(33) : error 017: undefined symbol "GetDistanceBetweenPlayers"
C:\Users\user\Desktop\samp03svr_RC3-2_win32\npcmodes\welcomebot.pwn(44) : error 030: compound statement not closed at the end of file (started at line 9)
Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase


9 Errors.



Re: [TUT] How to have conversations with NPCs! - Daren_Jacobson - 26.10.2009

random(4) could return any numbers in the set {0, 1, 2, 3} please fix your code to comply.


Re: [TUT] How to have conversations with NPCs! - MenaceX^ - 26.10.2009

exclamation mark.


Re: [TUT] How to have conversations with NPCs! - Davz*|*Criss - 15.03.2011

Great tutorial


Re: [TUT] How to have conversations with NPCs! - Retardedwolf - 15.03.2011

Quote:
Originally Posted by Davz*|*Criss
Посмотреть сообщение
Great
bump.

Also, gCP could also be considered inefficient since we now have foreach and other ways to make faster loops to retrieve a list of players(online)