SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 02/09/2009, 11:10 AM   #1
kc
Gangsta
 
kc's Avatar
 
Join Date: Aug 2007
Posts: 618
Reputation: 115
Default Creating a new NPC

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.

Last edited by dugi; 08/07/2010 at 11:22 PM.
kc is offline   Reply With Quote
Old 02/09/2009, 11:26 AM   #2
CL15
Little Clucker
 
Join Date: Oct 2007
Location: Austria
Posts: 5
Reputation: 0
Default Re: Tutorial -> Creating a new NPC

nice tutorial.
CL15 is offline   Reply With Quote
Old 02/09/2009, 11:27 AM   #3
Moustafa
Big Clucker
 
Join Date: Jun 2008
Posts: 165
Reputation: 1
Default Re: Tutorial -> Creating a new NPC

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.
Moustafa is offline   Reply With Quote
Old 02/09/2009, 11:28 AM   #4
[tag]lugia
Little Clucker
 
Join Date: Feb 2009
Posts: 3
Reputation: 0
Default Re: Tutorial -> Creating a new NPC

npc_record what fs
[tag]lugia is offline   Reply With Quote
Old 02/09/2009, 11:30 AM   #5
kc
Gangsta
 
kc's Avatar
 
Join Date: Aug 2007
Posts: 618
Reputation: 115
Default Re: Tutorial -> Creating a new NPC

Thanks for the comments

Quote:
Originally Posted by [tag
lugia ]
npc_record what fs
It is included in the 0.3 scripting package.
kc is offline   Reply With Quote
Old 02/09/2009, 11:51 AM   #6
Shubham
Big Clucker
 
Shubham's Avatar
 
Join Date: Apr 2009
Location: India
Posts: 84
Reputation: -1
Default Re: Tutorial -> Creating a new NPC

this helped me alot thanks
Shubham is offline   Reply With Quote
Old 02/09/2009, 12:08 PM   #7
Justsmile
Huge Clucker
 
Join Date: Dec 2008
Posts: 211
Reputation: 0
Default Re: Tutorial -> Creating a new NPC

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
Justsmile is offline   Reply With Quote
Old 02/09/2009, 12:15 PM   #8
kc
Gangsta
 
kc's Avatar
 
Join Date: Aug 2007
Posts: 618
Reputation: 115
Default Re: Tutorial -> Creating a new NPC

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?
kc is offline   Reply With Quote
Old 02/09/2009, 12:18 PM   #9
Justsmile
Huge Clucker
 
Join Date: Dec 2008
Posts: 211
Reputation: 0
Default Re: Tutorial -> Creating a new NPC

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

Pls help.
Justsmile is offline   Reply With Quote
Old 02/09/2009, 12:30 PM   #10
kc
Gangsta
 
kc's Avatar
 
Join Date: Aug 2007
Posts: 618
Reputation: 115
Default Re: Tutorial -> Creating a new NPC

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.
kc is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
need help with NPC creating the_zande Help Archive 2 09/07/2010 11:44 PM
help creating a new gm Lookin Help Archive 8 16/06/2010 03:23 PM
I need a tut on creating kujox32 Help Archive 0 05/05/2010 02:16 AM
Creating Box VirSpectorX Help Archive 5 15/04/2010 02:47 PM
Help creating my own... x.X.LEGENDARIO.X.x Everything and Nothing 20 06/02/2010 03:40 PM


All times are GMT. The time now is 09:00 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.