aframe Entities Entity Multi-Property Component Data (setAttribute)


Example

Updating Multi-Property Component Data

To update component data for a multi-property component, we can pass the name of a registered component as the componentName, and pass an object of properties as the value. A string is also acceptable (e.g., type: spot; distance: 30), but objects will save A-Frame some work in parsing:

// Only the properties passed in the object will be overwritten.
entity.setAttribute('light', {
  type: 'spot',
  distance: 30,
  intensity: 2.0
});

Or to update individual properties for a multi-property component, we can pass the name of registered component as the componentName, a property name as the second argument, and the property value to set as the third argument:

// All previous properties for the material component (besides the color)  will be unaffected.
entity.setAttribute('material', 'color', 'crimson');

Note that array property types behave uniquely:

  • Arrays are mutable. They are assigned by reference so changes to arrays will be visible by the component.
  • Updates to array type properties will not trigger the component’s update method nor emit events.

Updating Multi-Property Component Data

If true is passed as the third argument to .setAttribute, then non-specified properties will be reset and clobbered:

// All previous properties for the light component will be removed and overwritten.
entity.setAttribute('light', {
  type: 'spot',
  distance: 30,
  intensity: 2.0
}, true);