首页 > 解决方案 > 我正在尝试在 spark ar 中为我的游戏制作评分功能,但 promise.all 似乎不起作用

问题描述

我对编程很陌生,我正在尝试使用脚本来修补桥接技术。我不知道为什么这不起作用,这是我的代码:

const Scene = require('Scene');
const Patches = require('Patches');
export const Diagnostics = require('Diagnostics');

const patchValue = Patches.getScalarValue('GameScore');

Promise.all([
    Scene.root.findFirst("sampletxt"), 

   ]).then(function(results){
    
    var ScoreText = results[0];
    ScoreText.text = patchValue.toString();

    }); 

Diagnostics.watch('runtime -', patchValue); // im using this line to see if the script reads the value from patch.

这是来自控制台日志的错误:

改用PatchesModule.outputs.getScalar!@爱马仕运行时

此 API 已弃用,请升级到最新的 SDK 版本。信息:

@
HermesRuntime

Possible Unhandled Promise Rejection: Unexpeted SceneObject reference: {"identifier":"2_d_text_model:7030-1be1a0e4-3bb6-4a3b-bc9c-e17b7b57aa6c","name":"sampletxt","materialIdentifier":"","className":"planarText","modelId":505}

请帮助我,谢谢

标签: javascriptnode.jspromise.all

解决方案


  1. GameScore 从 Patches 接收一个数值并将其发送给 Script。
  2. ScoreText 从 GameScore 获取值并保留它。
  3. outputScore 从 ScoreText 中获取值并将其发送回 Patches。
  4. 在补丁中,我们将 outputScore Patch 连接到 sampletxt 的文本补丁。

补丁布局

const Patches = require("Patches");

//onStart Set GameScore Value
Patches.outputs.getScalar("GameScore").then((event) => {
  var ScoreText = event.pinLastValue();
  Patches.inputs.setString("outputScore", ScoreText.toString());

  //onChange Update GameScore Value
  event.monitor().subscribe(function (values) {
    ScoreText = values.newValue;
    Patches.inputs.setString("outputScore", ScoreText.toString());
  });
});

推荐阅读