I had a need, where some of my in-game script files would contain a different logic, which would depend on the platform the game was hosted on. For example, my
cloud.js has a logic that fetches the friend list of a player. If the game is hosted on Facebook, I use its API to fetch the list; if it is on Google, I use a different set of API to get the list; and so on. All of the versions of my
cloud.js listen for the same events, and fire the same events on some action completion, but the logic to complete these actions differs.
For example, within my game at some point, I simply fire an event, say
get top 5 scores of my friends, then one of my views expects to receive an array of friends by listening for
cloud fetched friends event and act on it. Meanwhile, my
cloud.js uses its logic to fetch the list, normalize it for the rest of my game scripts to consume and fires
cloud fetched friends event.
Initially. I had a single
cloud.js file with lots of
if statements, where I would change the API calls and handlers, based on the current platform. However, it quickly became hard to maintain. So, my solution was to download the non concatenated build from Playcanvas, and replace the correct version of the
cloud.js file via the local task tool, when it prepares the build for one platform or another.
I use the same approach to replace the root
index.html, since some platforms require own scripts to load first, before anything else is loaded. Or some additional functionality initialization is required, during the game load, etc.
After I replace the individual files, I concatenate all game related scripts (i.e. the ones you create via Editor or through
.createScript() method). To do this, I traverse though the
files/assets folder and concatenate all
.js files, except the Ammo physics files (I found it doesn’t want to work, if I also include it to the final merged file), into a single game file, say
my-game-scripts.js. Then, my task tool replaces the references of all the related assets in the
config.json to point to this newly created concatenated file. The engine hash checks are simple, so I just kept their values as they are.
In the end, I have different versions of the game, where the core game files are same, but the initialization differs, based on the target platform the build was prepared for.