# Storage

### App storage에 데이터 저장하기

> **권장하는 데이터 저장 방식**

{% hint style="info" %}
App.setStorage(string);
{% endhint %}

App.setStorage 함수는 기존 App storage 데이터 저장 방식을 보완한 데이터 저장 함수입니다.

> **권장하지 않는 데이터 저장 방식**

{% hint style="danger" %}
App.storage = string;\
App.save();
{% endhint %}

위  방식은 앱이 같은 스페이스 내 여러 맵에서 실행중인 경우, 데이터를 덮어쓰는 등의 문제가 발생할 수 있어 사용을 권장드리지 않습니다.

### App storage 값 읽어오기

{% hint style="info" %}
App.getStorage(function(){})
{% endhint %}

App.getStorage 함수는 앱이 실행중인 같은 스페이스 내 다른 맵의  App storage 데이터 변경 여부를 체크하여 같은 데이터를 가지도록 동기화 해주는 함수입니다.&#x20;

{% hint style="warning" %}
App.getStorage 함수는 비동기 함수이기 때문에 App.getStorage 함수 다음 라인에 App.storage를 사용하는 코드를 작성할 경우 동기화를 보장할 수 없습니다.
{% endhint %}

###

### App storage 사용 예제

아래 예제코드를 사이드바 앱으로 설치해 같은 스페이스 내 다른 맵에서 Q를 눌러 count 값이 동기화되는지 확인해보세요.

```javascript

App.onStart.Add(function(){
	if(App.storage == null){
		App.setStorage(JSON.stringify({count: 0}))
	}
})
// Q 키를 누르면 동작하는 함수
App.addOnKeyDown(81,function(player){
	// App.storage를 최신화 한 후 callback 함수를 실행합니다.
	App.getStorage(function () {
		let appStorage = JSON.parse(App.storage);
		appStorage.count += 1;
		App.sayToAll(`count: ${appStorage.count}`)
		// App.setStorage를 사용해 변경내용을 저장합니다.
		App.setStorage(JSON.stringify(appStorage));
	});
	// App.getStorage 함수 다음 라인에 App.storage 코드를 작성할 경우 동기화를 보장할 수 없습니다.
	App.sayToAll(App.storage);
})
```


---

# Agent Instructions: 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:

```
GET https://docs-kr.zep.us/zep-script-api/zepscriptapi/scriptapp/storage.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
