Scripts:Automatic Announcements

From BF2 Technical Information Wiki
Jump to navigation Jump to search

Ever want to have an some announcement that is automatically periodically displayed on all players' screens? Here's how to do it:

 import bf2
 import bf2.Timer
 import host
 
 interval = 30   # Number of seconds between announcements
 message = "Your announcement message goes here"
 
 def onTimer(data):
     global message
     host.rcon_invoke('game.sayall "%s"' % message)
 
 timer = bf2.Timer(onTimer, interval, 1)
 timer.setRecurring(interval)

To work, this script needs to be a module (in it's own file--for example, "simpleAnnounce.py") which is imported by one of the main BF2 modules or packages (bf2, the Admin module, or the bf2 mod); you can import it from any of these, but because of it's function, it probably makes the most sense to import it from the admin module.

To do this, put simpleAnnounce.py into the directory Battlefield 2 Server/Admin/standard_admin; then, in that same folder, edit the file __init__.py and add the line

 import simpleAnnounce 

(it doesn't matter where you put this line inside __init__.py).

When the Admin Module is initialized, it imports the standard_admin package, which, because of the change you made, will now also import your simpleAnnounce module. When a module is first imported the code in it is run, so simpleAnnounce will create a bf2.Timer object set to fire a TimerEvent every interval seconds, with a callback to the onTimer method. When onTimer gets called every interval seconds, it uses the game.sayall server command to send your message to all players.

This script is very basic--it should be easy to extend it to, for example, rotate one by one through a set of different messages, to read the messages it displays from a configuration file, etc.

--Woody