[SOLVED] Changing the contents of config.json remotely

Hi all,

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?

I can think of two ways doing this:

  1. Either have a server side script in your server that overwrites the active config.json and updates the url.
  2. In the start.js script of the loader you can monkey patch the app.configure the engine uses to load and parse the config.json file.

You can see how the method is defined here, and you can write in your own code to create a version that does that:

1 Like

What would I change here to use the variable mentioned above @Leonidas?

You would have to loop through the assets listed in the following variable and update their loading url with your variable:

var assets = response.assets;
1 Like

Thanks a lot. I will try this next morning as it is quite late here, and get back to you.

Unfortunately, due some constraints, I cannot do option 2. How would I do the above👆 @Leonidas ?

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 :thinking:

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?

@yaustar,

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

Any way to solve this?

var props = response.application_properties;
var scenes = response.scenes;
var assets = response.assets;

These three lines are not there.

Those lines aren’t part of the start.js file, but are part of the pc.Application.configure method, which is called in that file:

You can create a variation of that method in your code to add your custom variable in the asset filename default url.

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.

Where do I find start.js here?

Once I am able to locate it, I’m sure your edited file will do what I need it to.

3rd file from top, it’s just the forum parses the underscores and changes the filename:

__start__.js

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.

This is how I’ve done it: https://github.com/yaustar/config-json-example (added an index.js file with a variable to a path and changed the __start__.js configure function.

1 Like