> For the complete documentation index, see [llms.txt](https://docs.decentraland.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.decentraland.org/creator/content-creator-pt/scenes-sdk7/arquitetura/subscribe-to-changes.md).

# Inscrever-se nas Alterações

Detecte alterações em um component e execute funções a cada alteração

Uma maneira prática de escrever o seu código é subscrever eventos e executar uma função sempre que esse evento acontece.

Alguns [Event listeners](/creator/content-creator-pt/scenes-sdk7/interatividade/event-listeners.md) vêm predefinidos como parte do SDK, mas também pode usar o `onChange()` método em qualquer component para obter o mesmo resultado. Isto também funciona com qualquer [Custom Component](/creator/content-creator-pt/scenes-sdk7/arquitetura/custom-components.md) component que tenha definido, sem precisar de qualquer trabalho extra.

Por exemplo, a função seguinte verifica o `AvatarEquippedData` component na entidade do jogador e executa uma função se o jogador alterar qualquer um dos Wearables ou Emotes equipados. Os novos valores do component são passados como argumentos da função.

```ts
import { AvatarEquippedData } from '@dcl/sdk/ecs'

export function main() {
	AvatarEquippedData.onChange(engine.PlayerEntity, (equipped) => {
		if (!equipped) return
		console.log('Nova lista de wearables: ', equipped.wearableUrns)
		console.log('Nova lista de emotes: ', equipped.emoteUrns)
	})
}
```

Graças ao `onChange()` método, não é necessário criar um System e verificar iterativamente novos valores a cada frame; isso simplifica muito este caso de uso muito comum.

{% hint style="warning" %}
**📔 Observação**: Não use `onChange()` dentro de um System, pois isso subscreveria uma nova cópia da função em cada frame do game loop e poderia potencialmente causar travamentos.
{% endhint %}

O mesmo método funciona de imediato com [Custom Component](/creator/content-creator-pt/scenes-sdk7/arquitetura/custom-components.md). Por exemplo:

```ts
// definir component
export const MyComponent = engine.defineComponent('myComponent', {
	value1: Schemas.Boolean,
	value2: Schemas.Float,
})

// Utilização
export function main() {
	// Criar entidades
	const myEntity = engine.addEntity()

	// Criar instâncias do component
	MyComponent.create(myEntity, {
		value1: true,
		value2: 10,
	})

	// Subscrever alterações
	MyComponent.onChange(myEntity, (componentData) => {
		if (!componentData) return
		console.log(componentData.value1)
		console.log(componentData.value2)
	})
}
```

Também pode combinar esta abordagem com [Consultando Components](/creator/content-creator-pt/scenes-sdk7/arquitetura/querying-components.md), para subscrever em massa todas as Entities na Scene que tenham um determinado component à sua própria função.

```ts
export function main() {
	for (const [entity] of engine.getEntitiesWith(MyComponent)) {
		MyComponent.onChange(entity, (componentData) => {
			if (!componentData) return
			console.log(componentData.value1)
			console.log(componentData.value2)
		})
	}
}
```

Note que esta abordagem só irá subscrever `onChange()` para Entities que existam no início da Scene, por exemplo Entities criadas através da UI do [Creator Hub](/creator/content-creator-pt/scene-editor/primeiros-passos/about-editor.md).

{% hint style="info" %}
**💡 Dica**: Se preferir, em vez disso, lidar com eventos que não estejam necessariamente relacionados com a alteração de um component, recomendamos importar a biblioteca TypeScript [Mitt](https://www.npmjs.com/package/mitt) para a sua Scene. Esta biblioteca oferece funções simples para emitir e ouvir eventos.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.decentraland.org/creator/content-creator-pt/scenes-sdk7/arquitetura/subscribe-to-changes.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
