[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
#2

nice tutorial.
Reply
#3

Anyone could've done it if they went into editing the NPC's pwn file, but useful for noobs who hates to do such a surf-out for how to do it by themselves, well, anyways it is a good tut.
Reply
#4

npc_record what fs
Reply
#5

Thanks for the comments

Quote:
Originally Posted by [tag
lugia ]
npc_record what fs
It is included in the 0.3 scripting package.
Reply
#6

this helped me alot thanks
Reply
#7

Help.

I created recording "rocks_1". Now i Put under "GameModeInit" ConnectBot("MyFirstNPC,"rocks_1"). Under "OnPlayerSpawn" your thinks, only putplayerinvehicle(playerid, lspdcar1, 0);
The Problem is he disconnect after 1-2 seconds.

[14:06:47] [npc:join] MyFirstNPC has joined the server (0:127.0.0.1)
[14:06:47] i (the bot) spawned
[14:06:49] [npcart] MyFirstNPC has left the server (0:2)

Pls help me
Reply
#8

Quote:
Originally Posted by Bl4ckDice
Help.

I created recording "rocks_1". Now i Put under "GameModeInit" ConnectBot("MyFirstNPC,"rocks_1"). Under "OnPlayerSpawn" your thinks, only putplayerinvehicle(playerid, lspdcar1, 0);
The Problem is he disconnect after 1-2 seconds.

[14:06:47] [npc:join] MyFirstNPC has joined the server (0:127.0.0.1)
[14:06:47] i (the bot) spawned
[14:06:49] [npcart] MyFirstNPC has left the server (0:2)

Pls help me
Well disconnect reason 2 is kicked or banned, are you sure there isn't an anti-cheat or ping kick for example kicking the NPC?
Reply
#9

Only Anti-weapon cheat banns player, no ping kick.

Pls help.
Reply
#10

Quote:
Originally Posted by Bl4ckDice
Only Anti-weapon cheat banns player, no ping kick.

Pls help.
Well I know that bots register as having full health and armour, so they need to be excluded from any anti cheats.
Reply
#11

If someone cheats armor, or sth. like that, he will not be kicked, just a message to admins. Maybe, cause of /register and /login? I use a modified GodFather Version, based on the original. Everything else works fine.
Reply
#12

Quote:
Originally Posted by Bl4ckDice
If someone cheats armor, or sth. like that, he will not be kicked, just a message to admins. Maybe, cause of /register and /login? I use a modified GodFather Version, based on the original. Everything else works fine.
If you must login to spawn, then yes that would be the problem.
Reply
#13

No, gf problems, i fixxed it, Register ist not important^^
Reply
#14

Why is "SendCommand" for NPC undefined?
Reply
#15

I need it in my GM.^^
Reply
#16

It can not be used outside of the NPC scripts so you can not use it in your gamemode or filterscript.
Reply
#17

I _think_ that you can call OnPlayerCommandText(npc_id_here, "/command"); in your script to make an NPC perform a command.
Reply
#18

HOw get i the Id of the Bot?
Reply
#19

Quote:
Originally Posted by ft941
Hi! I'm Italian and I'm using a translator



My problem is this:
the car driven by my own ncp disappears and reappears after a few seconds automatically recovering within the NCP. I can not understand why!
A few have reported that problem. I don't know what causes it as of yet.

It could be to do with spawning trains with CreateVehicle and not AddStaticVehicle though.

Quote:
Originally Posted by Cedimedi
All should be correct, but it don't work, the player stays still at the spawnpoint!!
Do you have a file called HydraPilot.rec in your /npcmodes/recordings/ directory?

Is the player sitting at the spawnpoint in the vehicle, or on foot?
Reply
#20

Yes i have, and yes its in a vehicle.
Reply


Forum Jump:


Users browsing this thread: 13 Guest(s)