Accessing project files on runtime in preview mode

  • In Construct 2 I could add myscript.js into project files and include it into a project on runtime (with a plugin for example) by doing this:

    var myScriptTag=document.createElement('script');
    myScriptTag.setAttribute("type","text/javascript");
    myScriptTag.setAttribute("src", "myscript.js");
    document.getElementsByTagName("head")[0].appendChild(myScriptTag);[/code:21b9xvid]
    and it worked both in preview mode and on export. But when I add [b]myscript.js[/b] into project files in Construct 3, it's no longer accessible in a preview mode and gives 404. But it works fine on export.
    
    What should I do to access a project file on runtime in a preview mode?
  • Due to the way preview mode works in C3 loading local files has to be done slightly differently. This is one of the few differences in the runtime in C3. Instead of using "myscript.js" directly, call:

    var realUrl = this.runtime.getProjectFileUrl("myscript.js");[/code:26wp3i4q]
    
    That gives you the real URL to request that will work in both preview and export.
  • Due to the way preview mode works in C3 loading local files has to be done slightly differently. This is one of the few differences in the runtime in C3. Instead of using "myscript.js" directly, call:

    var realUrl = this.runtime.getProjectFileUrl("myscript.js");[/code:hpel1tob]
    
    That gives you the real URL to request that will work in both preview and export.
    

    Cool, thanks, that worked!

  • Ashley

    Please add this information into SDK.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Ashley With C3 Runtime I have this error:

    Error executing JavaScript: TypeError: Cannot read property 'getProjectFileUrl' of undefined

    This is my code:

    var nameScript= 'mathjs.js';
    var realUrl = this.runtime.getProjectFileUrl(nameScript);
    var jsFile = document.createElement('script');
    jsFile.src = realUrl;
    document.head.appendChild(jsFile);
    
  • This is because you do this.runtime.getProjectFileUrl(nameScript) in the global scope. This is supposed to be executed from inside of the plugin/behavior instance. When you do this in the global scope, your "this" is a window object that has no "runtime" property.

    From the global scope try c3runtime.getProjectFileUrl

    Ashley With C3 Runtime I have this error:

    Error executing JavaScript: TypeError: Cannot read property 'getProjectFileUrl' of undefined

    This is my code:

    > var nameScript= 'mathjs.js';
    var realUrl = this.runtime.getProjectFileUrl(nameScript);
    var jsFile = document.createElement('script');
    jsFile.src = realUrl;
    document.head.appendChild(jsFile);
    

  • valerypopoff Ashley

    I think there is no getProjectFileUrl() function in the new runtimes :(

    I solved with window.cr_previewProjectFiles[nameScript].

    This is my code:

    var nameScript= 'mathjs.js';
    var realUrl = window.cr_previewProjectFiles[nameScript];
    var jsFile = document.createElement('script');
    jsFile.setAttribute('type', 'text/javascript');
    jsFile.src = realUrl;
    document.head.appendChild(jsFile);
    
  • What's the situation with this.runtime.getProjectFileUrl in the new C3 runtime? How do I get file urls?

    Ashley

    Due to the way preview mode works in C3 loading local files has to be done slightly differently. This is one of the few differences in the runtime in C3. Instead of using "myscript.js" directly, call:

    var realUrl = this.runtime.getProjectFileUrl("myscript.js");[/code:26wp3i4q]
    
    That gives you the real URL to request that will work in both preview and export.
  • valerypopoff - in the C3 runtime use this._runtime.GetAssetManager().GetProjectFileUrl().

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)