我引用并复制了JavaScript脚本,该脚本从OpenWeather API获取天气信息,而我复制的脚本在AWS Sumerian中无法正常工作。
下面是我参考并尝试过的教程网站。
https://docs.sumerian.amazonaws.com/articles/weather-report-guest/
我个人认为脚本中的'fetch()'函数没有得到任何响应,也不知道为什么。
我要实现的是从开放的API获取天气信息,并让3D模型主机使用AWS Sumerian(3D建模工具)和Amazon Polly(文本到语音)服务来讲这些天气信息。
首先,此Sumerian由两种开发工具组成。一种是称为“状态机”的流程图工具,它可以控制Sumerian中的3D模型。 就像'MIT Scratch'一样,并且可以自行设置脚本条件。
另一个是从状态机基于JavaScript创建的“脚本”。以下是脚本API的参考网站。
https://content.sumerian.amazonaws.com/engine/latest/docs/
因此,在状态机中,我创建了2个状态,下面是我创建的状态。
Get Weather(On Get Weather Script Success, On Get Weather Failure)
↓
Tell Weather(On Tell Weather Script Success, On Tell Weather Script Failure)
问题是,在“获取天气”状态下,即使失败也没有来自“获取天气脚本”的响应。
请检查脚本(获取天气,告诉天气),然后回复代码中是否有错误或指出我在脚本中犯的任何错误。
获取天气脚本
'use strict';
/* global sumerian */
const WEATHER_URL = 'https://api.openweathermap.org/data/2.5/weather?q=seoul&appid=your openweathermap api key here'
// When used in a ScriptAction, called when a state is entered.
//
function enter(args, ctx) {
const { latitude, longitude } = ctx.behaviorData.payload; // 1
fetch('${WEATHER_URL}&lat={latitude}&lon=${longitude}') // 2
.then(response => response.json())
.then(data => {
ctx.behaviorData.payload.weather = data.weather[0].description; // 3
ctx.transitions.success(); // 4
});
}
// Defines script parameters.
//
var parameters = [];
Chrome控制台消息
[Sumerian] Loading engine version 0.25.201907101851+9b630ed with enabled features: core, bootstrapper, gizmo, sceneloader, animation, aws, debug, dom3d, host, particle, physics, cannonphysics, posteffect, timeline.
content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/sumerian.js:14 [Sumerian] Loaded Sumerian Engine Version: 0.25.201907101851+9b630ed
content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/sumerian.js:1 [Sumerian] Using WebGL2
content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/sumerian.js:14 [Sumerian] An unhandled error has occurred: SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at SpeechManager._parseSpeechMarks (content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/awspack.js:2)
at content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/awspack.js:2
at async Promise.all (ap-northeast-2.sumerian.amazonaws.com/edit/index 1)
(anonymous) @ content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/sumerian.js:14
(unknown) Cannot establish connection - target does not exist
content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/sumerian.js:14 [Sumerian] An unhandled error has occurred: SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at SpeechManager._parseSpeechMarks (content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/awspack.js:2)
at content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/awspack.js:2
at async Promise.all (ap-northeast-2.sumerian.amazonaws.com/edit/index 1)
(anonymous) @ content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/sumerian.js:14
content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/sumerian.js:1 [Sumerian] An error occurred at line 11 when calling the "Get Weather Script" script's "enter" function. Cannot destructure property `latitude` of 'undefined' or 'null'. TypeError: Cannot destructure property `latitude` of 'undefined' or 'null'.
at ScriptProxy_ScriptProxy.enter (ap-northeast-2.sumerian.amazonaws.com/edit/sumerian-custom-scripts/b70ff1b210bf482981c931bf6c4719d6.script.js?v=5:27)
at ScriptSystem.callScriptFunction (content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/sumerian.js:1)
at ScriptAction_ScriptAction.enter (content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/sumerian.js:14)
at State._callSafe (content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/sumerian.js:1)
at State.enter (content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/sumerian.js:1)
at Machine.enter (content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/sumerian.js:1)
at StateMachineComponent.start (content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/sumerian.js:1)
at StateMachineSystem_StateMachineSystem._startEntity (content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/sumerian.js:14)
at StateMachineSystem_StateMachineSystem.start (content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/sumerian.js:1)
at StateMachineSystem_StateMachineSystem.start (content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/sumerian.js:14)
_handleScriptError @ content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/sumerian.js:1
content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/sumerian.js:1 [Sumerian] An error occurred at line 9 when calling the "Get Weather Script" script's "enter" function. Cannot destructure property `latitude` of 'undefined' or 'null'. TypeError: Cannot destructure property `latitude` of 'undefined' or 'null'.
at ScriptProxy_ScriptProxy.enter (ap-northeast-2.sumerian.amazonaws.com/edit/sumerian-custom-scripts/b70ff1b210bf482981c931bf6c4719d6.script.js?v=6:25)
at ScriptSystem.callScriptFunction (content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/sumerian.js:1)
at ScriptAction_ScriptAction.enter (content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/sumerian.js:14)
at State._callSafe (content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/sumerian.js:1)
at State.enter (content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/sumerian.js:1)
at Machine.enter (content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/sumerian.js:1)
at StateMachineComponent.start (content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/sumerian.js:1)
at StateMachineSystem_StateMachineSystem._startEntity (content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/sumerian.js:14)
at StateMachineSystem_StateMachineSystem.start (content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/sumerian.js:1)
at StateMachineSystem_StateMachineSystem.start (content.sumerian.amazonaws.com/engine/0/25/201907101851+9b630ed/sumerian.js:14)
告诉天气脚本
'use strict';
/* global sumerian */
function compileSpeech(speech, args) {
return speech.replace(/\$\{([^}]+)\}/g, (match, variable) => args[variable] || '');
}
// When used in a ScriptAction, called when a state is entered
//
function enter(args, ctx) {
const speech = ctx.entity.speechComponent.speeches[0];
// Put dynamic data into speech
speech.body = compileSpeech(speech.body, ctx.behaviorData.payload)
}
// Defines script parameters.
//
var parameters = [];
我希望Get Weather Script能够正常工作并将JSON数据提供给Tell Weather Script,以便3D模型主机可以说出并告知天气。