Job Control offers a more flexible alternative to the builtin minetest.after function.
- Jobs can be cancelled, extended, or restarted via object-specific methods.
- Jobs can be repeated at a constant interval when the callback returns true.
- The elapsed, remaining, and overrun time of jobs can be readily calculated.
There are two ways to install Job Control: either as a typical mod within the directory structure of your game or by replacing the file 'builtin/common/after.lua' (be sure to create a backup of the original file). The latter has the advantage that it eliminates the small degree of overhead from an extraneous globalstep running in the background.
A job can be scheduled in the typical fashion, thus ensuring backward compatibility:
- minetest.after( wait, func, ... )
Executes the callback after a timeout with optional parameters. The newly constructed Job object is returned.
Each job object has access to the following methods and properties:
-
Job::wait
The timeout passed to the constructor. This property is immutable. -
Job::expiry
The expiration timestamp of the job. This property is immutable. -
Job::origin
The name of the mod that created the job. This property is immutable. -
Job::cancel( )
Removes the job from the pending job queue. The callback will no longer be executed. -
Job::extend( new_wait )
Reschedules the job with an additional timeout, beyond the expiration timestamp. -
Job::restart( wait )
Reschedules the job with a different timeout. -
Job::get_remain( )
Returns the remaining time period of the job. From this, it is possible to calculate the elapsed and overrun time periods as well.
As mentioned earlier, it is also possible to cycle a job simply by returning true in the callback. This can be useful for tasks that must be performed at regular intervals, such as writing data files to disk. Bear in mind, however, that this isn't a replacement for an actual synchronized timer since the callback execution itself can incur a marginal delay during each iteration.