02.09.2009, 11:10
(
Last edited by dugi; 08/07/2010 at 11:22 PM.
)
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...
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...
That may look confusing, so I will now go through it line by line.
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"
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.
Including the SA-MP NPC functions.
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.
This is another callback included in the NPC library. This will start the recording when the NPC has been put in a vehicle.
Yet another callback included in the NPC library, this will stop playing back the recording if for any reason the NPC exits the vehicle.
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...
Or, if you are adding the NPC into a filterscript...
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.
Or, if you are adding the NPC into a filterscript...
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.
If you want to add another NPC, this OnPlayerSpawn structure is easy to modify, here is an example for 2 NPCs...
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...
... 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...
...to your server.cfg to fix this issue.
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.
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
pawn Code:
#define RECORDING "mynpc" //This is the filename of your recording without the extension.
pawn Code:
#define RECORDING_TYPE 1 //1 for in vehicle and 2 for on foot.
pawn Code:
#include <a_npc>
pawn Code:
public OnRecordingPlaybackEnd() StartRecordingPlayback(RECORDING_TYPE, RECORDING);
pawn Code:
public OnNPCEnterVehicle(vehicleid, seatid) StartRecordingPlayback(RECORDING_TYPE, RECORDING);
pawn Code:
public OnNPCExitVehicle() StopRecordingPlayback();
pawn Code:
public OnNPCSpawn() StartRecordingPlayback(RECORDING_TYPE, RECORDING);
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.
pawn Code:
public OnGameModeInit()
{
print("my gamemode");
ConnectNPC("MyFirstNPC","mynpc");
return 1;
}
pawn Code:
public OnFilterScriptInit()
{
print("my filterscript");
ConnectNPC("MyFirstNPC","mynpc");
return 1;
}
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;
}
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;
}
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;
}
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;
}
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;
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