It's way less arcane than you seem to think. The task objects are JavaScript objects (variables with variables inside of them--we call these internal variables 'properties', usually). You can pass entire functions as variables in JavaScript, so when a new passage transition starts, each function inside of each task object is called. From SugarCube's source code:
Object.keys(postdisplay).forEach(function (task) {
if (typeof postdisplay[task] === 'function') {
postdisplay[task].call(this, task);
}
}, passage);
And so on for each task in it's proper place.
So the code that creates a function in a task object creates a new property on the relevant object and assigns a function to it.
predisplay['name'] = function (taskName) {...};
// creates new property on the predisplay object called 'name' and assigns it an anonymous function
Functions can be called with arguments, and task objects are always called with an argument referencing the task's name. This allows you to manipulate the task object in certain ways; for example, it allows you to delete tasks or create single use tasks:
predisplay['name'] = function (taskName) {
delete predisplay[taskName]; // single-use task
console.log('this task happens only once');
};
predisplay['name'] = function (taskName) {
console.log('this task happens 10 times (assuming $var starts at 0)');
if (State.variables.var > 10) {
delete predisplay[taskName];
// delete task after tenth time it's called
} else {
State.variables.var++;
}
};
You also don't have to use the variable taskName to store the task's name in the function:
predisplay['name'] = function (x) {
delete predisplay[x];
console.log('this is predisplay task ' + x);
};
However, I'd recommend using taskName because
- It's what the documentation uses.
- It's what everyone else uses.
You can get an array of any object's properties via the Object.keys() standard JavaScript method, by passing the name of the task object you want to look at to the method:
<<set $taskKeys to Object.keys(predisplay)>>
<<print $taskKeys.join(', ')>>
By default, all the task objects are empty, so you can use any names you want.
Note: I didn't test any of this code, so it's possible that there are errors in it.