Load scene plugins for all scenes, or only those that need them?

Should I just register all my scene plugins (via config) and have them available to all scenes? (ie, thinking of them as global extensions, with functionality that is explicitly invoked where needed)

Or conversely, trying to only load plugins for scenes that actually need them? (thinking of them more as composable elements, that may provide implicit/automatic functionality just by being attached to a scene)

It seems like the former is what I see used in practice in examples and discussions.

I’ll give you an example: Trying to build an RPG with an overworld, towns, caves, etc. These are all “locations” (a scene that a player can walk around in), so I’m trying to put that functionality into a Location scene plugin.

Should I be trying to only load this plugin for those scenes, or register it for all scenes (and then only initialize it from the scenes that want to use it)?

I find the examples that load scene plugins kinda weird (eg Phaser - Examples - v3.85.0 - plugins - Load Scene Plugin Test 1), is loading at runtime via a URL (in assets) really the way to go? I would much prefer to import them.

I have found that this seems to work, even though I can find no documentation or examples that use this approach:

import MyScenePlugin from './MyScenePlugin';

export default class MyScene extends Phaser.Scene {
  preload() {
    this.load.scenePlugin('myScenePlugin', MyScenePlugin);
  }
}

Thanks in advance for any help or guidance!

:waving_hand:

import LocationPlugin from './LocationPlugin';

new Phaser.Game({
  plugins: {
    scene: [
      { key: 'LocationPlugin', plugin: LocationPlugin, mapping: 'location' }
    ]
  },
  scene: [
    new Phaser.Scene({
      key: 'menu',
      // No 'LocationPlugin'.
      plugins: Phaser.Plugins.DefaultPlugins.DefaultScene
    }),
    new Phaser.Scene({
      key: 'town'
      // All scene plugins, including 'LocationPlugin'.
    })
  ]
});

Thanks very much for this.

It seems though, like controlling it this way could get messy with many different plugins being needed by many different scenes?

(Example: overworld and cave scenes need EncountersPlugin, but town does not, even though all three are Location scenes)

I do like the idea of importing scene-specific dependencies directly (like in my 2nd example above), would this work?

Or, I’m thinking to keep things simpler, just have all scene plugins available to all scenes, and only some get initialized (eg scenes would call this.location.initLocation(), this.encounters.initRandomEncounters(), etc)

1 Like

That should work fine, yes.