A-Frame Entity-Component System: Component Initialization Order
0 reputation · 07 Dec 2022, 01:51 UTC
0 reputation · 07 Dec 2022, 01:51 UTC
A-Frame utilizes an entity-component system where multiple components can be attached to a single entity. According to the framework's behavior, the init() method for each component is triggered based on the declaration order within the HTML markup.
A challenge arises when two components on the same entity have mutual dependencies. If Component A requires data that is only generated during the init() phase of Component B, the current system does not provide a built-in mechanism to enforce a specific execution sequence regardless of markup order.
While using the tick event or manual sequencing are common workarounds, these methods increase boilerplate and may impact performance in complex scenes.
33275 reputation · 07 Dec 2022, 02:29 UTC
There is no built‑in mechanism in A‑Frame to force a specific init() execution order. The dependencies array in a component schema only guarantees that the listed sibling components are present; it does not change the sequence in which init() runs.
init() method in the exact order the components appear in the entity’s HTML markup.dependencies property is processed after markup parsing to add missing components, but it does not reorder init() calls.componentinitialized event on an entity each time a component finishes its init() phase.loaded event fires after all components attached at parse time have completed init() (but before the first render tick).componentinitialized event (or the entity’s loaded event) and run your setup code in the callback.AFRAME.registerComponent('needs-data', {
init: function () {
this.el.addEventListener('componentinitialized', (e) => {
if (e.detail.name === 'provides-data') {
// The provider’s init has finished; safe to read its data.
const data = this.el.components['provides-data'].getData();
this.useData(data);
}
});
},
useData: function (d) { /* … */ }
});
componentinitialized) before executing the shared logic.tick – Only use a per‑frame flag if you cannot guarantee that the dependency will have initialized before the first render (e.g., the dependency is added dynamically after the entity is already in the scene). Remember that tick runs every frame and should be removed once the condition is met to avoid overhead.init(). Place them in different orders in the markup and confirm the log order matches the markup regardless of any dependencies array.componentinitialized in component A that checks for B’s initialization. Verify that the callback runs after B’s log appears.dependencies property.Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.