I’ve had similar problems with adding sounds to doors and other triggered mechanism.
The Sound entity has a ‘active’ property, and when you change the value from false to true, the sound will start playing. If the sound asset in the Sound entity is a continuous loop, then you can switch the active property to false and true again, and the sound will stop and continue. But for sounds that are not looped, like the open door sound, this won’t work. Once the sound is finished playing, switching the active property won’t help.
I have found a work-around: you can re-set the Sound entity’s ‘sound’ property, i.e. the sound asset used by the Sound enity. By first clearing the sound’s sound property, and then setting it back to the original value, the entity’s state is modified in such a way that when you set the active propery to true again, it will play again.
You could modify the lockedDoor script like this:
instead of:
self.properties.sound.active = true
you could write:
– get sound entity and asset
local soundEntity = self.properties.sound
local soundAsset = soundEntity.sound
– stop the sound first, if it was running, and clear the sound asset
soundEntity.active = false
soundEntity.sound = nil
– re-set the asset and start playing
soundEntity.sound = soundAsset
soundEntity.active = true
(And if you use it frequently, better wrap it in a function)
You could also use sound assets in scripts directly, instead of Sound entities, but in the basic tutorials, Sound entities are used, and I think that is the nicest way to get beginners started, and the Sound Entity has some handy extra properties such as volume, pitch, range, fall-off, etc.
If only the Sound entity was a bit more helpful in these cases! I think i 'll put in a feature request for some handy functions on Sound entities to start, stop and restart sounds. Same for Effect entities, where non-looping effects like explosions will only run once. If these entities have a few extra functions, then users can connect these sounds and effects to events, without have to write scripts.