Closed Ariorh1337 closed 1 month ago
I think what you need is to implement the "awake" method: https://phaser.io/editor/docs/scene-editor/user-components-awake-event
"scene-awake" will be called only when all elements have already been created, but this is not suitable when we need to create an element based on the properties of another element. In this case, we have to think about creating elements as template and then updating them to normal state. What I propose allows us to "bypass" the limitation that does not allow us to execute code immediately after the constructor but before the next element is created
Well, if you look the code the editor generates, the properties of user components are set at the end of the editorCreate
method. Yet I don't understand why the awake
method doesn't work for you. If you explain a bit more what's your particular case then I can have a better idea about the purpose of the feature you are requesting.
This is the code example, I hope it can show the meaning of the problem:
class ParentPrefab {
public property_index: number = 666;
constructor(scene, x, y) {
const child = new ChildPrefab(scene, 0, 0);
child.property_index = this.property_index;
scene.events.once("scene-awake", () => {
this.awake();
});
}
awake() {
console.log(1, this.property_index);
}
}
class ChildPrefab {
public property_index: number = 777;
constructor(scene, x, y) {
scene.events.once("scene-awake", () => {
this.awake();
});
}
awake() {
console.log(2, this.property_index);
}
}
class Scene {
constructor() {}
editorCreate() {
const parent = new ParentPrefab(this, 0, 0);
parent.property_index = 999;
this.events.emit("scene-awake");
}
}
// Console output:
// (2, 666) - ChildPrefab
// (1, 999) - ParentPrefab
// Expected output:
// (2, 999) - ChildPrefab
// (1, 999) - ParentPrefab
I don't get it. I see there you only create an instance of ParentPrefab. Is ChildPrefab a subclass (prefab variant) or ParentPrefab?
class ChildPrefab extends ParentPrefab
?
If you create a small demo project and share it here maybe I can have a better understanding.
I am closing this for lack of further information from the user.
Now the huge problem is that we can't run the code right after the declaration for the fact that all the properties are assigned outside the constructor. I want the component to always call a method after declaring the properties that would allow to bypass this problem.
In the screenshot I added my custom solution that allows such behavior, but the problem with my solution is that it is not universal because of the importance of assigning onceDefined at the very end. unfortunately I don't have the tools to make this property always the last one for children that inherit the behavior.
PS: the best solution is to allow some properties to be passed to the constructor