I’ve hit a bit of a snag on my first project; I set up my project such that all the asset paths are contained within a json file along with other game object data. I realized too little too late that while you can load the json data within the preload function, game.cache.json.get apparently doesn’t return anything if called from preload; you instead have to call it from the create function.
The game I’m making is primarily a first-person point-and-click adventure game, and has a LOT of different screens, and I’d like to avoid listing them all in the actual body of the code, if possible. Keeping the asset paths in a data file seems like the more elegant solution, but the structure of Phaser is getting in the way of that. Is this Phaser’s way of pushing me towards a different structure that’s better suited for this, or is there simply a method I’m missing?
My main questions are:
(1) Is there a simple way to access json data in preload?
(2) Is it advisable?
and
(3) If not, what’s a better alternative?
Assuming that you’re loading the JSON through this.load.json: network requests are asynchronous. Even after you make the request for the JSON, it won’t be loaded until after preload has finished executing. This is why you can’t access the data.
If you need some data to be loaded before preload, you can use a scene payload.
// This is perfect for loading in small JSON config files for example,
// or a tiny amount of preloader assets that the preloader itself needs to use.
Does this mean this technique doesn’t scale well for larger data files which specify lots of assets?
Also, what’s the appropriate general type for a json data file? ‘atlas’ seems to only be for a specific situation. Edit: Figured this out. The type is simply ‘json’.
It’s equivalent to loading the files from preload (they’re just loaded earlier). However, since the scene can’t run while waiting for the payload, large files could result in a delay before anything is shown to the player. Realistically, most JSON files are unlikely to be big enough to cause trouble.