Need help with Script: How can I play a sound?
Moderators: winston, another_commander
Need help with Script: How can I play a sound?
Hello guy I want to play some sounds when my condition changes, like :
condition 1= I can use witchdrive(key "J")
condition 2= Mass lock (key "J")
condition 3= when attacked
I´ve tryed this:
this.alertConditionChanged = function(newCondition, oldCondition)
{
switch(newCondition)
{
case 0: // we're docked
{
notifySound.sound = "[test]";
notifySound.play();
}
case 1: // we're at green alert
case 2: // or we're at yellow alert
{
if(player.ship.energy > 108)
{
notifySound.sound = "[test]";
notifySound.play();
}
break;
But this dont works with me and I am beggining to scripts so help me please
condition 1= I can use witchdrive(key "J")
condition 2= Mass lock (key "J")
condition 3= when attacked
I´ve tryed this:
this.alertConditionChanged = function(newCondition, oldCondition)
{
switch(newCondition)
{
case 0: // we're docked
{
notifySound.sound = "[test]";
notifySound.play();
}
case 1: // we're at green alert
case 2: // or we're at yellow alert
{
if(player.ship.energy > 108)
{
notifySound.sound = "[test]";
notifySound.play();
}
break;
But this dont works with me and I am beggining to scripts so help me please
- Diziet Sma
- ---- E L I T E ----
- Posts: 6312
- 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?
You forgot to create a new SoundSource first.. (see the Oolite JavaScript Reference: SoundSource for details)
This:
should look something like:
Also, the above requires that
Rather than using a
Further, don't forget that
For one of the most stripped-down-to-basics examples of how to play a sound, take a look at my Q-Bomb Detector OXP. All it does is play a sound when a certain condition is detected.
This:
Code: Select all
{
notifySound.sound = "[test]";
notifySound.play();
}
Code: Select all
{
var notifySound = new SoundSource;
notifySound.sound = "[test]";
notifySound.play();
}
[test]
be declared in a customsounds.plist
. This would look something like:
Code: Select all
{
"[test]" = "testsound.ogg";
}
customsounds.plist
(recommended), you could supply the filename of the sound directly, in a string, as in:
Code: Select all
{
var notifySound = new SoundSource;
notifySound.sound = "testsound.ogg";
notifySound.play();
}
testsound.ogg
needs to be in a folder called 'Sounds'. For one of the most stripped-down-to-basics examples of how to play a sound, take a look at my Q-Bomb Detector OXP. All it does is play a sound when a certain condition is detected.
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
- Eric Walch
- Slightly Grand Rear Admiral
- Posts: 5536
- Joined: Sat Jun 16, 2007 3:48 pm
- Location: Netherlands
Re: Need help with Script: How can I play a sound?
Similar as above, i first define the sound in startup:
And than every time I need to play it, I use:
But you could also use:
to play the sound 10 times. Or you can set the loop to true, in which case you must stop it later with a new command.
EDIT: the method by Diziet Sma with a local 'var' is probably more memory friendly.
Code: Select all
this.startUp = function ()
{
this.mySound = new SoundSource;
this.mySound.sound = "mySound.ogg";
this.mySound.loop = false; // not needed, as it is false by default
}
Code: Select all
this.mySound.play();
Code: Select all
this.mySound.play(10);
EDIT: the method by Diziet Sma with a local 'var' is probably more memory friendly.
UPS-Courier & DeepSpacePirates & others at the box and some older versions
- Diziet Sma
- ---- E L I T E ----
- Posts: 6312
- 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?
Interesting.. as I've been considering doing something like the above.Eric Walch wrote:Similar as above, i first define the sound in startup:And than every time I need to play it, I use:Code: Select all
this.startUp = function () { this.mySound = new SoundSource; this.mySound.sound = "mySound.ogg"; this.mySound.loop = false; // not needed, as it is false by default }
Code: Select all
this.mySound.play();
The only problem the Q-Bomb Detector has at present is that the first time it's triggered in a gaming session (at least I'm pretty sure it's only the first time) there can be a few seconds delay in the siren playing. I'm guessing the delay is caused by the game needing to load in the audio file from disk. Of course, with a Q-bomb in the vicinity, that few seconds delay can be fatal.. would your method above amount to a pre-loading of the audio? And if not, is there a way that I can pre-load it, so as to eliminate that delay?
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
Re: Need help with Script: How can I play a sound?
You could probably set it to load, play and then immediately stop (ie so it doesn't actually play anything) on start-up or somewhere suitably early. That way if it is a loading/caching delay, then it should be done sometime before it'd be a problem.
My OXPs via Boxspace or from my Wiki pages .
Thargoid TV
Dropbox Referral Link
Thargoid TV
Dropbox Referral Link
- JensAyton
- Grand Admiral Emeritus
- Posts: 6657
- Joined: Sat Apr 02, 2005 2:43 pm
- Location: Sweden
- Contact:
Re: Need help with Script: How can I play a sound?
Oolite preloads all sounds in customsounds.plist, and the q-mine alarm plays instantly for me. Perhaps SDL confounds our efforts by loading sounds lazily?
E-mail: [email protected]
Re: Need help with Script: How can I play a sound?
Thanks guys so much. I will try that codes and post here a feedback, see you soon...
I hope it works.
I hope it works.
- Diziet Sma
- ---- E L I T E ----
- Posts: 6312
- 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?
Thanks for that info, Jens.. looks as if there's nothing that can be done, then.. it's only an occasional thing, fortunately, but has happened to me once or twice, and it recently helped Capt. Reynolds pick up a Darwin Award..
(on the other hand, maybe Thargoid's trick might overcome SDL's laziness..)
You're very welcome, DarkSoul.. looking forward to your feedback.
(on the other hand, maybe Thargoid's trick might overcome SDL's laziness..)
You're very welcome, DarkSoul.. looking forward to your feedback.
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
-
- Quite Grand Sub-Admiral
- Posts: 6683
- Joined: Wed Feb 28, 2007 7:54 am
Re: Need help with Script: How can I play a sound?
It is unlikely that the sound delay thing is SDL related. I am getting the qbomb alert sound playing at the same time as the message warning appears on screen with Q-mine Detector, same as Jens.
- Diziet Sma
- ---- E L I T E ----
- Posts: 6312
- 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?
Hmm.. I wonder if maybe it's related to running on a lower-spec computer? Mine has only a 1.1GHz CPU. Could be if it has a lot to keep track of at the time, it's bogging down a little..
Good thing it's a rare event, even on this machine.
Edit:
What I'm wondering about is, why is it that occasionally, from the q-bomb being launched to the message/siren routine getting triggered. there can be a delay of around 3 or so seconds? It's almost as if
Good thing it's a rare event, even on this machine.
Edit:
Well, that's as it should be anyway.. the message and sound are part of the same subroutine.another_commander wrote:I am getting the qbomb alert sound playing at the same time as the message warning appears on screen with Q-mine Detector, same as Jens.
What I'm wondering about is, why is it that occasionally, from the q-bomb being launched to the message/siren routine getting triggered. there can be a delay of around 3 or so seconds? It's almost as if
this.cascadeWeaponDetected
is -just once in a while- taking a long time to catch on to what's happening.. (not that I'm saying that's where the problem is)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
- JensAyton
- Grand Admiral Emeritus
- Posts: 6657
- Joined: Sat Apr 02, 2005 2:43 pm
- Location: Sweden
- Contact:
Re: Need help with Script: How can I play a sound?
It could be a memory thing – the sound, or information about it, is paged out and it takes the virtual memory system a while to bring it back in. If SDL is waiting for it on a background thread, the game will continue to run in the meantime.Diziet Sma wrote:Hmm.. I wonder if maybe it's related to running on a lower-spec computer? Mine has only a 1.1GHz CPU. Could be if it has a lot to keep track of at the time, it's bogging down a little..
E-mail: [email protected]
- Diziet Sma
- ---- E L I T E ----
- Posts: 6312
- 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?
That makes a lot of sense..
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
Re: Need help with Script: How can I play a sound?
Well folks sorry I am delayed but not yet can pllay any sound. I dont understand it. Help please.
- Smivs
- Retired Assassin
- Posts: 8408
- Joined: Tue Feb 09, 2010 11:31 am
- Location: Lost in space
- Contact:
Re: Need help with Script: How can I play a sound?
Can you give us some more details?
Maybe post the script here so we can check it (c&p it between 'code' tags), and tell us how you are putting it into the game.
Smivs
Maybe post the script here so we can check it (c&p it between 'code' tags), and tell us how you are putting it into the game.
Smivs
Commander Smivs, the friendliest Gourd this side of Riedquat.
Re: Need help with Script: How can I play a sound?
OK I just try use some part of GENERIC HUD script:
this.name = "test";
this.author = "DarkSoul";
this.copyright = "Do what you want with it";
this.description = "test";
this.version = "1.0";
this.alertConditionChanged = function(newCondition, oldCondition)
{
switch(newCondition)
{
case 0: // we're docked
{
var testSound = new SoundSource;
testSound.sound = "[test_01]";
testSound.play();
}
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
{
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
{
testSound.play();
}
//break;
}
}
}
this.shipLaunchedFromStation = function()
{
if(this.energyCheckTimer)
{
this.energyCheckTimer.start()
}
else
{
this.energyCheckTimer = new Timer(this, this.energyCheck,0,2) // use a timer to keep an eye on the HUD state
}
}
this.energyCheck = function()
{
if(player.ship.docked)
{
this.energyCheckTimer.stop();
}
else
{
if(player.ship.energy < 109 )
{
testSound.play();
}
if(player.ship.energy > 108 && 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
}
}
}
this.shipDied = function()
{
if(this.energyCheckTimer)
{
this.energyCheckTimer.stop()
}
}
this.name = "test";
this.author = "DarkSoul";
this.copyright = "Do what you want with it";
this.description = "test";
this.version = "1.0";
this.alertConditionChanged = function(newCondition, oldCondition)
{
switch(newCondition)
{
case 0: // we're docked
{
var testSound = new SoundSource;
testSound.sound = "[test_01]";
testSound.play();
}
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
{
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
{
testSound.play();
}
//break;
}
}
}
this.shipLaunchedFromStation = function()
{
if(this.energyCheckTimer)
{
this.energyCheckTimer.start()
}
else
{
this.energyCheckTimer = new Timer(this, this.energyCheck,0,2) // use a timer to keep an eye on the HUD state
}
}
this.energyCheck = function()
{
if(player.ship.docked)
{
this.energyCheckTimer.stop();
}
else
{
if(player.ship.energy < 109 )
{
testSound.play();
}
if(player.ship.energy > 108 && 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
}
}
}
this.shipDied = function()
{
if(this.energyCheckTimer)
{
this.energyCheckTimer.stop()
}
}