Skip to content

Quirky Sound Object Behavior

ZephiriusZephirius Member Posts: 411
edited January 2023 in Builders - Scripting
I think the problem might lie in my code, so I'll post this right here. ;)

I have through a conversation node, a script to activate a sound object. The sound object is set to "Play Once", yet it appears to fire off at random intervals, totally breaking the intended immersion of the unfolding scene.
The sound object initially fires once, plays and ends smoothly. The problem lies when you exit the map and return. When you do the sound object seemingly fires randomly. The truth is I don't know if the return has anything to do with the sound object playing again.

I just know the sound object is set to play once and is activated by code. It plays as scripted, but afterwards, seems to play randomly.

code -
#include "x3_inc_string"

void main()
{
    object oPC = GetPCSpeaker();

    DestroyObject(GetObjectByTag("TOMB_BLOCK"), 0.0);

    if  ( GetLocalInt(oPC, "DO_ONCE__" + GetTag(OBJECT_SELF)) ) return;
          SetLocalInt(oPC, "DO_ONCE__" + GetTag(OBJECT_SELF), TRUE);

    location lLoc = GetLocation(oPC);
    location lLoc2 = GetLocation(GetWaypointByTag("SHAD_LOC"));

    DelayCommand(2.0, AssignCommand(OBJECT_SELF, ActionForceMoveToLocation(lLoc2, TRUE, 30.0)));
    DestroyObject(OBJECT_SELF, 6.0);

    // Declare string variables.
    string sTalk = "Music by Ronnie James Dio.";
    string sRGB = StringToRGBString(sTalk, STRING_COLOR_GREEN);

    DelayCommand(0.5, FloatingTextStringOnCreature(sRGB, oPC));
    DelayCommand(0.5, SoundObjectPlay(GetObjectByTag("DIO_01")));
}

Thanks for any insights you might have...
Post edited by Zephirius on

Comments

  • ZephiriusZephirius Member Posts: 411
    ...just an observation but - the closer I get to the sound object, it seems to fire again.
  • MelkiorMelkior Member Posts: 181
    Find out how long the sound takes to play. Add another DelayCommand at the end of the script, delayed for the length of the sound plus the 0.5s delay before starting. Use SoundObjectStop inside that DelayCommand to turn off the sound object.
    So long as whatever the script is running on (the conversation object) still exists at the end of the delay, the sound object should be turned off positively after playing once and not play again.

    Another possibility is that this script is being called from something you didn't expect to call it from, such as the OnPerception event of an NPC.

    And then again, in multi-player it would be entirely possible for this whole start-stop thing to be messed up by one player aborting the conversation and another starting it before the sound object had finished playing.
  • MelkiorMelkior Member Posts: 181
    edited January 2023
    Here's another suggestion: Instead of using a sound object and activating it, use the PlaySound command with the name of the sound (which you can get from the sound object). That should reliably play the sound just once at that conversation branch. The sound object in this case will be whatever the script is running on.

    In fact, if I remember correctly there's a conversation option known as "other actions" which might be able to do what you want, with a bit of playing around.

    You could also use the AssignCommand command to make a nearby non-static invisible object play the sound, so that the conversation owner doesn't have to take time out to play the sound.
  • ZephiriusZephirius Member Posts: 411
    Thanks Melkior. Will give these suggestions a go.
  • ZephiriusZephirius Member Posts: 411
    edited January 2023
    Melkior wrote: »
    Here's another suggestion: Instead of using a sound object and activating it, use the PlaySound command with the name of the sound (which you can get from the sound object). That should reliably play the sound just once at that conversation branch. The sound object in this case will be whatever the script is running on.

    I've tried using PlaySound before with mixed results. Indeed, it does work.

    The problem lies with the localization of the sound being called. By moving the PC further away from the initial location where the sound was first called, the sound will inevitably become fainter until finally disappearing altogether. That's why I'm using SoundObjectPlay, for the "plays area wide" option that it gives you.
  • MelkiorMelkior Member Posts: 181
    Zephirius wrote: »
    Melkior wrote: »
    Here's another suggestion: Instead of using a sound object and activating it, use the PlaySound command with the name of the sound (which you can get from the sound object). That should reliably play the sound just once at that conversation branch. The sound object in this case will be whatever the script is running on.

    I've tried using PlaySound before with mixed results. Indeed, it does work.

    The problem lies with the localization of the sound being called. By moving the PC further away from the initial location where the sound was first called, the sound will inevitably become fainter until finally disappearing altogether. That's why I'm using SoundObjectPlay, for the "plays area wide" option that it gives you.

    In that case, perhaps you need to use my first suggestion which is use DelayCommand to shut off the sound object after playing once. That way, it can't be retriggered until the conversation is used again.

    For a backstop, I'd probably also set up a do-once variable to stop that conversation branch from being selected again until after the sound has stopped, and I'd have another DelayCommand to remove that variable to re-enable it.

    That is, unless the sound is intended to be truly play-once-only and can never be retriggered.
  • ForSeriousForSerious Member Posts: 446
    I've done this type of thing before.
    You have to call SoundObjectStop on it after SoundObjectPlay. Use DelayCommand to get it to turn off at the right time.
Sign In or Register to comment.