2017-06-02 15:14:52 +00:00
/**
Schedule . c
Schedule can be used to execute scripts or functions repetitively with delay .
@ author
*/
2010-02-18 23:49:27 +00:00
2010-03-30 13:33:21 +00:00
// Executes a script repetitively with delay.
2017-10-20 22:10:42 +00:00
// documented in /docs/sdk/script/fn
2011-07-31 17:27:26 +00:00
global func Schedule ( object obj , string script , int interval , int repeats )
2009-12-29 13:44:16 +00:00
{
2010-03-30 13:33:21 +00:00
// Defaults.
2010-09-13 15:27:48 +00:00
if ( ! repeats )
2010-03-30 13:33:21 +00:00
repeats = 1 ;
// Create effect.
2017-06-02 15:14:52 +00:00
var fx = AddEffect ( " IntSchedule " , obj , 1 , interval , obj ) ;
if ( ! fx )
2010-03-30 13:33:21 +00:00
return false ;
// Set variables.
2017-06-02 15:14:52 +00:00
fx . Script = script ;
fx . Repeats = repeats ;
2010-03-30 13:33:21 +00:00
return true ;
2009-12-29 13:44:16 +00:00
}
2017-06-02 15:14:52 +00:00
global func FxIntScheduleTimer ( object obj , effect fx )
2009-12-29 13:44:16 +00:00
{
2010-03-30 13:33:21 +00:00
// Just a specific number of repeats.
2017-06-02 15:14:52 +00:00
var done = - - fx . Repeats < = 0 ;
2010-03-30 13:33:21 +00:00
// Execute.
2017-06-02 15:14:52 +00:00
eval ( fx . Script ) ;
2012-05-05 10:09:44 +00:00
// Remove schedule if done.
if ( done )
return FX_Execute_Kill ;
return FX_OK ;
}
// Adds a timed call to an object, replacement of DefCore TimerCall.
2013-10-25 15:32:53 +00:00
global func AddTimer ( call_function , int interval )
2012-05-05 10:09:44 +00:00
{
if ( ! this )
return false ;
// Default to one second.
if ( interval = = nil )
interval = 36 ;
// Create effect and add function and repeat infinitely.
2017-06-02 15:14:52 +00:00
var fx = AddEffect ( " IntScheduleCall " , this , 1 , interval , this ) ;
fx . Function = call_function ;
fx . NoStop = true ;
fx . Pars = [ ] ;
2012-05-05 10:09:44 +00:00
return true ;
2009-12-29 13:44:16 +00:00
}
2012-06-24 18:37:28 +00:00
// removes a timer from an object that was added earlier with AddTimer. This removes exactly one timer that fits to the name and returns true on success
2013-01-01 23:50:01 +00:00
global func RemoveTimer ( call_function /* name or pointer to the timer to remove */ )
2012-06-24 18:37:28 +00:00
{
if ( ! this )
return false ;
2017-06-02 15:14:52 +00:00
var fx , index = 0 ;
while ( fx = GetEffect ( " IntScheduleCall " , this , index + + ) )
2012-06-24 18:37:28 +00:00
{
2017-06-02 15:14:52 +00:00
if ( fx . Function ! = call_function ) continue ;
if ( fx . NoStop ! = true ) continue ;
RemoveEffect ( nil , this , fx ) ;
2012-06-24 18:37:28 +00:00
return true ;
}
// not found
return false ;
}
2010-03-30 13:33:21 +00:00
// Executes a function repetitively with delay.
2017-10-20 22:10:42 +00:00
// documented in /docs/sdk/script/fn
2013-01-01 23:50:01 +00:00
global func ScheduleCall ( object obj , call_function , int interval , int repeats , par0 , par1 , par2 , par3 , par4 )
2009-12-29 13:44:16 +00:00
{
2010-03-30 13:33:21 +00:00
// Defaults.
2010-09-13 15:27:48 +00:00
if ( ! repeats )
2010-03-30 13:33:21 +00:00
repeats = 1 ;
// Create effect.
2017-06-02 15:14:52 +00:00
var fx = AddEffect ( " IntScheduleCall " , obj , 1 , interval , obj ) ;
if ( ! fx )
2010-03-30 13:33:21 +00:00
return false ;
// Set variables.
2017-06-02 15:14:52 +00:00
fx . Function = call_function ;
fx . Repeats = repeats ;
fx . Pars = [ par0 , par1 , par2 , par3 , par4 ] ;
2010-03-30 13:33:21 +00:00
return true ;
2009-12-29 13:44:16 +00:00
}
2017-06-02 15:14:52 +00:00
global func FxIntScheduleCallTimer ( object obj , effect fx )
2009-12-29 13:44:16 +00:00
{
2010-03-30 13:33:21 +00:00
// Just a specific number of repeats.
2017-06-02 15:14:52 +00:00
var done = - - fx . Repeats < = 0 ;
2010-03-30 13:33:21 +00:00
// Execute.
2017-06-02 15:14:52 +00:00
Call ( fx . Function , fx . Pars [ 0 ] , fx . Pars [ 1 ] , fx . Pars [ 2 ] , fx . Pars [ 3 ] , fx . Pars [ 4 ] ) ;
2012-05-05 10:09:44 +00:00
// Remove schedule call if done, take into account infinite schedules.
2017-06-02 15:14:52 +00:00
if ( done & & ! fx . NoStop )
2012-05-05 10:09:44 +00:00
return FX_Execute_Kill ;
return FX_OK ;
2009-12-29 13:44:16 +00:00
}
2017-10-20 22:10:42 +00:00
// documented in /docs/sdk/script/fn
2013-01-01 23:50:01 +00:00
global func ClearScheduleCall ( object obj , call_function )
2009-12-29 13:44:16 +00:00
{
2018-07-23 07:11:51 +00:00
var i ;
2010-03-30 13:33:21 +00:00
// Count downwards from effectnumber, to remove effects.
i = GetEffectCount ( " IntScheduleCall " , obj ) ;
2018-07-23 07:11:51 +00:00
while ( i - - ) {
2010-03-30 13:33:21 +00:00
// Check All ScheduleCall-Effects.
2018-07-23 07:11:51 +00:00
var fx = GetEffect ( " IntScheduleCall " , obj , i ) ;
if ( fx & & fx . Function = = call_function )
// Found right function. Remove effect.
RemoveEffect ( nil , obj , fx ) ;
}
2010-03-30 13:33:21 +00:00
return ;
2009-12-29 13:44:16 +00:00
}