12.08.2011, 12:04
This tutorial shows how you can send commands to NPC.
To send a command from gamemode/filterscript to NPC, you need to call a function in the npcmode. Normally you would use CallRemoteFunction. But CallRemoteFunction doesn't work for npcmodes, so you have to use something else, for example sending client messages.
Basically the method works like this:
(gamemode/filterscript) SendClientMessage > OnClientMessage (npcmode)
In gamemode/filterscript you use the format function to pack the data to a string and send it with SendClientMessage function.
In npcmode you use OnClientMessage callback to receive the data and unpack it with sscanf (for example).
With this method you can send ANY data you want from server to NPC.
Now I will show an example using zcmd and sscanf
gamemode/filterscript:
npcmode:
Now if you have NPC connected with ID 0 and you have a /me command in your script, you can use /sendcmd command to make the NPC use the /me command.
Example:
Or you can make the NPC play any recording file in ...\npcmodes\recordings\ folder.
Example:
zcmd - https://sampforum.blast.hk/showthread.php?tid=91354
sscanf - https://sampforum.blast.hk/showthread.php?tid=120356
To send a command from gamemode/filterscript to NPC, you need to call a function in the npcmode. Normally you would use CallRemoteFunction. But CallRemoteFunction doesn't work for npcmodes, so you have to use something else, for example sending client messages.
Basically the method works like this:
(gamemode/filterscript) SendClientMessage > OnClientMessage (npcmode)
In gamemode/filterscript you use the format function to pack the data to a string and send it with SendClientMessage function.
In npcmode you use OnClientMessage callback to receive the data and unpack it with sscanf (for example).
With this method you can send ANY data you want from server to NPC.
Now I will show an example using zcmd and sscanf
gamemode/filterscript:
pawn Code:
CMD:sendcmd(playerid, params[])
{
new npcid, cmd[128], msg[128];
if(sscanf(params, "us[128]", npcid, cmd)) return SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: /sendcmd [npc] [cmd]");
if(!IsPlayerConnected(npcid)) return SendClientMessage(playerid, 0xEE0000FF, "Invalid player!");
if(!IsPlayerNPC(npcid)) return SendClientMessage(playerid, 0xEE0000FF, "Player is not NPC!");
format(msg, sizeof(msg), "/sendcmd %s", cmd);
SendClientMessage(npcid, 123456, msg);
return 1;
}
CMD:playback(playerid, params[])
{
new npcid, playbacktype, recordname[64], msg[128];
if(sscanf(params, "uds[64]", npcid, playbacktype, recordname))
return SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: /playback [npc] [type] [name]");
if(!IsPlayerConnected(npcid)) return SendClientMessage(playerid, 0xEE0000FF, "Invalid player!");
if(!IsPlayerNPC(npcid)) return SendClientMessage(playerid, 0xEE0000FF, "Player is not NPC!");
format(msg, sizeof(msg), "/playback %d %s", playbacktype, recordname);
SendClientMessage(npcid, 123456, msg);
return 1;
}
pawn Code:
public OnClientMessage(color, text[])
{
if(text[0] != '/' || color != 123456) return; // ignore all other client messages that NPC receives
new cmd[64], params[128];
sscanf(text, "s[64]s[128]", cmd, params);
if(strcmp(cmd, "/sendcmd", true) == 0)
{
SendCommand(params);
return;
}
if(strcmp(cmd, "/playback", true) == 0)
{
new playbacktype, recordname[64], msg[128];
sscanf(params, "ds[64]", playbacktype, recordname);
format(msg, sizeof(msg), "Starting playback (type: %d name: %s)", playbacktype, recordname);
SendChat(msg);
StartRecordingPlayback(playbacktype, recordname);
return;
}
}
Example:
Code:
/sendcmd 0 /me is a bot
Example:
Code:
/playback 0 2 animtest1
sscanf - https://sampforum.blast.hk/showthread.php?tid=120356