SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 12/08/2011, 12:04 PM   #1
MadeMan
High-roller
 
MadeMan's Avatar
 
Join Date: Jun 2007
Posts: 3,472
Reputation: 237
Default Sending commands to NPC

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:
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;
}

npcmode:
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;
    }
}

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:
Code:
/sendcmd 0 /me is a bot
Or you can make the NPC play any recording file in ...\npcmodes\recordings\ folder.

Example:
Code:
/playback 0 2 animtest1

zcmd - http://forum.sa-mp.com/showthread.php?t=91354
sscanf - http://forum.sa-mp.com/showthread.php?t=120356
MadeMan is offline   Reply With Quote
Old 12/08/2011, 12:40 PM   #2
[HiC]TheKiller
High-roller
 
Join Date: Mar 2008
Location: New Zealand
Posts: 3,075
Reputation: 406
Default Re: Sending commands to NPC

I don't really understand why you are sending a message to the NPC. Why don't you just call the ZCMD function or even the strcmp function directly from the command.

pawn Code:
CMD:sendcmd(playerid, params[])
{
    new npcid, cmd[128], msg[128], str[150];
    if(sscanf(params, "us[128]s[150]", npcid, cmd, str)) return SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: /sendcmd [npc] [cmd] [params]");
    if(!IsPlayerConnected(npcid)) return SendClientMessage(playerid, 0xEE0000FF, "Invalid player!");
    if(!IsPlayerNPC(npcid)) return SendClientMessage(playerid, 0xEE0000FF, "Player is not NPC!");
    format(msg, sizeof(msg), "cmd_%s", cmd);
    CallRemoteFunction(msg, "is", npcid, str);
    return 1;
}

Anyway, a little explanation would go a long way .
__________________
People seem to enjoy negitive repping me for topics that I made in 2009.
Therefore, if you want to view my releases, you can go here.

Skype: TheKiller@dr.com
I do NOT script for cash. Add me on MSN if you need help with small issues.
[HiC]TheKiller is offline   Reply With Quote
Old 12/08/2011, 02:09 PM   #3
MadeMan
High-roller
 
MadeMan's Avatar
 
Join Date: Jun 2007
Posts: 3,472
Reputation: 237
Default Re: Sending commands to NPC

Quote:
Originally Posted by [HiC]TheKiller View Post
I don't really understand why you are sending a message to the NPC. Why don't you just call the ZCMD function or even the strcmp function directly from the command.

pawn Code:
CMD:sendcmd(playerid, params[])
{
    new npcid, cmd[128], msg[128], str[150];
    if(sscanf(params, "us[128]s[150]", npcid, cmd, str)) return SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: /sendcmd [npc] [cmd] [params]");
    if(!IsPlayerConnected(npcid)) return SendClientMessage(playerid, 0xEE0000FF, "Invalid player!");
    if(!IsPlayerNPC(npcid)) return SendClientMessage(playerid, 0xEE0000FF, "Player is not NPC!");
    format(msg, sizeof(msg), "cmd_%s", cmd);
    CallRemoteFunction(msg, "is", npcid, str);
    return 1;
}

Anyway, a little explanation would go a long way .
Read carefully.

Quote:
Originally Posted by MadeMan View Post
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.
MadeMan is offline   Reply With Quote
Old 13/08/2011, 12:10 AM   #4
[HiC]TheKiller
High-roller
 
Join Date: Mar 2008
Location: New Zealand
Posts: 3,075
Reputation: 406
Default Re: Sending commands to NPC

I misunderstood the topic. I thought that you meant applying a current ZCMD command to a bot. This is for sending SendCommand / SendChat messages. It still would be nice to have a bit more explanation to why you do what you do.
__________________
People seem to enjoy negitive repping me for topics that I made in 2009.
Therefore, if you want to view my releases, you can go here.

Skype: TheKiller@dr.com
I do NOT script for cash. Add me on MSN if you need help with small issues.
[HiC]TheKiller is offline   Reply With Quote
Old 13/08/2011, 12:22 AM   #5
RealCop228
Godfather
 
Join Date: Feb 2010
Location: Springboro, OH
Posts: 8,268
Reputation: 670
Default Re: Sending commands to NPC

Quote:
Originally Posted by MadeMan View Post
Normally you would use CallRemoteFunction. But CallRemoteFunction doesn't work for npcmodes, so you have to use something else, for example sending client messages.
I'll translate for people:

ZCMD uses "CallRemoteFunction" and that's where it gets its speed. But since you can't use that function with NPC's, then you can't call the ZCMD command directly.
__________________
Putting [URGENT] in the thread title stops me from opening the thread. Nobody else's problem on this forum is more important that another person's. Try not to be self-centered!
RealCop228 is offline   Reply With Quote
Old 04/10/2011, 10:05 AM   #6
iPLEOMAX
High-roller
 
iPLEOMAX's Avatar
 
Join Date: Mar 2011
Location: Al Ain, UAE. Homeland: Bangladesh.
Posts: 1,311
Reputation: 719
Default Re: Sending commands to NPC

Awesome, Thanks for the playback part!
__________________
Proud member & developer at CODE5 Gaming (cod5samp.com)
Los Santos: Cops & Robbers
Resurrection
Total War

*In case you saw me in any other server, it's probably an impersonator or my fan!
iPLEOMAX is offline   Reply With Quote
Old 09/12/2012, 10:52 PM   #7
mastermax7777
Huge Clucker
 
Join Date: Oct 2012
Posts: 428
Reputation: -46
Default Re: Sending commands to NPC

thanks dude..works all who say bump f*ck you! i just wanted to thank the author and help others maybe
mastermax7777 is offline   Reply With Quote
Old 25/03/2013, 09:54 PM   #8
omidi
Big Clucker
 
omidi's Avatar
 
Join Date: Jul 2010
Location: Vanuatu
Posts: 68
Reputation: 0
Default Re: Sending commands to NPC

hi man my code , its not wokring for me what is the problem plz tell me every thing will compile complete but not work in game D:
in game mode
Code:
dcmd_sendcmd(playerid,params[])
{
	#pragma unused params

		new npcid, cmd[128], msg[128];
		 if(!IsPlayerConnected(npcid)) return SendClientMessage(playerid, 0xEE0000FF, "Invalid player!");
        format(msg, sizeof(msg), "/sendcmd %s", cmd);
    	SendClientMessage(npcid, 123456, msg);
    
	return 1;
}
in npc filterscript
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;
    }
	return 1;
}
omidi is offline   Reply With Quote
Old 20/06/2014, 10:20 PM   #9
Tidzii
Big Clucker
 
Tidzii's Avatar
 
Join Date: Sep 2011
Location: France
Posts: 187
Reputation: 1
Default Re: Sending commands to NPC

I puted all in what you said and how you said, In GM and Npcmode ,and in game the /sendcmd works but when i /sendcmd 0 /me is cool , the NPC don't say nothing ... weird
__________________
ONLY !!!!!! 10 Free (.net , .com , .org , .US) Website Hosting !!!



Free cPanel Web Hosting with PHP5/Mysql - no advertising!
Register now: http://www.000webhost.com/764958.html

Earn money for Watching Website for 30sec :
http://wad.ojooo.com/register.php?ref=tidzii
Tidzii 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
problem with sending rcon commands... hazdog Help Archive 16 04/01/2011 07:30 PM
Not sending winners name. :s LifeStyle Help Archive 3 11/09/2010 10:41 AM
Sending name problem LifeStyle Help Archive 2 09/09/2010 01:04 PM
kill streak sending message sending wrong Andy_McKinley Help Archive 0 08/07/2010 12:40 PM
Sending one message instead of 2 Perker12345 Help Archive 4 11/01/2010 01:00 PM


All times are GMT. The time now is 04:03 PM.


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