_server.createNpc()

Availability:

SmartFoxServer PRO 1.6.5

Usage:

_server.createNpc(userName, ipAdr, port, zoneName)

Description:

Creates a Non-Player-Character (NPC), a User which is not driven by a real client but can be governed by server side code. In order to be able to create NPCs you will need to first enable them via the <EnableNPC>true</EnableNPC> setting in your config.xml file.

NPCs need to be connected to a physical socket end in order to "trick" the server into thinking that it is a real connected client. This is done behind the scenes by running a connection to the localhost, so if you take a look at the Admin Tool you will notice that NPC connections all come from 127.0.0.1

NPCs are subject to the MaxIdleUserTime policy as any other connected users, so you will need to start a thread or add a Scheduled task (recommended) that invokes the updateMessageTime() method on each non-player character to avoid disconnections.

KNOWN PROBLEMS:
There is a known bug related to the creation of non-player characters (NPC). We have noticed that all connections coming from NPCs remain in CLOSE_WAIT state after closing them. This is not an issue if NPCs are created once and then used for the entire life cycle of the server instance. However if you plan to dynamically create and destroy many of them it could eventually exhaust the number of sockets/file-descriptors available of the underlying OS.

Properties:

userName   provide a user name for this NPC User
ipAdr   The IP address of the Server. The NPC will use a real connection to the Server, so that it can receive events and behave exactly like any other User in the system. In order to obtain best performance it is suggest to always connecte NPC Users to the localhost (127.0.0.1)
port   The Server TCP port
zoneName   The name of the Zone where the NPC User should be logged in.

Returns:

the connected User Object

Example #1:

Create a room with a max of 25 users

	var theNpcUser = _server.createNPC("TestNPC", "127.0.0.1", 9339, "openspace") 

 

See also: