Difference between AssetsManager.onFinished vs onTasksDoneObservable

Docs says onFinished is called when tasks are ‘processed’ vs onTasksDoneObservable is when they are ‘executed’. I guess I need clarification what is the difference?

what I see is that my tasks (for example loading a bunch of textures) is actually all completed when onFinished is called. The tasks array contains my texture tasks and it apears those tasks have actually loaded the textures. Then onTasksDoneObservable ends up being called after that but the tasks array in there is empty. So I’m a bit confused. No repro on the playground at the moment but wanted to understand the differences here first.

ps: ultimately what im trying to do is chain a group of tasks together. So for example , first loading all texture, then loading models only after all textures are loaded successfully. So these observables are critical to hook into those events.

They are both identical but a loonnnnnggg time ago :slight_smile: we did not have observables so we used to have onFinish. We kept it for back compat reasons.

fair enough. So then how come onTasksDoneObservable returns an empty array of tasks. Should it not contain an array of all current added tasks?

PG: https://www.babylonjs-playground.com/#SXEV7#15

The code is actually looking like this:

const currentTasks = this._tasks.slice();

                if (this.onFinish) {
                    // Calling onFinish with immutable array of tasks
                    this.onFinish(currentTasks);
                }

                // Let's remove successful tasks
                for (const task of currentTasks) {
                    if (task.taskState === AssetTaskState.DONE) {
                        const index = this._tasks.indexOf(task);

                        if (index > -1) {
                            this._tasks.splice(index, 1);
                        }
                    }
                }

                this.onTasksDoneObservable.notifyObservers(this._tasks);

This actually explains why the tasks list is empty but in this case I wonder we are even sending the list back. @Deltakosh might have a clue ?

ok looks like omits any successful tasks. I was just hoping to check task status like this as per documentation:
assetsManager.onTasksDoneObservable.add(function(tasks) {
var errors = tasks.filter(function(task) {return task.taskState === BABYLON.AssetTaskState.ERROR});
var successes = tasks.filter(function(task) {return task.taskState !== BABYLON.AssetTaskState.ERROR});
});

see: Asset Manager | Babylon.js Documentation

So it is all good, you can still find all the tasks in error, no need to filter out the successfull ones ?

im not understanding your question… the resulting array onTasksDoneObservable is empty. There are no error or success tasks in there as its an empty array. no data. That array should not be empty correct? So this is not behaving as documented…

It is empty cause they are all successful. If any tasks were in error they would be in the list. I ll update the doc.

1 Like

Understood, if this is the expected result im happy. I was just worried I was doing something bad. Thanks again.

2 Likes