# Examples

## Cocos Creator GDK Examples

You can check outthe example code in the package at `negdk/assets/negdk/sample/NEGdkSample.ts`

### Initialize the GDK

```ts
export class ExampleInit extends Component {
    protected onLoad(): void {
        const config: NEGdkConfig = {
            gameKey: "eternal",
            platformSource: "ETERNAL",
            ramperAppId: "vgzjondflu",
            scheme: "negdkcc",
            subPlatform: "",
            environment: NEEnvironment.DEV,
            loginCacheEnabled: true
        }
        NEGdk.getInstance().initialize(config, (errorCode) => {
            const success = errorCode === NEErrorCode.SUCCESS;
            if (success) {
                conslose.log("GDK initialize successfully!");
            } else {
                conslose.error(`GDK initialize failed, errorCode = ${errorCode}`);
                // Check if already login in case you are using login cache
                if (negdk.isLoggedIn()) {
                    // already login 
                } else {
                    // not login yet
                }
            }
        });
    }
}
```

### Login

#### Social Login

```ts
export class ExampleSocialLogin extends Component {

    private loginWithGoogle() {
        NEGdk.getInstance().loginWithGoogle(this.onLoginCompleted.bind(this));
    }

    private loginWithFacebook() {
        NEGdk.getInstance().loginWithFacebook(this.onLoginCompleted.bind(this));
    }

    private loginWithEmail() {
        NEGdk.getInstance().loginWithEmail(this.onLoginCompleted.bind(this));
    }

    private loginWithTelegram() {
        NEGdk.getInstance().loginWithTelegram(this.onLoginCompleted.bind(this));
    }

    private loginWithApple() {
        NEGdk.getInstance().loginWithApple(this.onLoginCompleted.bind(this));
    }

    private onLoginCompleted(errorCode: number, data: NELoginData) {
        const success = errorCode === NEErrorCode.SUCCESS;
        if (success) {
            console.log(`Login success, authen token: ${data.authToken}`});
        } else {
            console.log(`Login failed, errorCode: ${errorCode}`});
        }
    }
}
```

#### Create/Restore Wallet

```ts
export class ExampleWalletLogin extends Component {

    private createWalletAndLogin(): string {
        // Create a new wallet
        const phrase = NEGdk.getInstance().createWallet();

        // Login with new wallet
        NEGdk.getInstance().loginWithPhrase(phrase, this.onLoginCompleted.bind(this));
    }

    private restoreWallet() {
        // Allow user to enter seedphrase
        const phrase = this.editBoxPhrase.string;

        // Restore wallet and login
        NEGdk.getInstance().loginWithPhrase(phrase, this.onLoginCompleted.bind(this));
    }

    private onLoginCompleted(errorCode: number, data: NELoginData) {
        const success = errorCode === NEErrorCode.SUCCESS;
        if (success) {
            console.log(`Login success, authen token: ${data.authToken}`});
        } else {
            console.log(`Login failed, errorCode: ${errorCode}`});
        }
    }
}
```

### Get User Pets Data

```ts
export class ExampleUserPets extends Component {
    private requestUserPets() {
        NEGdk.getInstance().requestUserPets((response) => {
            if (!response.errorCode) {
                var pets = response.data; // get user pets data from response
            } else {
                // error handling
            }
        });
    }
}
```

### Get User Balances Data

```ts
export class ExampleUserBalances extends Component {
    private requestUserBalances() {
        NEGdk.getInstance().requestUserBalances((response,) => {
            if (!response.errorCode) {
                var balances = response.data; // get user balances data from response
            } else {
                // error handling
            }
        });
    }
}
```

### Get Shop Data

```ts
export class ExampleShop extends Component {
    private requestShopInfo() {
        NEGdk.getInstance().requestShopInfo((response) => {
            if (!response.errorCode) {
                var shopPacks = response.data; // get shop packs data from response
            } else {
                // error handling
            }
        });
    }
}
```

### Buy a Pack in Shop

```ts
export class ExampleShopBuy extends Component {
    private buyShopPack() {
        var packId = 26;
        var amount = 1;
        var currency = "SPACE_STONE";
        NEGdk.getInstance().buyShopPack(packId, amount, currency, (response) => {
            if (!response.errorCode) {
                var remainBalance = response.data; // user balance for the currency after purchase
            } else {
                // Failed to buy the pack!!!
            }
        });
    }
}
```


---

# 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.eternals.game/gdk-documentation/cocos-creator/examples.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.
