Tuesday, February 5, 2013

Summoning the Sound Ninja - a PlaySound fix

The PlaySound function puts a sound playing request into the queue of the target creature, which means it doesn't actually play the sound until the other actions in the queue happen. "But I want to play the sound now, not when and even if the queue finishes" you say?

http://nwn2.wikia.com/wiki/PlaySound mentioned using a scripthidden "sound ninja" to stand in and play a sound, but didn't have an implementation, and the suggested implementation looked like it hard coded the sound into the onSpawn script of the sound ninja, so you'd have to make a different ninja with a different script for each sound. So here you go, a generic sound ninja summoning version of PlaySound, this ninja does not require any scripts at all.

    credit to kevL at http://social.bioware.com/forum/1/topic/164/index/15800566#15801095
 ga_play_sound replacement, summons the "sound ninja" creature to play sounds since
 playsound goes at end of action queue, so an npc may not play immediately
 for more see http://nwn2.wikia.com/wiki/PlaySound

 Note that the sound ninja does not need to have any scripts on it at all, but "Disable AI
 while hidden" must not be checked.

 Also note the string sSound is not the tag of a sound object, but the name of the actual
 wav sound file to play

#include "ginc_param_const"

void kL_PlayNinja(string sSound, object oSoundNinja, float fDelay)
    DelayCommand(fDelay, AssignCommand(oSoundNinja, PlaySound(sSound)));
    fDelay += 10.f;
    DelayCommand(fDelay, DestroyObject(oSoundNinja));

void main(string sSound, string sTarget, float fDelay)
    object oTarget = GetTarget(sTarget, TARGET_OWNER);//
    location lTarget = GetLocation(oTarget);
    object oSoundNinja = CreateObject(OBJECT_TYPE_CREATURE, "sh_sound_ninja", lTarget);
    if (GetIsObjectValid(oSoundNinja))
        DelayCommand(0.1f, kL_PlayNinja(sSound, oSoundNinja, fDelay));

No comments:

Post a Comment