[Tutorial] Creating a new NPC
#1

This is bound to be a hot topic for questions, so I will go through all that I have worked out how to do with NPCs as a tutorial.

Recording a playback file
First of all, we need to record a playback file for our NPC to use. Start up a server, any gamemode will do, then login to rcon. (Type "/rcon login <your_rcon_pass_here>" ) and load the filterscript npc_record (Type "/rcon loadfs npc_record" )
Now, there are 3 main recording commands in the npc_record filterscript...
  • /vrecord <filename> - Starts recording a vehicle path to the specified filename.
  • /ofrecord <filename> - Starts recording an on foot path to the specified filename.
  • /stoprecord - Stops recording both vehicle and on foot paths.
In this tutorial, we will be making a vehicle path, so get in a vehicle, and type /vrecord mynpc (Note you must be logged into rcon) to start recording.
Drive around a bit, and when you are finished type /stoprecord.
Close the game, and go to your scriptfiles directory, there should be a file called mynpc.rec. Move that file to <Your SA-MP server directory>/npcmodes/recordings/
Done? Okay, great! You have now recorded a path for your NPC, and moved it into the required directory.

Controlling the NPC
You may have noticed a lot of pawn scripts in the npcmodes directory, these are standalone "mini" scripts that control the behaviour of your NPC.
So, lets create one for your freshly recorded path!
Start a new file in pawno, and replace its contents with...
pawn Code:
#define RECORDING "mynpc" //This is the filename of your recording without the extension.
#define RECORDING_TYPE 1 //1 for in vehicle and 2 for on foot.

#include <a_npc>
main(){}
public OnRecordingPlaybackEnd() StartRecordingPlayback(RECORDING_TYPE, RECORDING);

#if RECORDING_TYPE == 1
  public OnNPCEnterVehicle(vehicleid, seatid) StartRecordingPlayback(RECORDING_TYPE, RECORDING);
  public OnNPCExitVehicle() StopRecordingPlayback();
#else
  public OnNPCSpawn() StartRecordingPlayback(RECORDING_TYPE, RECORDING);
#endif
That may look confusing, so I will now go through it line by line.

pawn Code:
#define RECORDING "mynpc" //This is the filename of your recording without the extension.
As the comment says, this is the filename of the path you recorded without the extension. Our playback recording was named mynpc, and its file was mynpc.rec so we enter in "mynpc"

pawn Code:
#define RECORDING_TYPE 1 //1 for in vehicle and 2 for on foot.
As the comment says again, this is the recording type. If your NPC is on foot, this define would be set to 2, if it was in a vehicle it would be set to 1.

pawn Code:
#include <a_npc>
Including the SA-MP NPC functions.

pawn Code:
public OnRecordingPlaybackEnd() StartRecordingPlayback(RECORDING_TYPE, RECORDING);
OnRecordingPlaybackEnd is a callback included in the NPC library included the line above. This line of code makes the recording loop, as when the recording playback has ended, it starts the recording again.

pawn Code:
public OnNPCEnterVehicle(vehicleid, seatid) StartRecordingPlayback(RECORDING_TYPE, RECORDING);
This is another callback included in the NPC library. This will start the recording when the NPC has been put in a vehicle.

pawn Code:
public OnNPCExitVehicle() StopRecordingPlayback();
Yet another callback included in the NPC library, this will stop playing back the recording if for any reason the NPC exits the vehicle.

pawn Code:
public OnNPCSpawn() StartRecordingPlayback(RECORDING_TYPE, RECORDING);
Yet another callback included in the NPC library, this will start playback when the NPC spawns.

As you can see, it is all very simple. Now save your NPC "mini" script as "mynpc.pwn" in the <Your SA-MP server directory>/npcmodes/ directory, and compile.
If it compiled, you can now continue to the last step of this tutorial, if it failed to compile, make sure you have updated your SA-MP includes to the latest 0.3 versions, and make sure you haven't missed any brackets or semi-colons. Still won't compile? Post a reply.

Connecting your NPC
So, we have the path file, we have the "mini" pawn script for controlling the NPC directly, now we need to load the NPC into a gamemode or filterscript!
Close the "mini" pawn script, and load up your gamemode or a filterscript in pawno, It's time to be introduced to the ConnectNPC function, and as its name suggests it is used to connect an NPC to your server.
Lets explore this functions parameters, and what they mean...

pawn Code:
ConnectNPC("Dave","mynpc");
  • "Dave" - The first parameter is the NPC's name. With this you can detect different NPCs, and perform different actions to them.
  • "mynpc" - This is the filename (without extension) of the "mini" script we created above.
With your new knowledge of the ConnectNPC function, lets try and make our NPC connect when your gamemode or filterscript loads.

pawn Code:
public OnGameModeInit()
{
  print("my gamemode");
  ConnectNPC("MyFirstNPC","mynpc");
  return 1;
}
Or, if you are adding the NPC into a filterscript...

pawn Code:
public OnFilterScriptInit()
{
  print("my filterscript");
  ConnectNPC("MyFirstNPC","mynpc");
  return 1;
}
Now, as we created a path in a vehicle, we need to create a vehicle for the NPC to drive around in.
OnGameModeInit looks like this, now I have created a vehicle for the NPC to use...
NOTE: If your NPC is not in a vehicle you can skip this step.
pawn Code:
new MyFirstNPCVehicle; //Global variable!
public OnGameModeInit()
{
  print("my gamemode");
  ConnectNPC("MyFirstNPC","mynpc");
  MyFirstNPCVehicle = CreateVehicle(400, 0.0, 0.0, 5.0, 0.0, 3, 3, 5000);
  return 1;
}
Or, if you are adding the NPC into a filterscript...

pawn Code:
new MyFirstNPCVehicle; //Global variable!
public OnFilterScriptInit()
{
  print("my filterscript");
  ConnectNPC("MyFirstNPC","mynpc");
  MyFirstNPCVehicle = CreateVehicle(400, 0.0, 0.0, 5.0, 0.0, 3, 3, 5000);
  return 1;
}
Note that the actual location of the vehicle doesn't matter, as it will be teleported to wherever the NPC's path starts, when the path starts playing back.

Just one more thing before you can go in-game and test out your first NPC, we need to put the NPC into the vehicle we created for it.
I did this using OnPlayerSpawn...
NOTE: If your NPC is not in a vehicle you can skip this step.
pawn Code:
public OnPlayerSpawn(playerid)
{
  if(IsPlayerNPC(playerid)) //Checks if the player that just spawned is an NPC.
  {
    new npcname[MAX_PLAYER_NAME];
    GetPlayerName(playerid, npcname, sizeof(npcname)); //Getting the NPC's name.
    if(!strcmp(npcname, "MyFirstNPC", true)) //Checking if the NPC's name is MyFirstNPC
    {
      PutPlayerInVehicle(playerid, MyFirstNPCVehicle, 0); //Putting the NPC into the vehicle we created for it.
    }
    return 1;
  }
  //Other stuff for normal players goes here!
  return 1;
}
If you want to add another NPC, this OnPlayerSpawn structure is easy to modify, here is an example for 2 NPCs...

pawn Code:
public OnPlayerSpawn(playerid)
{
  if(IsPlayerNPC(playerid)) //Checks if the player that just spawned is an NPC.
  {
    new npcname[MAX_PLAYER_NAME];
    GetPlayerName(playerid, npcname, sizeof(npcname)); //Getting the NPC's name.
    if(!strcmp(npcname, "MyFirstNPC", true)) //Checking if the NPC's name is MyFirstNPC
    {
      PutPlayerInVehicle(playerid, MyFirstNPCVehicle, 0); //Putting the NPC into the vehicle we created for it.
      return 1;
    }
    if(!strcmp(npcname, "MySecondNPC", true))
    {
      PutPlayerInVehicle(playerid, AnotherVehicleID, 0);
      return 1;
    }
    return 1;
  }
  //Other stuff for normal players goes here!
  return 1;
}
Now, compile your gamemode or filterscript, and go in-game!
With any luck, you will see your NPC driving around the path you set for it, congratulations! You have made your first NPC!
Didn't work? Post here to see if we can troubleshoot your problem.
Any more questions? Again, post in this thread!

Common problems

My NPC leaves my server after it joins
Your script is forcing the NPC to login, or you have an anti-cheat / ping kicker that is interfering with your NPC. You can add...
pawn Code:
if(IsPlayerNPC(playerid)) return 1;
... as the first line of any callbacks that bots will use, e.g. OnPlayerRequestClass or OnPlayerRequestSpawn. For an efficient, simple way of excluding NPCs from loops, check out foreach by ******.

My NPC doesn't join my server at all
This is most likely caused by your server being passworded.

I can't get the npc_record filterscript to load
You either don't have the filterscript, or it is already loaded. Try continuing anyway.

My NPC just stands at the spawnpoint
Go back, and re-do the OnPlayerSpawn part of the tutorial.

My NPCs used to connect, but now I have upgraded to >= RC5 they don't.
There is a new maxnpc server.cfg variable that defaults to 0. add...
Code:
maxnpc number_goes_here
...to your server.cfg to fix this issue.
Reply


Messages In This Thread
Creating a new NPC - by kc - 02.09.2009, 11:10
Re: Tutorial -> Creating a new NPC - by CL15 - 02.09.2009, 11:26
Re: Tutorial -> Creating a new NPC - by Moustafa - 02.09.2009, 11:27
Re: Tutorial -> Creating a new NPC - by [tag]lugia - 02.09.2009, 11:28
Re: Tutorial -> Creating a new NPC - by kc - 02.09.2009, 11:30
Re: Tutorial -> Creating a new NPC - by Shubham - 02.09.2009, 11:51
Re: Tutorial -> Creating a new NPC - by Justsmile - 02.09.2009, 12:08
Re: Tutorial -> Creating a new NPC - by kc - 02.09.2009, 12:15
Re: Tutorial -> Creating a new NPC - by Justsmile - 02.09.2009, 12:18
Re: Tutorial -> Creating a new NPC - by kc - 02.09.2009, 12:30
Re: Tutorial -> Creating a new NPC - by Justsmile - 02.09.2009, 12:36
Re: Tutorial -> Creating a new NPC - by kc - 02.09.2009, 12:39
Re: Tutorial -> Creating a new NPC - by Justsmile - 02.09.2009, 12:49
Re: Tutorial -> Creating a new NPC - by Justsmile - 02.09.2009, 13:11
Re: Tutorial -> Creating a new NPC - by Justsmile - 02.09.2009, 13:23
Re: Tutorial -> Creating a new NPC - by dugi - 02.09.2009, 13:24
Re: Tutorial -> Creating a new NPC - by kc - 02.09.2009, 13:26
Re: Tutorial -> Creating a new NPC - by Justsmile - 02.09.2009, 13:28
Re: Tutorial -> Creating a new NPC - by kc - 04.09.2009, 18:46
Re: Tutorial -> Creating a new NPC - by Cedimedi - 04.09.2009, 18:52
Re: Tutorial -> Creating a new NPC - by ft941 - 04.09.2009, 18:59
Re: Tutorial -> Creating a new NPC - by SFather - 04.09.2009, 19:57
Re: Tutorial -> Creating a new NPC - by kc - 04.09.2009, 20:00
Re: Tutorial -> Creating a new NPC - by eXchainZ-FoReVeR - 04.09.2009, 21:27
Re: Tutorial -> Creating a new NPC - by SFather - 04.09.2009, 22:13
Re: Tutorial -> Creating a new NPC - by CaLaP - 04.09.2009, 22:24
Re: Tutorial -> Creating a new NPC - by Bcklup - 05.09.2009, 03:32
Re: Tutorial -> Creating a new NPC - by JaTochNietDan - 05.09.2009, 03:32
Re: Tutorial -> Creating a new NPC - by Bcklup - 05.09.2009, 03:37
Re: Tutorial -> Creating a new NPC - by Zeex - 05.09.2009, 06:12
Re: Tutorial -> Creating a new NPC - by Bcklup - 05.09.2009, 06:43
Re: Tutorial -> Creating a new NPC - by Cedimedi - 05.09.2009, 07:46
Re: Tutorial -> Creating a new NPC - by ft941 - 05.09.2009, 08:36
Re: Tutorial -> Creating a new NPC - by Whiteagle - 05.09.2009, 08:40
Re: Tutorial -> Creating a new NPC - by Cedimedi - 05.09.2009, 08:53
Re: Tutorial -> Creating a new NPC - by _Ben_[Fux-c.co.il RP] - 05.09.2009, 09:15
Re: Tutorial -> Creating a new NPC - by kc - 05.09.2009, 09:55
Re: Tutorial -> Creating a new NPC - by XxerykxX - 05.09.2009, 11:19
Re: Tutorial -> Creating a new NPC - by Youtube - 29.11.2009, 09:36
Re: Tutorial -> Creating a new NPC - by Rac3r - 30.11.2009, 09:27
Re: Tutorial -> Creating a new NPC - by Youtube - 30.11.2009, 12:53
Re: Tutorial -> Creating a new NPC - by Rac3r - 30.11.2009, 13:26
Re: Tutorial -> Creating a new NPC - by Youtube - 30.11.2009, 13:44
Re: Tutorial -> Creating a new NPC - by Franjdea - 30.11.2009, 16:59
Re: Tutorial -> Creating a new NPC - by DJDhan - 03.12.2009, 18:03
Re: Tutorial -> Creating a new NPC - by Joe Staff - 03.12.2009, 22:13
Re: Tutorial -> Creating a new NPC - by DJDhan - 04.12.2009, 08:44
Re: Tutorial -> Creating a new NPC - by DJDhan - 04.12.2009, 08:47
Re: Tutorial -> Creating a new NPC - by HydraX - 02.01.2010, 03:49
Re: Tutorial -> Creating a new NPC - by DRIFT_HUNTER - 03.01.2010, 14:45
Re: Tutorial -> Creating a new NPC - by FragMonkey - 03.01.2010, 22:20
Re: Tutorial -> Creating a new NPC - by rs2fun111 - 03.01.2010, 23:40
Re: Tutorial -> Creating a new NPC - by Mauri - 05.01.2010, 00:37
Re: Tutorial -> Creating a new NPC - by gozo12 - 09.01.2010, 16:18
Re: Tutorial -> Creating a new NPC - by braduz - 10.01.2010, 16:18
Re: Tutorial -> Creating a new NPC - by Bearfist - 14.01.2010, 20:27
Re: Tutorial -> Creating a new NPC - by braduz - 14.01.2010, 20:29
Re: Tutorial -> Creating a new NPC - by Bearfist - 14.01.2010, 20:52
Re: Tutorial -> Creating a new NPC - by selten98 - 08.07.2010, 08:16
Re: Creating a new NPC - by GangsTa_ - 10.07.2010, 19:10
Re: Creating a new NPC - by samuel_hamza - 14.07.2010, 13:53
Re: Creating a new NPC - by w84m3 - 07.01.2011, 14:29
Re: Creating a new NPC - by Scenario - 07.01.2011, 14:30
Re: Creating a new NPC - by Infamous - 07.01.2011, 14:31
Re: Creating a new NPC - by Dainyzxz - 07.01.2011, 18:40
Re: Creating a new NPC - by n00bSklly - 14.03.2011, 17:35
Respuesta: Creating a new NPC - by Host-samp - 01.04.2011, 20:58
Re: Creating a new NPC - by OBSERVADOR - 03.04.2011, 05:17
Respuesta: Creating a new NPC - by Lunnatiicz - 03.04.2011, 08:35
Re: Creating a new NPC - by aceisnice - 12.04.2011, 06:23
Respuesta: Creating a new NPC - by Alex_Obando - 18.04.2011, 17:35
Re: Creating a new NPC - by Luis- - 07.05.2011, 16:30
Re: Creating a new NPC - by Miguel - 11.05.2011, 00:46
Re: Creating a new NPC - by jonnyboy - 11.05.2011, 05:22
Re: Creating a new NPC - by sansko - 11.05.2011, 05:39
Re: Creating a new NPC - by jonnyboy - 12.05.2011, 05:52
Re: Creating a new NPC - by mu057677 - 12.05.2011, 12:57
Re: Creating a new NPC - by mu057677 - 12.05.2011, 15:43
Re: Creating a new NPC - by jonnyboy - 12.05.2011, 17:00
Re: Creating a new NPC - by mu057677 - 13.05.2011, 03:26
Re: Creating a new NPC - by Beastlynoob - 30.05.2011, 02:13
Re: Creating a new NPC - by buster_ - 30.05.2011, 18:39
Re: Creating a new NPC - by Robert_Crawford - 03.06.2011, 02:52
Re: Creating a new NPC - by abubaker98 - 06.06.2011, 15:44
Re: Creating a new NPC - by Gagi_Corleone - 07.06.2011, 15:06
Re: Creating a new NPC - by L84S - 25.06.2011, 19:44
Re: Creating a new NPC - by Setkus - 15.08.2011, 19:45
Re: Creating a new NPC - by Setkus - 15.08.2011, 20:14
Re: Creating a new NPC - by sansko - 15.08.2011, 20:16
Re: Creating a new NPC - by Setkus - 15.08.2011, 20:17
AW: Creating a new NPC - by FaiSokumati - 16.08.2011, 08:28
Re: Creating a new NPC - by jat1801 - 17.08.2011, 02:06
Re: Creating a new NPC - by Setkus - 17.08.2011, 18:51
Re: Creating a new NPC - by Setkus - 17.08.2011, 18:59
Re: Creating a new NPC - by Macros50 - 23.08.2011, 12:30
Re: Creating a new NPC - by System64 - 23.08.2011, 13:23
Re: Creating a new NPC - by Macros50 - 24.08.2011, 06:24
Re: Creating a new NPC - by sherlock - 30.08.2011, 12:16
Re: Creating a new NPC - by bomm89 - 04.09.2011, 01:48
Re: Creating a new NPC - by whitedragon - 04.09.2011, 10:04
Re: Creating a new NPC - by DeadDon - 06.09.2011, 13:31
Re: Creating a new NPC - by MicroD - 17.09.2011, 17:14
Re: Creating a new NPC - by loganout - 18.09.2011, 15:15
Re: Creating a new NPC - by manchestera - 16.10.2011, 16:00
Re: Creating a new NPC - by whitedragon - 16.10.2011, 16:06
Re: Creating a new NPC - by manchestera - 16.10.2011, 16:22
Re: Creating a new NPC - by Jagofr - 16.10.2011, 18:27
Re: Creating a new NPC - by cowmilker69 - 23.04.2012, 01:55
Re: Creating a new NPC - by stormchaser206 - 04.05.2012, 00:28
Re: Creating a new NPC - by adelechevarria - 04.05.2012, 16:36
Re: Creating a new NPC - by faff - 13.05.2012, 14:16
Re: Creating a new NPC - by stormchaser206 - 14.05.2012, 00:48
Re: Creating a new NPC - by Suraj - 14.05.2012, 01:02
Re: Creating a new NPC - by stormchaser206 - 15.05.2012, 21:23
Re: Creating a new NPC - by maverick378 - 16.05.2012, 14:52
Re: Creating a new NPC - by JEkvall95 - 11.06.2012, 18:06
Re: Creating a new NPC - by JEkvall95 - 13.06.2012, 12:45
Re: Creating a new NPC - by x96664 - 14.11.2012, 14:24
Re: Creating a new NPC - by [KHK]Khalid - 14.11.2012, 15:09
Re: Creating a new NPC - by romka - 06.12.2012, 06:58
Re: Creating a new NPC - by Y_Less - 06.12.2012, 07:05
Re: Creating a new NPC - by Xlithan - 05.01.2013, 18:41
Re: Creating a new NPC - by FalconCRO - 22.02.2013, 19:23
Re: Creating a new NPC - by xX4m4zingXx - 24.02.2013, 20:06
Re: Creating a new NPC - by xX4m4zingXx - 04.04.2013, 18:49
Re: Creating a new NPC - by Craker - 22.04.2013, 12:41
Re: Creating a new NPC - by davve95 - 22.04.2013, 13:25
Re: Creating a new NPC - by ClubU295 - 21.03.2014, 19:53
Re: Creating a new NPC - by iZN - 21.03.2014, 20:17
Re: Creating a new NPC - by GShock - 24.03.2014, 17:18
Re: Creating a new NPC - by DaYviSoN - 17.04.2014, 21:01
Re: Creating a new NPC - by lonalovegood1 - 26.04.2014, 11:38
Re: Creating a new NPC - by satafinix - 01.05.2014, 05:36
Re: Creating a new NPC - by mehdimmz - 25.10.2014, 15:21
Re: Creating a new NPC - by Abagail - 26.10.2014, 00:36
Re: Creating a new NPC - by Pottus - 26.10.2014, 02:06
Re: Creating a new NPC - by Salvator_Salieri - 26.11.2014, 01:19
Re: Creating a new NPC - by Arastair - 27.11.2014, 14:08
Re: Creating a new NPC - by Luis- - 27.11.2014, 14:16
Re: Creating a new NPC - by cyberlord - 10.12.2014, 11:34
Re: Creating a new NPC - by danish007 - 13.12.2014, 16:43
Re: Creating a new NPC - by bogushas - 02.01.2015, 18:31
Re: Creating a new NPC - by Glossy42O - 02.01.2015, 21:30
Re: Creating a new NPC - by Arxalan - 04.01.2015, 14:56
Re: Creating a new NPC - by dan40o - 10.01.2015, 17:05
Re: Creating a new NPC - by Wipe - 10.01.2015, 22:27
How to get unlimited nitrogen - by oppo1234 - 13.01.2015, 08:21
Re: Creating a new NPC - by Devil143 - 06.03.2015, 12:01
Re: Creating a new NPC - by edwardluciano - 15.03.2015, 23:52
Re: Creating a new NPC - by Pearson - 07.06.2015, 17:43
Re: Creating a new NPC - by ammaz - 16.07.2015, 03:17
Re: Creating a new NPC - by MohammedZ - 09.08.2015, 13:46
Re: Creating a new NPC - by Gotham - 08.09.2016, 10:03
Re: Creating a new NPC - by FreAkeD - 08.09.2016, 10:57
need help with the second and therd npc - by yonik1988 - 14.03.2018, 19:47
Re: Creating a new NPC - by yonik1988 - 30.06.2018, 22:57
Re: Creating a new NPC - by yonik1988 - 01.07.2018, 16:04

Forum Jump:


Users browsing this thread: 1 Guest(s)