getComponent() in update()

In Unity, it is not recommended to GetComponent() in Update() as it affects performance. Does using getComponent() heavily in update() affect the framerate in Cocos Creator too?

can you provide more details on how you plan to use them?

For example, on a node I have something like this in update():

if (this.targeting !== null && this.targeting.getComponent('Trashcan').currentState !== TRASHCANSTATE.dirty) { }

If all you have to do is check if currentState has changed, then why not just fire an event when happens? You only need to program what will happen next, and subscribe that logic to the event.

Thanks. So, putting getComponent() in update() does affect performance in Cocos Creator too like in Unity?

I would think so, yes.

hi @jrosich,
could you provide code for creating an event that will fire when a value changes?
Also, please provide code for how to call the function.
thanks!

If you do not create components in runtime, you could get and save the reference to the TrashCan component in the start() method and use this reference later on. There is no reason to call getComponent() in the update() method.