Random sound in a script

I wanted a kind of randomization of a sound in a script. I thought I knew how to do it, but apperently it doesn’t work like I thought. Here’s my custom function in my script (based on an original script by @travus):

void GaspSound()
{

	object oDoor = GetObjectByTag("arenastone");


	object oIP = CreateObject(OBJECT_TYPE_PLACEABLE, "plc_ipoint ", GetLocation(oDoor));
								//	do not remove this space	^ 	

	string sWave = "gasp1";	// <--- sound file name
	string sWave2 = "gasp2";
	string sWave3 = "gasp3";
	string sWave4 = "gasp4";
	string sSound;
	
	int rndm = Random(40);
	
	if (rndm <= 10) sWave = sSound;
	else if (rndm <= 20) sWave2 = sSound;
	else if (rndm <= 30) sWave3 = sSound;
	else if (rndm <= 40) sWave4 = sSound;
	

	DelayCommand(0.2f, AssignCommand(oIP, PlaySound(sSound, TRUE)));	
	SetPlotFlag(oIP, FALSE);
	DestroyObject(oIP, 1.5f);



}

these were backwards: sWave = sSound
should be sSound = sWave (etc)

void GaspSound()
{
	object loc = GetLocation(GetObjectByTag("arenastone"));
	object oIP = CreateObject(OBJECT_TYPE_PLACEABLE, "plc_ipoint ", loc); // do not remove space at end of the resref

	string sSound;
	switch (Random(4))
	{
		case 0: sSound = "gasp1"; break;
		case 1: sSound = "gasp2"; break;
		case 2: sSound = "gasp3"; break;
		case 3: sSound = "gasp4"; break;
	}

	DelayCommand(0.2f, AssignCommand(oIP, PlaySound(sSound, TRUE)));

	SetPlotFlag(oIP, FALSE);
	DestroyObject(oIP, 1.5f);
}

 
The script could be simplified even more if the object “arenastone” can play the sound …

1 Like

@kevL_s - Actually, your script didn’t compile. Maybe you meant:

void GaspSound()
{
	location loc = GetLocation(GetObjectByTag("arenastone"));
	object oIP = CreateObject(OBJECT_TYPE_PLACEABLE, "plc_ipoint ", loc); // do not remove space at end of the resref

	string sSound;
	switch (Random(4))
	{
		case 0: sSound = "gasp1"; break;
		case 1: sSound = "gasp2"; break;
		case 2: sSound = "gasp3"; break;
		case 3: sSound = "gasp4"; break;
	}

	DelayCommand(0.2f, AssignCommand(oIP, PlaySound(sSound, TRUE)));

	SetPlotFlag(oIP, FALSE);
	DestroyObject(oIP, 1.5f);
}
1 Like

yep, typedef mismatch :)

1 Like