Accessing project files on runtime in preview mode

For discussion and help with the Construct 3 Plugin SDK

Post » Sat Jul 01, 2017 12:09 am

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:

Code: Select all
var myScriptTag=document.createElement('script');
myScriptTag.setAttribute("type","text/javascript");
myScriptTag.setAttribute("src", "myscript.js");
document.getElementsByTagName("head")[0].appendChild(myScriptTag);

and it worked both in preview mode and on export. But when I add myscript.js 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?
B
9
S
4
Posts: 12
Reputation: 751

Post » Sat Jul 01, 2017 12:12 pm

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:

Code: Select all
var realUrl = this.runtime.getProjectFileUrl("myscript.js");


That gives you the real URL to request that will work in both preview and export.
Scirra Founder
B
387
S
230
G
88
Posts: 24,249
Reputation: 192,450

Post » Sat Jul 01, 2017 8:45 pm

Ashley wrote: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:

Code: Select all
var realUrl = this.runtime.getProjectFileUrl("myscript.js");


That gives you the real URL to request that will work in both preview and export.


Cool, thanks, that worked!
B
9
S
4
Posts: 12
Reputation: 751

Post » Sun Jul 02, 2017 3:11 pm

@Ashley

Please add this information into SDK.
B
108
S
26
G
260
Posts: 4,434
Reputation: 146,191


Return to Plugin SDK

Who is online

Users browsing this forum: No registered users and 0 guests