Last active
March 15, 2017 00:02
-
-
Save humbletim/d0a5019c371eb85b8b5b202fa4571b43 to your computer and use it in GitHub Desktop.
Entity unload resource leaks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var TROMBONE_URL = "https://s3.amazonaws.com/hifi-public/tony/audio/sad-trombone.wav"; | |
return { | |
_resource: SoundCache.prefetch(TROMBONE_URL), | |
_sound: undefined, | |
_onStateChanged: function() { | |
var resource = this._resource; | |
if (resource.state === Resource.State.FINISHED) { | |
resource.stateChanged.disconnect(this, '_onStateChanged'); | |
this._sound = SoundCache.getSound(TROMBONE_URL); | |
this.play(); | |
} else if (resource.state === Resource.State.FAILED) { | |
resource.stateChanged.disconnect(this, '_onStateChanged'); | |
this._sound = null; | |
print("Failed to download sound: " + TROMBONE_URL); | |
} | |
}, | |
preload: function(uuid) { | |
print("++ resource check preload", uuid); | |
this._resource.stateChanged.connect(this, '_onStateChanged'); | |
this._onStateChanged(); | |
}, | |
play: function(uuid, args) { | |
if (this.tromboneInjector) { | |
this.tromboneInjector.restart(); | |
} else { | |
this.tromboneInjector = Audio.playSound(this._sound, { | |
position: MyAvatar.position, | |
volume: .01, | |
loop: true, | |
localOnly: true, | |
}); | |
} | |
}, | |
stop: function(uuid, args) { | |
if (this.tromboneInjector) { | |
this.tromboneInjector.stop(); | |
} | |
}, | |
unload: function(uuid) { | |
print("-- resource check unload", uuid); | |
this.stop(); | |
if (this._sound === undefined) { | |
this._resource.stateChanged.disconnect(this, '_onStateChanged'); | |
} | |
}, | |
}; | |
}) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var uuid = Entities.addEntity({ | |
type: 'Sphere', | |
position: MyAvatar.position, | |
collisionless: true, | |
script: Script.resolvePath('').replace('.js','-entity.js'), | |
lifetime: 60 | |
}); | |
print('== created entity', uuid); | |
Script.scriptEnding.connect(function() { Entities.deleteEntity(uuid); }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment