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

Discussion and information relevant to creating special missions, new ships, skins etc.

Moderators: winston, another_commander

User avatar
Tricky
---- E L I T E ----
---- E L I T E ----
Posts: 821
Joined: Sun May 13, 2012 11:12 pm
Location: Bradford, UK. (Anarchic)

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

Post 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
        }
    }
}
DarkSoul
Average
Average
Posts: 11
Joined: Sat Jun 01, 2013 1:59 am

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

Post by DarkSoul »

Thanks for help but I tryed this script and dont work
User avatar
Diziet Sma
---- E L I T E ----
---- E L I T E ----
Posts: 6311
Joined: Mon Apr 06, 2009 12:20 pm
Location: Aboard the Pitviper S.E. "Blackwidow"

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

Post 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.
Most games have some sort of paddling-pool-and-water-wings beginning to ease you in: Oolite takes the rather more Darwinian approach of heaving you straight into the ocean, often with a brick or two in your pockets for luck. ~ Disembodied
Post Reply