Here is a useful stand-alone zone extension to manage intervals and timed tasks. Keeping a centralized single Scheduler will create and use only 1 thread in the java vm, preventing performance problems.
Just put it in the sfsExtensions folder (together with the "uty" folder, which contains System.as, used by ScheduleMan).
Download ScheduleMan
Usage:
Create the zone level class by adding to your ZONE in config.xml:
Code: Select all
<extension name="ScheduleMan" className="ScheduleMan.as" type="script" />The task will call timed a function in any extension. The onlyargument in the function will be an instance of "MyTask", from which all sent properties can be accessible, and also a property "task" with the real instance of the java Task.
The MyTask instance, also has a method stop(), that will stop the task from running, and remove all references cleaning up for garbage collection.
You have to make an extension call.
For simple tasks do the following:
Code: Select all
_server.getCurrentZone().getExtension("ScheduleMan").handleInternalRequest({cmd:"createTask", par:params});Code: Select all
var params = {
id:"My Task", // String with any ID of your choice"
interval:3, // Number of seconds to wait to perform the task
recurrent:true, // If task has to be recurrent. If false, it happens only once
executions:10, // number of times the task has to be executed if recurrent
func:myFunction, // Direct reference to a method that the task will call on any execution
par:{} // This can be anything. Any extra parameter to be stored
}For room tasks do the following:
Room tasks are designed to happen in rooms that can be dynamically created and destroyed. If room is bound to be destroyed, task will kill itself automatically.
Code: Select all
_server.getCurrentZone().getExtension("ScheduleMan").handleInternalRequest({cmd:"createGameRoomTask", par:params});Code: Select all
room:Room // Direct reference to the Room instance
roomExt:this // (optional) direct reference to the room extensionIn order to inhibit task action and cleanup, task can notice if room is inactive using the property "roomState". If roomState == "DESTROYED", task knows room doesn't need it anymore.
you can add you custom code to detect if room is bound to be destroyed in line 129 of ScheduleMas.as, in the function gameRoomIsInactive(room)
I wish I had found this myself long time ago
namaste
Filippo
PS
You can also take advantage of the System.as class, very very very useful in any extension. It provides a lot of useful methods and speeds up SFS server side development a lot...
I.E.
_system.sendMessage(recipient, cmd, par);
recipient can be:
"ROOM" - Will send to all users presents in the actual room
"ZONE" - Will send to all usersin the zone
User - A single user object
[User, User, User] - An array of users
channel - A single user channel (useful for custom login)...
Etc... explore it yourself
I will make another post about it...