question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prefabs: constructor ordering of custom definition props and START-USER-CTR-CODE

See original GitHub issue
  • Phaser Editor Version: 3.15
  • Operating system: Mac Big Sur

Description

Note that while technically this is a bug, it is more about a bigger issue of the data update model in prefabs.

This came about due to a related change in https://github.com/PhaserEditor2D/PhaserEditor2D-v3/issues/132#issuecomment-876707383.

Currently in the constructor the ordering of data init is:

  1. codegen of Phaser objects created in the editor
  2. custom definition of props
  3. USER-CTR-CODE

The issue is that any custom objects (i.e. ones that Phaser Editor does not have a representation for) are created in the USER-CTR-CODE section which means that props with setters will not be applied to custom objects. It also means that every custom setter must have guard code to check if the custom objects have been created yet just to handle the constructor sequencing since the custom objects will be defined after the constructor returns.

Possible solution

There are a couple possible solutions:

  1. Have two sections of prop definitions in the constructor: One before the USER-CTR-CODE and one after. The one before is for props that do not have custom setters and can be used in custom object init, and the other will be for props with custom setters that may apply to the custom objects.

  2. A single section of custom definition props that appears after USER-CTR-CODE. That way the custom setter can apply values to the custom objects. This is simpler than (1) since the custom setters must apply the values anyway so easier to just assume that the values get set in the setter rather than in the custom object init.

The application of props without custom definitions would need to happen in a start method that can be triggered via a generated event handler.

this.scene.events.once(Phaser.Scenes.Events.UPDATE, this.start, this);

Bigger discussion

There are currently components-awake and prefab-awake events that are fired in the prefab’s editor generated create handler. There are a few issues with this approach.

  1. Pollution of scene with 3 lines per each prefab in the scene. Double that if the prefab has components. For a scene with even a modest number of prefabs (i.e. ~20) the majority of the scene becomes event emitter lines.

  2. prefab-awake encourages a polling data model

prefab-awake was added so that custom props can be applied to objects. The problem is that if the properties change later there is no way to know unless a previous value is checked or if they are updated in every single update call. Neither is efficient.

It would be better to eliminate the prefab-awake and force the use of setters. Note that phaser itself does this so that property updates are properly reflected in the objects (e.g. https://github.com/photonstorm/phaser/blob/5c8ecbcf999e6f328d21884e877c9e5935d2d350/src/gameobjects/text/Text.js#L1321).

Some properties like position are handled during the render cycle and don’t need immediate update, but many property updates trigger a one time change in the object. Either can be handled by a custom setter.

Question about components

It seems component-awake should be handled entirely within the component itself. In the component constructor an event handler can be added so that the code currently in awake would be executed there instead.

The component already has a reference to the gameObject which has a reference to the scene so the following can be placed in the constructor:

this.gameObject.scene.events.once(Phaser.Scenes.Events.UPDATE, this.start, this);

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:19 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
jrabekcommented, Jul 17, 2021

This looks awesome @PhaserEditor2D ! Thanks for the quick change. I’ll give it a test out as soon as I can.

1reaction
PhaserEditor2Dcommented, Jul 16, 2021

Hi @jrabek!

In base the custom definition properties are a very recent addition, I think it is not going to create a compatibility issue.

Please, can you check the latest changes in the develop branch? You can follow the instructions here: https://github.com/PhaserEditor2D/PhaserEditor2D-v3-build-develop

Now the custom definition properties are initialized after the USER-CTR-CODE section. If you need to add custom actions after the custom definition properties are set, then you can listen to the prefab-awake event.

This applies to prefabs and user components.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Phaser Editor 2D v3.20.0 released. Welcome nested prefabs!
A new version with new amazing features: nested prefabs, nine-patch, ... #136 Prefabs: constructor ordering of custom definition props and ...
Read more >
What is the correct way to access props in the constructor?
When implementing the constructor for a React.Component subclass, you should call super(props) before any other statement.
Read more >
vue/require-prop-type-constructor
This rule reports prop types that can't be presumed as constructors. It's impossible to catch every possible case and know whether the prop...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found