What's New in Director 8.5 > Multiuser Server-Side Scripting > Adding server-side scripts

 

Adding server-side scripts

Adding server-side scripting functionality to a movie requires that you create a script file for your movie and edit the Scriptmap.ls file.

To add a script to the server for your movie:

1

Make a text file of the script, name the file and give it an "ls" extension. This script should contain handlers for each server event you want the script to react to, plus any custom events you define.

2

Place the file in the server's scripts folder.

3

Open the Scriptmap.ls file in a text editor and add a line to it with the following syntax:

theMap.append( [movieID: "yourMovieID", scriptFileName: "yourScriptFileName"] )

This line should be added immediately after the last line that contains the same syntax. The string yourMovieID is the movie ID your movie will use when logging into the server with connectToNetServer(). The string yourScriptFileName is the actual file name of your movie's script file that you placed into the server's scripts folder.

For example, to add the file TestScript.ls to the Scriptmap.ls file and associate it with the movie ID TestMovie, you might start with a Scriptmap.ls file like this:

on scriptMap
	theMap = []

	theMap.append( [ #movieID: "BlackJack*", #scriptFileName: \
	"BlackJack.ls" ] )
	theMap.append( [ #movieID: "SimpleChat", #scriptFileName: \
	"SimpleChat.ls" ] )

	return theMap
end

Then you would add a line to it like the one in the following:

on scriptMap
	theMap = []

	theMap.append( [ #movieID: "BlackJack*", #scriptFileName: \
	"BlackJack.ls" ] )
	theMap.append( [ #movieID: "SimpleChat", #scriptFileName: \
	"SimpleChat.ls" ] )
	-- This is the new line
	theMap.append( [ #movieID: "TestMovie", #scriptFileName: \
	"TestScript.ls" ] )

	return theMap
end

Note that the append commands at the beginning of each line cause the lists to be combined, resulting in a list of lists that Lingo interprets like this:

[ [ movieID: "BlackJack*", scriptFileName: "BlackJack.ls" ], 		[movieID: "SimpleChat", scriptFileName: "SimpleChat.ls" ], 		[movieID: "TestMovie", scriptFileName: "TestScript.ls" ] 	]

If you want to associate a server-script with a specific group within a movie, add a #groupID property to the list. This will cause only messages sent from members of that group to be forwarded to the specified script.

theMap.append( [#movieID: "TestMovie", #groupID: "@chatUsers",\
#scriptFileName:	 "TestScript.ls"] )

4

Restart the server.

You can also make the server reload the Scriptmap.ls file without restarting by having a movie send a message to the server with a subject of System.Script.Admin.Reload. In order for this to work, the case statement in the Dispatcher.ls file's incomingMessage handler must be uncommented.

The following excerpt of the Dispatcher script's on incomingMessage handler contains the case statement:

-- These commands may be useful during development; be sure to
	-- disable them for a production server
	--
	--  case subject of 
	--    "System.Script.Admin.Reload":
	--      put "LingoVM: reloading all scripts."
	--        tlist = thread().list
	--        repeat with t in tlist
	--          t.forget()
	--        end repeat
	--      the timeoutList = []
	--      me.loadScriptMap()
	--      exit
	--    "System.Script.Admin.Ping": 
	--      sender.sendMessage(subject, msg)
	--      exit
	--    "System.Script.Admin.ShowState":
	--      showServerState()
	--      exit
		--  end case

Note that the entire case statement is commented so that it will not be active by default. Reloading the Scriptmap.ls file will abort any script threads in progress. See Multithreading.

When adding server-side scripts, you can choose to associate several movie IDs with a single script file or several script files with a single movie ID, depending on the requirements of your multiuser application. The asterisk (*) after the #movieID BlackJack indicates that any movie ID beginning with the string BlackJack will be associated with the BlackJack.ls file. The Dispatcher script's on wildCompare handler resolves movie IDs beginning with the given string and associates them with the correct script file.