[Huawei Developers] Mayism:
1. Environment

SDK Version：agconnect-storage:1.3.0.300

Platform：Cocos Creator

Device：HUAWEI P40 Pro

AppGallery Connect:

https://developer.huawei.com/consumer/en/service/josp/agc/index.html

2. Enabling and Configuring Cloud Functions in AppGallery Connect

Note: Currently, Cloud Functions is still under beta state. To use the service, you need to send an email for application. For details, check:

https://developer.huawei.com/consum...allery-connect-Guides/agc-cloudfunction-apply

1. Create an app first and add it to a project, or select an app from the project list on the My projects page in AppGallery Connect.

Under the project, go to Build > Cloud Functions, and click Enable now.

2. Create a function based on the description in the following codelab.

https://developer.huawei.com/consumer/en/codelab/CloudFunctions/index.html#4

3. Create an HTTP trigger and save the suffix of the value of Trigger URL in the Details area as the trigger identifier of future client requests.

3. Integrating the Cloud Functions SDK in Cocos Creator

1. Integrate the SDK.

Official documentation:

https://docs.cocos.com/creator/manual/en/cocos-service/agc-applinking.html

1. On the Service panel of Cocos Creator, find Cloud Functions. Currently, the Cloud Functions SDK supports only the Android platform.

2. Before integrating a service, you need to associate the service with an app. Click Association. In the dialog box that is displayed, click Create. The Cocos console is displayed.

3. On the Cocos console, create a game.

4. Go back to Cocos Creator and create, refresh, or select an association.

5. Return to the Cloud Functions page, and enable the service.

2. Download the JSON file.

1. Go to Project settings in AppGallery Connect and download the latest agconnect-services.json file.

2. Save the downloaded agconnect-services.json file to the settings directory in your Cocos project.

4. Configuring the UI Layout

Configure buttons for triggering a cloud function, obtaining the timeout interval, and setting the timeout interval separately.

5. Cloud Functions Development

1. Initialize Cloud Functions in the start block.

start () {

this._func = huawei.agc.func.funcService;

},

2. Call Cloud Functions with input parameters.

callWithParam:function() {
this._func.wrap("testcodelab-$latest").call((err, data) => {
if (err !== null) {
console.log("Cloud Function", `error: ${JSON.stringify(err)}`);
}
console.log("Cloud Function", `result: ${JSON.stringify(data)}`);
}, {
year: "2020",
}
);
},

3. Call Cloud Functions without input parameters.

callWithoutParam:function() {
this._func.wrap("testcodelab-$latest").call((err, data) => {
if (err !== null) {
console.log("Cloud Function", `error: ${JSON.stringify(err)}`);
}
console.log("Cloud Function", `result: ${JSON.stringify(data)}`);
});
},

4. Obtain and set the timeout interval of a function.

setTime:function() {
this._func.wrap("testcodelab-$latest").setTimeout(100,3);
console.log("Cloud Function", `setTime: ` + 100);
},
getTime:function() {
let currTime = this._func.wrap("testcodelab-$latest").getTimeout();
console.log("Cloud Function", `currTime: ` + currTime);
},

6. Packaging and Testing

In Cocos Creator, go to Project > Build…, package an Android app, and install it to your device. To verify the functions, click the buttons from top to bottom.

If the following information is displayed in JavaScript logs, the integration is successful.

7. Summary

Cloud Functions allows you to keep all functions and related code on the cloud side. You only need to call the wrap.call method to trigger the functions, which reduces the code amount and package size of your APK package.

For more details, please check:

Cloud Functions development guide:

https://developer.huawei.com/consum...connect-Guides/agc-cloudfunction-introduction

Cocos documentation:

https://docs.cocos.com/creator/manual/en/cocos-service/agc-cloudfunc.html