> 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-ko/sdk7/architecture/subscribe-to-changes.md).

# 변경 사항 구독

컴포넌트의 변경 사항을 감지하고 변경될 때마다 함수를 실행하세요

코드를 작성하는 깔끔한 방법은 이벤트를 구독하고, 그 이벤트가 발생할 때마다 함수를 실행하는 것입니다.

여러 가지 [이벤트 리스너](/creator/content-creator-ko/sdk7/interactivity/event-listeners.md) 는 SDK의 일부로 미리 정의되어 있지만, 또한 다음을 사용할 수도 있습니다 `onChange()` 메서드를 어떤 컴포넌트에든 사용하여 같은 효과를 얻을 수 있습니다. 이 방법은 또한 어떤 [커스텀 컴포넌트](/creator/content-creator-ko/sdk7/architecture/custom-components.md) 사용자가 정의한 것과도 추가 작업 없이 사용할 수 있습니다.

예를 들어, 다음 함수는 다음을 확인합니다 `AvatarEquippedData` 플레이어 엔티티의 컴포넌트를 확인하고, 플레이어가 착용한 웨어러블이나 이모트 중 하나라도 변경되면 함수를 실행합니다. 컴포넌트의 새 값은 함수 인자로 전달됩니다.

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

export function main() {
	AvatarEquippedData.onChange(engine.PlayerEntity, (equipped) => {
		if (!equipped) return
		console.log('새 착용 아이템 목록: ', equipped.wearableUrns)
		console.log('새 이모트 목록 : ', equipped.emoteUrns)
	})
}
```

다음 덕분에 `onChange()` 메서드 덕분에 시스템을 만들고 매 프레임마다 새 값을 반복해서 확인할 필요가 없으며, 이 매우 흔한 사용 사례를 크게 단순화해 줍니다.

{% hint style="warning" %}
**📔 참고**: 사용하지 마세요 `onChange()` System 안에서 사용하면 게임 루프의 매 프레임마다 함수의 새 복사본이 구독되므로, 잠재적으로 충돌로 이어질 수 있습니다.
{% endhint %}

같은 메서드는 다음과도 바로 사용할 수 있습니다 [커스텀 컴포넌트](/creator/content-creator-ko/sdk7/architecture/custom-components.md). 예를 들면:

```ts
// 컴포넌트 정의
export const MyComponent = engine.defineComponent('myComponent', {
	value1: Schemas.Boolean,
	value2: Schemas.Float,
})

// 사용법
export function main() {
	// 엔티티 생성
	const myEntity = engine.addEntity()

	// 컴포넌트 인스턴스 생성
	MyComponent.create(myEntity, {
		value1: true,
		value2: 10,
	})

	// 변경 사항 구독
	MyComponent.onChange(myEntity, (componentData) => {
		if (!componentData) return
		console.log(componentData.value1)
		console.log(componentData.value2)
	})
}
```

이 접근 방식은 다음과도 함께 사용할 수 있습니다 [컴포넌트 조회](/creator/content-creator-ko/sdk7/architecture/querying-components.md), 특정 컴포넌트를 가진 씬의 모든 엔티티를 각자의 함수에 일괄 구독하는 데 사용됩니다.

```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)
		})
	}
}
```

참고로, 이 접근 방식은 다음에만 구독합니다 `onChange()` 씬 시작 시 존재하는 엔티티에 대해서만, 예를 들어 다음을 통해 생성된 엔티티 [크리에이터 허브](/creator/content-creator-ko/scene-editor/get-started/about-editor.md).

{% hint style="info" %}
**💡 팁**: 대신 컴포넌트 변경과 반드시 관련되지 않은 이벤트를 처리하고 싶다면, TypeScript 라이브러리 [Mitt](https://www.npmjs.com/package/mitt) 를 씬에 가져오는 것을 권장합니다. 이 라이브러리는 이벤트를 발생시키고 수신하는 간단한 함수를 제공합니다.
{% 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.decentraland.org/creator/content-creator-ko/sdk7/architecture/subscribe-to-changes.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
