Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android原生版本, cocos的API game.restart() 返回值为 undefined . #17924

Closed
finscn opened this issue Nov 27, 2024 · 8 comments
Closed
Assignees
Labels

Comments

@finscn
Copy link
Contributor

finscn commented Nov 27, 2024

Cocos Creator version

3.8.4

System information

android

Issue description

从cocos源码里来看, 应该返回一个 Promise 对象, 小游戏和web版本来看, 也确实是 Promise .
但是 Android原生版本 返回 undefined .

原生在 jsb-game.js 里被替换成了

cc.game.restart = function () {
    // Need to clear scene, or native object destructor won't be invoke.
    cc.director.getScene().destroy();
    cc.Object._deferredDestroy();
    __restartVM();
};

此处应该保证 返回值 与 web/小游戏 平台一致, 哪怕返回一个空的/虚假的 Promise对象 也好.

Relevant error log output

No response

Steps to reproduce

.

Minimal reproduction project

No response

@finscn finscn added Bug Needs Triage Needs to be assigned by the team labels Nov 27, 2024
@cumoody

This comment was marked as resolved.

@minggo minggo self-assigned this Jan 14, 2025
@minggo minggo added this to the 3.8.6 milestone Jan 14, 2025
@minggo minggo removed the Needs Triage Needs to be assigned by the team label Jan 14, 2025
@minggo
Copy link
Contributor

minggo commented Jan 15, 2025

在原生平台,因为整个虚拟机重启了,所以没法执行 promise。按照我上面的修改没有效果。

@minggo minggo closed this as completed Jan 15, 2025
@minggo minggo removed this from the 3.8.6 milestone Jan 15, 2025
@finscn
Copy link
Contributor Author

finscn commented Jan 15, 2025

@minggo 其实只要能重启游戏, 这个函数的使命就完成了.
后面的 promise 链是否被执行没那么重要. 重要的是不要返回空. 且有一样的api , 哪怕什么都不做也可以.

目前cocos中执行下面的代码 原生平台会报错:

cc.game.restart().then(()=>{
    console.log('game restart') 
})

d.ts 里也没有说 cc.game.restart() 可能返回 undefined.

两个修改思路:

  1. 最简单的办法是 在 d.ts 里 让 cc.game.restart 的返回值可以为 undefined. 这样开发者就知道自己去判断. (但是我不喜欢这种)
  2. 返回 promise对象, 但是在 restart 里告诉开发者 这个 promise 不会被执行. 比如
cc.game.restart = function () {

   CC_LOG_WARNING('在此处告诉开发者 原生平台后面的 promise 不会被执行')

    // Need to clear scene, or native object destructor won't be invoke.
    cc.director.getScene().destroy();
    cc.Object._deferredDestroy();
    __restartVM();
   return Promise.resolve();
};

我个人更喜欢 方案2

这样 在ts代码里 执行

cc.game.restart().then(()=>{
    console.log('game restart') 
})

至少不会报错. 虽然 console.log('game restart') 可能不会被正确执行.

否则我还要自己去判断 cc.game.restart() 返回值. 而且 d.ts 里也没有说 cc.game.restart() 可能返回 undefined.

@minggo
Copy link
Contributor

minggo commented Jan 15, 2025

现在对外导出的 web 的 api。你说的报错是啥意思?

@finscn
Copy link
Contributor Author

finscn commented Jan 15, 2025

@minggo
执行

cc.game.restart().then(()=>{
    console.log('game restart') 
})

报错啊. 因为原生平台 cc.game.restart() 返回值是 undefined. 类似

Uncaught TypeError: Cannot read properties of undefined (reading 'then')
    at <anonymous>:1:3

这样的错误

@minggo
Copy link
Contributor

minggo commented Jan 15, 2025

我试试。原生的话,restart 已经重启了虚拟机,then 的代码应该都不会执行了。你是哪个平台测试的?

@minggo
Copy link
Contributor

minggo commented Jan 15, 2025

我在 mac 测试的,没看到报错。

@finscn
Copy link
Contributor Author

finscn commented Jan 15, 2025

我试试。原生的话,restart 已经重启了虚拟机,then 的代码应该都不会执行了。你是哪个平台测试的?

taptap 安卓 apk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants