Global plugins are properly registered to scenes when the game is hot-reloaded

Ouverte
#7,151 0 commentaires 1 réaction 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 3 jours

Personne n'a encore pris cette issue.

Évaluation

Difficulté
4/5
Temps estimé
3-5 jours
Accessibilité débutants
45/100
Type d'issue
Bug
Clarté
Plutôt claire
Activité
À l'abandon
Stack technique
javascript
Domaine
game-dev

Piste de recherche

Commencez par src/plugins/PluginManager.js aux lignes liées 444-448, puis reproduisez le problème en utilisant la configuration globale des plugins et l’accès à la scène présentés dans le rapport. Vérifiez le chemin de hot-reload et confirmez que le rechargement ne produit plus « Plugin key in use: l10n » et que le mappage de la scène reste disponible.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

Version

  • Phaser Version: 3.88.2
  • Operating system: Windows
  • Browser: Chrome

Description

When adding a global plugin through the game config, the plugin is automatically added to scenes.

However, if you're using hot-reloading (by default, I'm using the Svelte game template, which has this), re-adding the plugin doesn't work (Plugin key in use) as it's already there.

Image

Because of this, however, re-registering with scenes doesn't work, and you need to do it manually in the Boot scene or whatever

init() {
  const localisation = this.game.plugins.get(localisationPluginMapping) as LocalisationPlugin;
  this.game.scene.getScenes(false).forEach((scene) => {
    scene.l10n = localisation;
  });
}

I think it's because of this code in PluginManager.js where it returns null instead of the plugin.

Example Test Code

Create a basic plugin:

export class LocalisationPlugin extends Phaser.Plugins.BasePlugin {
	constructor(pluginManager: Phaser.Plugins.PluginManager) {
		super(pluginManager);
	}
...
}

export const LocalisationPluginConfig: Phaser.Types.Core.PluginObjectItem = {
	key: 'l10n',
	plugin: LocalisationPlugin,
	mapping: 'l10n'
};

Add the plugin to your config

const config: Phaser.Types.Core.GameConfig = {
	type: AUTO,
	width: Math.min(1024, window.innerWidth),
	height: Math.min(768, window.innerHeight),
	parent: 'game-container',
	backgroundColor: '#028af8',
	scene: [Boot, Preloader, MainMenu, MainGame],
	scale: {
		mode: Phaser.Scale.FIT,
		autoCenter: Phaser.Scale.CENTER_BOTH
	},
	plugins: {
		global: [LocalisationPluginConfig]
	}
};

In one of your scenes, access the plugin

create() {
	this.instructionsText = this.add
		.text(
			this.cameras.main.width / 2,
			this.cameras.main.height - 100,
			this.l10n.translateClickToStart().toUpperCase(),
			{
				fontFamily: 'Arial',
				fontSize: 15,
				color: '#ffffff',
				stroke: '#000000',
				strokeThickness: 3,
				align: 'center'
			}
		)
		.setOrigin(0.5);
}

Perform some action where hot-reloading is necessary. You should see the warning message Plugin key in use: l10n and the call to translateClickToStart will fail as l10n is undefined

Additional Information

I have very little experience with the code, but I think we could either:

  • Properly remove global plugins (scene plugins may also suffer, I haven't tested) so they can be re-added as normal
  • Gracefully handle the plugin already existing so it can still be re-registered with scenes (return the plugin rather than null)
  • Use this.game.plugins.get('l10n') rather than the return of registering the plugin (which is what I'm assuming it's using)
Langage dominant
JavaScript
Étoiles
40.7k
Forks
7.2k
Métriques de merge des PR
Aucune PR mergée en 30 j

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de phaserjs/phaser

Toutes les issues de phaserjs/phaser

Issues similaires

Plus d'issues JavaScript

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.