Question about Phaser Asset Management

I’ve been working with phaser and I’m curious, is it necessary for each scene to have their own preload() method as a standard form of asset management? I would think it be better handled in a central config file so that developers have a reasonable scope of where a file is loaded. If you wanted to get granular you could have a directory of loader files per scene and export them to the config file. The idea is to have the framework do the preload work for you.

I just think loading assets directly in a scene (when using a bundler) leads to bloated error-prone code if you’re not careful. I realize you could set up a ‘bootup’ scene and shove your assets in there, but I imagine a config file would make a lot more sense to many more people.

I’m also curious why you have to specify a key and a file location when the key could trivially be parsed from the filename. If necessary you could override the key as an optional, secondary parameter or an inline object.

The Cache, which is the system that holds the loaded files, is global for the game. So yes, you can have a boot scene that loads all of the assets you’re going to need at once - and actually, many people prefer to do it this way.

How you invoke the loader is up to you. You can create any config file you want and parse it however you want, as long as you translate it to calls to the loader. If you prefer something built-in, Phaser has a feature called file packs that are similar to config files.

I honestly can’t give you a good reason for it. It’s been like this since at least Phaser 2. Of course, nothing prevents you from writing your own wrapper or parsing your own config format.

2 Likes

You can write a pack file (or object) for this and load it.

You can pass a key only, treating it as the URL (with optional extension).

2 Likes

Yeah the boot scene method is what I use currently-- I was just curious why asset loading in preload() as a publicly exposed method per-scene is the de-facto standard since it could lead to clumsy code. It’s part of a broader issue I have with using phaser in general (you can instantiate particle emitters with a configuration object but have to mutate physics body options with setters?). Maybe I just don’t see the benefit of ad-hoc asset loading per scene in web-based games rather than loading all assets up-front and specifying any special scene based loading in a config file.

Also I realize I could extend phaser to do a lot of this but my hope is to open a Quality of Life discussion rather than keeping this in a “it’s always been done this way” bucket.

Either way, thank you for the clarifications, guys!

Do you mean you’d want this.physics.make.image({/* config */})?

1 Like

Something like that would be nice-- or

// GameObject can image, circ, rect etc.
scene.physics.add(GameObject, { 
  bounce: .5, // set x, y at once
  velocity: new Phaser.Math.Vector2(100, 0), // set x, y in a vector
  collider:
    world: {
      onCollision: this.handleWorldCollide, // Passing method would set setColliderWorldBounds to true and add the method to scene.physics.world.on('worldbounds')
      checkCollision: {up: false}
    },
    SomeGameObject: { /* same idea but with a specific gameObject */ }
});