Jump to content

Class Based Asset Manager Question


Calsa
 Share

Recommended Posts

Hi all, I have not coded in a while but have been doing some playing lately. I have been working on a simple control class for assetmanager for a multi level type game. I have run into a bit of an issue inside the .onSucess. as soon as the engine hits the this.library[task.name] line Babylon skips that line and everything else inside onSucess. I can add in statements inside the { above that line and it works fine, but something right at that line is wonky which has had me stumped for a good hour. Advice?

Oh, and yes, I know I am way to addicted to using ; and they are not necessary. Just still feels weird. Find and replace at the end I guess :\ 

class Assets{
    constructor(destiny){
        this.destiny = destiny;
        this.scene = destiny.scene;
        this.initalized = false;
        this.assetsManager = new BABYLON.AssetsManager(this.scene);
        this.library = {}; // loaded template model library object
    };
    
    queMeshTask(meshURL, meshName){
        var meshPath = meshURL + meshName + "/";
        var meshFullName = meshName + ".obj";
        if(debugModeOn==1){dm("Queue Mesh: " + meshPath + meshFullName, 1)};
        var meshTask = this.assetsManager.addMeshTask(meshName, "", meshPath, meshFullName);
        meshTask.onSuccess = function(task){
            this.library[task.name] = task.loadedMeshes; // <-- This and anything after it within the onSuccess {} will not execute. HELP!
            if(debugModeOn==1){ dm(task.name + " has been initialized.", 1)}; // dm = in game console debugger
            for(i=0; i < task.loadedMeshes.length; i++){
                task.loadedMeshes[i].isVisible = false; // hide the library meshes
            };
            this.scene.getObjectByName(meshName).initNode(this.library[task.name]);
        };
    };
    
    initLoading(engine){
        this.assetsManager.onProgress = function(remainingCount, totalCount, lastFinishedTask) {
            engine.loadingUIText = 'Please stand by. Items Remaining ' + remainingCount + ' out of ' + totalCount + '...';
        };

        this.assetsManager.onFinish = function (tasks) {
            dm("Success, All Assets Loaded", 1);
            this.initalized = true;
        };
        this.assetsManager.load();
    };
};

 

Link to comment
Share on other sites

Unfortunately "this" in your function context is NOT the object but the function itself (I know this is weird but this is how JS works :))

So you could easily fix it with arrow functions that capture the host context:

meshTask.onSuccess = (task)=> {
            this.library[task.name] = task.loadedMeshes; // <-- This and anything after it within the onSuccess {} will not execute. HELP!
            if(debugModeOn==1){ dm(task.name + " has been initialized.", 1)}; // dm = in game console debugger
            for(i=0; i < task.loadedMeshes.length; i++){
                task.loadedMeshes[i].isVisible = false; // hide the library meshes
            };
            this.scene.getObjectByName(meshName).initNode(this.library[task.name]);
        };

"this" inside an arrow function is the host object so this.library will exist

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...