sidd
sidd

Reputation: 669

update is not firing in aframe component

I read from the aframe docs that whenever we update the position or any other value the update of the attached component would fire I am just trying to fire the update on every position change.

This is the component:-

        AFRAME.registerComponent('checking', {
          init: function(){
            console.log("initialized");
          },
          update: function(){
            console.log("valueUpdated: "+this.el.id);
          },
          tick: function(){

          }
        });

update is being fired once with the init when the doc is loaded but not when I am doing this from the console like obj.setAttribute("position","4 6 7"); acc to the documentation it should happen right or I am making some very basic mistake?

Thanks...

Upvotes: 0

Views: 996

Answers (1)

Piotr Adam Milewski
Piotr Adam Milewski

Reputation: 14665

I think You are confusing a component with an entity. The entity is a container, which behavior and appearance are defined via components.
So the update function is firing at the beginning + when you change the component, like via setAttribute('checking','newValue').


You can do Your check either by:

including a listener for the 'componentChanged' event:

  this.el.addEventListener('componentChanged',function(e){
       if(e.detail.name==='position'){
          console.log(e.detail.newData);
       }
    });

checking if the position changed on tick, but that seems highly inneficient:

   init(){
     this.pos = this.el.getAttribute('position');
   }
   tick: function(){
     if( this.el.getAttribute('position') != this.pos ){
         this.pos = this.el.getAttribute('position');
     }
   }

Upvotes: 1

Related Questions