I have a variable that contains the url to one of my assets, stored in my index.js script. I’d like to use that variable in the “url” field of config.json, but can’t put the variable in directly. Is there any way I can use the value that the variable contains in the url field of config?
Does the variable value in index.js change? I assume there’s an issue with forking/cloning the project on whatever platform you are hosting it on?
I’m not entirely sure that param is used in config.json
Looking at __start__.js, it uses the variable SCENE_PATH which is defined in index.html
Either way, I would have thought you could change the function configure in __start__.js to use the variable in index.js as long as that script is loaded first in the index.html
What are the constraints that are preventing you in doing this?
The function in __start__.js, the configure function does not have the assets variable unlike the script attached by @Leonidas. The variable value changes in index.js, yes. Can I do something similar to SCENE_PATH? However, it appears that config.json uses the scene path directly, and not the variable.
config.json is just data and as far as I can tell, url it isn’t used in the loading of the app. SCENE_PATH is used instead in the callback of loading the JSON data in __start__.js
I am exporting the project from the editor. I don’t really know of the right way to do this, and this script isn’t included in the export. Could you guide me a bit further please? I know what to do once I find this script, but don’t know where to find and edit it.
Here is how I would update __start__.js to override the default app.configure method to allow editing the default asset url prior loading it:
// code replaced from line 119 to 150
var appConfigureOverride = function(url, callback) {
var self = app;
pc.http.get(url, function(err, response) {
if (err) {
callback(err);
return;
}
var props = response.application_properties;
var scenes = response.scenes;
var assets = response.assets;
for (var assetId in assets) {
var asset = assets[assetId];
if (!asset.file || !asset.file.url) continue;
var assetUrl = asset.file.url;
// add your variable here to edit/update the default asset url
asset.file.url = assetUrl;
}
self._parseApplicationProperties(props, function(err) {
self._parseScenes(scenes);
self._parseAssets(assets);
if (!err) {
callback(null);
} else {
callback(err);
}
});
});
};
var configure = function() {
appConfigureOverride(CONFIG_FILENAME, function(err) {
if (err) {
console.error(err);
}
configureCss(app._fillMode, app._width, app._height);
// do the first reflow after a timeout because of
// iOS showing a squished iframe sometimes
setTimeout(function() {
reflow();
window.addEventListener("resize", reflow, false);
window.addEventListener("orientationchange", reflow, false);
app.preload(function(err) {
if (err) {
console.error(err);
}
app.loadScene(SCENE_PATH, function(err, scene) {
if (err) {
console.error(err);
}
app.start();
});
});
});
});
};
That wasn’t my query @Leonidas. Where would I find this script in order to edit it? How would I make sure it is being used by the editor? In other words, how would I monkey patch while using the editor?
Not sure I understand what you mean, the config.json file isn’t part of your editor project. But part of the build you export from Playcanvas.
The start.js included in each exported build is responsible for parsing and loading the config.json file.
Above I’ve edited the start.js file for you to do what I think is what you ask: on runtime to allow you to update the url field of every asset in config.json with your variable.
So I just copy paste the edited contents above into this file? I would not like to change the url of all the assets, just some of them. Your edited file seems to do it for all of them.