HomeWeb Components API Reference - v0.31.0
    Preparing search index...

    Class ScriptInstanceElement

    The ScriptInstanceElement interface provides properties and methods for manipulating <pc-script-instance> elements. The ScriptInstanceElement interface also inherits the properties and methods of the AsyncElement interface.

    Script attributes can be supplied through two channels:

    • Per-property attributes: any non-reserved attribute on the element maps to the script attribute of the same name (kebab-case to camelCase, e.g. focus-point → focusPoint). Values are parsed according to the type of the attribute's current value — initially the script's declared default (numbers, booleans, strings, Vec2/3/4, Color, Quat as Euler angles) — and the asset:/entity:/vec2:/vec3:/vec4:/color: prefixes may be used to be explicit. An entity: reference is an entity name — resolved against the nearest enclosing entity first, then outward, then the document — or a document-wide # selector (entity:#id); a bare value is always a name, never an element id.
    • The attributes JSON attribute: an object supporting nested structures and attribute names that collide with reserved HTML attribute names (e.g. title).

    When both specify the same attribute, the per-property attribute wins — at creation and whenever either channel changes at runtime. The element's own name and enabled attributes configure the element itself and are not script attributes.

    Changing name on a live element destroys the old-name script instance and creates the new-name one, re-applying both attribute channels to it.

    The element becomes ready once its script instance has been created by the parent <pc-script> element. An instance the document declares when the application boots is created before the application starts and initialized as it starts, along with every other script, so it is ready before its initialize() has run. So is one in a subtree added to an application that is already running: it is created as the subtree is built, and initialized with the subtree's other scripts as the subtree joins the scene. A script class registered after the element is waited for, and its instance created with the element's declared state when it arrives; one still missing once no script asset is left loading logs a warning.

    scriptattributeschange - Fired when the script's attributes change. The detail carries the new attributes object. Bubbles.

    scriptenablechange - Fired when the script's enabled state changes. The detail carries the new enabled state. Bubbles.

    scriptnamechange - Fired when the script is renamed on a live element. The detail carries oldName and newName. Bubbles.

    Hierarchy (View Summary)

    Index
    • get closestApp(): AppElement | null

      The nearest ancestor <pc-app> element, or null if this element has no <pc-app> ancestor. The search starts at the parent, so an element never resolves to itself.

      Returns AppElement | null

      The closest app element, or null.

    • get closestEntity(): EntityBaseElement | null

      The nearest ancestor element that fronts an entity — <pc-entity>, <pc-model> or <pc-node> — or null if this element has no such ancestor. The search starts at the parent, so an element never resolves to itself.

      Returns EntityBaseElement | null

      The closest entity-fronting element, or null.

    • get name(): string

      Gets the name of the script.

      Returns string

      The name.

    • set name(value: string): void

      Sets the name of the script to create. The name attribute is the single source of truth (it is what the parent <pc-script> element reads when creating the instance), so the property writes through to it — assigning before insertion works as expected:

      const script = document.createElement('pc-script-instance');
      script.name = 'rotate';
      scriptsElement.appendChild(script);
      await script.ready();

      Parameters

      • value: string

        The name.

      Returns void

    • get scriptAttributes(): Record<string, any>

      Gets the attributes of the script as an object whose asset:, entity:, vec2:, vec3:, vec4: and color: prefixed values are resolved when applied — an entity: value being an entity name (nearest enclosing entity first, then outward, then the document) or a document-wide # selector (entity:#id), never a bare element id.

      Returns Record<string, any>

      The attributes of the script.

    • set scriptAttributes(value: Record<string, any>): void

      Sets the attributes of the script as an object. Values are converted with the same rules as the attributes attribute: asset:/entity: references and vec2:/vec3:/vec4:/ color: prefixed strings are resolved (an entity name against the nearest enclosing entity first, then outward, then the document — or a document-wide # selector; a bare value is always a name, never an element id), and a plain numeric array is converted to the type of the attribute it targets when that attribute currently holds a Vec2, Vec3, Vec4 or Color.

      Parameters

      • value: Record<string, any>

        The attributes of the script.

      Returns void

    • Called when the element is fully initialized and ready. Subclasses should call this when they're ready. Resolves the ready promise and dispatches a bubbling, composed ready event. Signals at most once per readiness cycle: a repeat call before _resetReady has re-armed the promise does nothing.

      Returns void

    • Returns the ready promise to its pending state. Subclasses should call this when the resource their readiness announced is torn down (typically from disconnectedCallback), so that a later re-initialization can signal readiness again. Does nothing while the promise is still pending — an in-flight waiter carries over to the next readiness cycle rather than being stranded on a promise nothing will ever resolve.

      Returns void

    • Returns a promise that resolves with this element when it's ready. This is the low-level primitive underlying whenReady, which is the recommended way to wait for elements.

      Readiness tracks the element's current lifecycle: once a ready element is torn down (for example by removing it from the document), this returns a fresh promise that resolves when the element is next ready. A promise obtained earlier stays resolved — call this again after re-inserting an element rather than reusing a promise from before its removal.

      Returns Promise<ScriptInstanceElement>

      A promise that resolves with this element when it's ready.