History

apply default userData values; add error checking; throttle toggle clicks

humbletim committed 9 years ago

Commit 1048c2a1 parent 7025ed51 file · +52 −15Browse files

Showing the changes to scripts/tutorials/entity_scripts (1 of 1 file). Show all

Unified · Split

scripts/tutorials/entity_scripts/ambientSound.js+52−15
@@ -27,6 +27,14 @@ · 26 unmodified lines
2727 var DEFAULT_URL = "http://hifi-content.s3.amazonaws.com/ken/samples/forest_ambiX.wav";
2828 var DEFAULT_VOLUME = 1.0;
2929
30+ var DEFAULT_USERDATA = {
31+ soundURL: DEFAULT_URL,
32+ range: DEFAULT_RANGE,
33+ maxVolume: DEFAULT_VOLUME,
34+ disabled: true,
35+ grabbableKey: { wantsTrigger: true },
36+ };
37+
3038 var soundURL = "";
3139 var soundOptions = {
3240 loop: true,
@@ -60,7 +68,13 @@ · 27 unmodified lines
6068 var oldSoundURL = soundURL;
6169 var props = Entities.getEntityProperties(entity, [ "userData" ]);
6270 if (props.userData) {
63− var data = JSON.parse(props.userData);
71+ try {
72+ var data = JSON.parse(props.userData);
73+ } catch(e) {
74+ debugPrint("unable to parse userData JSON string: " + props.userData);
75+ this.cleanup();
76+ return;
77+ }
6478 if (data.soundURL && !(soundURL === data.soundURL)) {
6579 soundURL = data.soundURL;
6680 debugPrint("Read ambient sound URL: " + soundURL);
@@ -125,18 +139,27 @@ · 58 unmodified lines
125139 return;
126140 }
127141 var props = Entities.getEntityProperties(entity, [ "userData", "age", "scriptTimestamp" ]);
142+ if (!props.userData) {
143+ debugPrint("userData is empty; ignoring " + hint);
144+ this.cleanup();
145+ return;
146+ }
128147 var data = JSON.parse(props.userData);
129148 data.disabled = !data.disabled;
130149
131150 debugPrint(hint + " -- triggering ambient sound " + (data.disabled ? "OFF" : "ON") + " (" + soundURL + ")");
132151
133152 this.cleanup();
153+ // Prevent rapid-fire toggling (which seems to sometimes lead to sound injectors becoming unstoppable)
154+ this._toggle = this.clickDownOnEntity = function(hint) { debugPrint("ignoring additonal clicks" + hint); };
134155
135− // Save the userData and bump scriptTimestamp, which causes all nearby listeners to apply the state change
136− Entities.editEntity(entity, {
137− userData: JSON.stringify(data),
138− scriptTimestamp: Math.round(props.age * 1000)
139− });
156+ Script.setTimeout(function() {
157+ // Save the userData and bump scriptTimestamp, which causes all nearby listeners to apply the state change
158+ Entities.editEntity(entity, {
159+ userData: JSON.stringify(data),
160+ scriptTimestamp: props.scriptTimestamp + 1
161+ });
162+ }, 250);
140163 };
141164
142165 this._updateColor = function(disabled) {
@@ -155,21 +178,35 @@ · 12 unmodified lines
155178
156179 this.preload = function(entityID) {
157180 // Load the sound and range from the entity userData fields, and note the position of the entity.
158− debugPrint("Ambient sound preload");
181+ debugPrint("Ambient sound preload " + entityID);
159182 entity = entityID;
160183 _this = this;
161184
162− var props = Entities.getEntityProperties(entity, [ "userData" ]);
185+ var props = Entities.getEntityProperties(entity, [ "name", "userData" ]);
186+ var data = {};
163187 if (props.userData) {
164− var data = JSON.parse(props.userData);
165− this._updateColor(data.disabled);
166− if (data.disabled) {
167− _this.maybeUpdate();
168− return;
188+ data = JSON.parse(props.userData);
189+ }
190+ var changed = false;
191+ for(var p in DEFAULT_USERDATA) {
192+ if (!(p in data)) {
193+ data[p] = DEFAULT_USERDATA[p];
194+ changed = true;
169195 }
170196 }
171−
172− checkTimer = Script.setInterval(_this.maybeUpdate, UPDATE_INTERVAL_MSECS);
197+ if (!data.grabbableKey.wantsTrigger) {
198+ data.grabbableKey.wantsTrigger = true;
199+ changed = true;
200+ }
201+ if (changed) {
202+ debugPrint("applying default values to userData");
203+ Entities.editEntity(entity, { userData: JSON.stringify(data) });
204+ }
205+ this._updateColor(data.disabled);
206+ this.updateSettings();
207+ if (!data.disabled) {
208+ checkTimer = Script.setInterval(_this.maybeUpdate, UPDATE_INTERVAL_MSECS);
209+ }
173210 };
174211
175212 this.maybeUpdate = function() {