I’m having trouble getting a sound object to play. I would like it to play using the OnEnter event for the area when the PC transitions. It plays in the toolset but won’t play when I code it.
In NWN2 it’s tricky to play a sound right away for some reason. Maybe it’s the same thing with NWN1 and NWNEE. In any case, I have a script made by @travus which works really well. I think this should work with NWN1 too. You need the tag of an object that’s in the same as where you want the sound to be played. Here’s the script:
void main()
{
object oPC = GetEnteringObject();
if (!GetIsPC(oPC)) return;
object oObjectInArea = GetObjectByTag("TagOfObject");
object oIP = CreateObject(OBJECT_TYPE_PLACEABLE, "plc_ipoint ", GetLocation(oObjectInArea));
// do not remove this space ^
string sWave = "gui_flutesong02"; // <--- sound file name
DelayCommand(0.2f, AssignCommand(oIP, PlaySound(sWave, TRUE)));
SetPlotFlag(oIP, FALSE);
DestroyObject(oIP, 0.3f);
}
EDIT: Actually, maybe you could even use the oPC as the oObjectInArea come to think of it.
First, make sure the sound object is correctly setup to play by having a simple lever object trigger the sound object. i.e. Test the sound plays as you think it should using a different hook event other than the area On Enter. It’s important to establish you have the sound object setup correctly: Single_Shot_Global
If this is NWN2, then using the area’s On Client Enter script hook is more reliable to use.
Alternatively, fire the script like andgalf suggests, using a direct sound file play command: PlaySound, making sure you use you use the TRUE parameter.
Have the sound event object played from entering a trigger (upon arrival) instead. i.e. The trigger event is fired after the PC arrives at the area where they enter a trigger.
Also note that the function GetNearestObjectByTag, may require the parameter to replace OBJECT_SELF to help find the sound object in question. i.e. It may be that it simply needs to be …
This. GetNearestObjectByTag will fail if the second parameter is area. Since OP didn’t specify the parameter, then OBJECT_SELF was used and OBJECT_SELF is area type of object since the script has been running in area’s OnEnter event.
With my scripter hat on, I don’t understand how that works, if, as @Shadooow said, OBJECT_SELF is still the area.
Even using oPC, GetNearestObjectByTag() doesn’t quite do what you might expect if we are in the area OnEnter event. At that time, strangely, the PC location is (0 0 0), not the point at which the PC enters the area moments later.
That is not consistent with my testing. Both spawned NPC and player moving areas have valid location at the time of area’s OnEnter module fires. And thus GetNearest* functions works if you pass such creature into parameter. (The reason why the debug message returns 7f00000 when Guard enters area is because I was looking for nearest placeable and there aren’t any in dungeon where the Guard just spawned)