Page 2 of 2

Re: Need help with Script: How can I play a sound?

Posted: Fri Jun 21, 2013 2:10 am
by Tricky
I cleaned a few bits up. You assigned the SoundSource as a private variable in condition 0 which means it wasn't available to the rest of the script.

Try this as your world script.

Code: Select all

this.name = "test";
this.author = "DarkSoul";
this.copyright = "Do what you want with it";
this.description = "test";
this.version = "1.0";

/* Setup the sound source for use. */
this.startUp = function () {
    /* Use public variable that can be accessed by the rest of the script. */
    this.$testSound = new SoundSource;
    /* Play once, no repeat. */
    this.$testSound.loop = false;
    /* Default custom sound assignment. */
    this.$testSound.sound = "[test_01]";
}

this.alertConditionChanged = function (newCondition, oldCondition) {
    switch (newCondition) {
    case 0: // we're docked
        {
            this.$testSound.sound = "[test_docked]";
            this.$testSound.play();
        }
        break;
    case 1: // we're at green alert
    case 2: // or we're at yellow alert
        {
            if (player.ship.energy > 108) // if we're not using the damaged HUD
            {
                this.$testSound.sound = "[test_alert1or2]";
                this.$testSound.play();
            }
        }
        break;
    case 3: // we're at red alert
        {
            if (player.alertHostiles && player.ship.energy > 108) // and under attack and not using the damaged HUD
            {
                this.$testSound.sound = "[test_alert3]";
                this.$testSound.play();
            }
        }
        break;
    }
}

this.shipLaunchedFromStation = function () {
    this.$energyCheckTimer = new Timer(this, this.$energyCheck, 0, 2) // use a timer to keep an eye on the HUD state
}

this.shipDied = this.$stopEnergyCheckTimer = function () {
    if (this.$energyCheckTimer) {
        if (this.$energyCheckTimer.isRunning) {
            this.$energyCheckTimer.stop();
        }

        delete this.$energyCheckTimer;
    }
}

this.$energyCheck = function () {
    if (player.ship.docked) {
        this.$stopEnergyCheckTimer();
    } else {
        if (player.ship.energy < 109) {
            if (!this.$testSound.isPlaying) {
                /* Start the sound playing if it has stopped. */
                this.$testSound.play();
            }
        } else if (player.ship.hud == "kwsnafuhud.plist") {
            this.alertConditionChanged(player.alertCondition, 0); // if energy is >49 and we're still displaying the damaged HUD, use other code to repair
        }
    }
}

Re: Need help with Script: How can I play a sound?

Posted: Mon Jun 24, 2013 3:50 am
by DarkSoul
Thanks for help but I tryed this script and dont work

Re: Need help with Script: How can I play a sound?

Posted: Mon Jun 24, 2013 6:31 am
by Diziet Sma
Hmm.. after you make changes to your script, are you making sure that Oolite refreshes its cache?

To do this, when you start Oolite, you must hold down the shift key and keep it held down until you see the spinning Cobra MkIII. If you don't do this, Oolite won't notice your changes and will continue to use the old version of your script stored in the cache.

This is necessary every time you make a change to a script.