I've got this room that has a tractor driving around in the background (running non-blocking and performing excellent in the rep_exec_always script) and I want the sound of the tractor to scale according to its distance to the player. Don't worry, I ran into some problems...
In the rooms repeatedly execute always script:
// scale sound
if (tractordriving == true && IsGUIOn(OPTIONS)==false) {
if (IsChannelPlaying(3)==false) PlaySoundEx(371, 3);
//adjust volume according to scaling
if (cChar3.y < 95) SetChannelVolume(3, 30);
else if (cChar3.y >= 95 && cChar3.y < 110) SetChannelVolume(3, 50);
else if (cChar3.y >= 110 && cChar3.y < 125) SetChannelVolume(3, 70);
else if (cChar3.y >= 125 && cChar3.y < 140) SetChannelVolume(3, 100);
else SetChannelVolume(3, 140); // character has an equal to / larger y-coordinate than 140
}
Now, this sound code works and sounds perfectly when running a blocking sequence, for instance looking at something using the cEgo.Say command, or while in a cutscene. Whenever something non-blocking is happening, like nothing at all (player can move cursor around), the sound stutters and pops like crazy, and it seems to be stuck at the largest volume. This really don't make sense to me. What's going on here? :'(
Edit: Sorry, I left out the little detail that the code was actually in the rep exec always script...
The room's rep_ex isn't run during a blocking event.
Try moving the code to rep_ex_always.
The stuttering might be caused by the sound being played every 40th of a second due to IsChannelPlaying not returning true quick enough after PlaySound is called.
Quote from: KhrisMUC on Fri 04/07/2008 00:47:05
The room's rep_ex isn't run during a blocking event.
Try moving the code to rep_ex_always.
The stuttering might be caused by the sound being played every 40th of a second due to IsChannelPlaying not returning true quick enough after PlaySound is called.
I accidentally left out that the code I posted actually *was* in repeatedly execute always... Sorry about that.
Now, why does the sound play perfectly with no stuttering when cEgo is examining an object (talking), and play horribly when he ain't doing anything? It just doesn't make sense. Some part of me wants to believe it's because it's setting the sound volume 40 times every second. However, if this is a problem, why doesn't it sound screwy when doing a blocking command?
My guess is, the following line somehow got executed continuously:
if (IsChannelPlaying(3)==false) PlaySoundEx(371, 3);
So the effect got restarted every game loop (I don't know whether playing a new effect will reset the channel volume though, hopefully not), but I don't know the reason either. I'm not sure whether there is a bug at the moment.
On an unrelated note, you can actually simplify the if-else-if codes a bit:
if (cChar3.y < 95) SetChannelVolume(3, 30);
else if (cChar3.y < 110) SetChannelVolume(3, 50);
else if (cChar3.y < 125) SetChannelVolume(3, 70);
else if (cChar3.y < 140) SetChannelVolume(3, 100);
else SetChannelVolume(3, 140); // character has an equal to / larger y-coordinate than 140
Since, for example, in the 1st 'else if' line, as it's the 'else' part of (cChar3.y < 95), it's gauranteed that cChar3.y >= 95 already.
Quote from: Gilbot V7000a on Fri 04/07/2008 02:11:13
On an unrelated note, you can actually simplify the if-else-if codes a bit:
Yeah, I had this in the back of my head while writing the code, yet I did it the hard way ;) It's fixed now.
Quote from: Gilbot V7000a on Fri 04/07/2008 02:11:13
My guess is, the following line somehow got executed continuously:
if (IsChannelPlaying(3)==false) PlaySoundEx(371, 3);
So the effect got restarted every game loop (I don't know whether playing a new effect will reset the channel volume though, hopefully not).
Nah, I can hear the 3 second sample loop perfectly, there's just this intense clicking and popping noise coming from it as well as it always plays at the loudest level. At first I thought there was something running in repeatedly execute that interrupted the sound scaling code (seeing as it works perfectly while doing something blocking such as talking), but I can't find anything resembling either the room or sound in general in there. This is the games repeatedly execute code I'm talking about here -- the room doesn't even have a repeatedly execute script.
Looks like I'm gonna have to resort to hardcode spesific sound effects in there instead of having it scale automatically. Which is a pity because if the player has an old computer or some intensive program in the background and the game slows down, maybe there'll be a short pause between the sound clips.
Here's the move-around code for the character the sound scaling belongs to, also being in rep_exec_always:
if (runtractorinbackground == true) {
if (cChar3.Moving == false) {
if (cChar3.x==169 && cChar3.y==133) { //moving up
cChar3.Walk(169, 91, eNoBlock, eWalkableAreas);
oHarvester.Animate(3, 2,eRepeat,eNoBlock,eForwards); // object connected to tractor, following cChar3
oHarvester.StopMoving();
oHarvester.SetPosition(169-35, 133);
oHarvester.Move(169-20,91, 1,eNoBlock,eAnywhere);
}
else if (cChar3.x==169 && cChar3.y==91) { //moving right
cChar3.Walk(280, 91, eNoBlock, eWalkableAreas);
oHarvester.Animate(2, 2,eRepeat,eNoBlock,eForwards);
oHarvester.StopMoving();
oHarvester.SetPosition(169-45, 88);
oHarvester.Move(280,88, 1,eNoBlock,eWalkableAreas);
}
else if (cChar3.x==280 && cChar3.y==91) { //moving down
cChar3.Walk(280, 133, eNoBlock, eWalkableAreas);
oHarvester.Animate(0, 2,eRepeat,eNoBlock,eForwards);
oHarvester.StopMoving();
oHarvester.SetPosition(280-20, 91-10);
oHarvester.Move(280-35,133, 1,eNoBlock,eWalkableAreas);
}
else if (cChar3.x==280 && cChar3.y==133) { //moving left
cChar3.Walk(169, 133, eNoBlock, eWalkableAreas);
oHarvester.Animate(1, 2,eRepeat,eNoBlock,eForwards);
oHarvester.StopMoving();
oHarvester.SetPosition(280+30, 133);
oHarvester.Move(169,133,1,eNoBlock,eWalkableAreas);
}
}
}
Just in case anyone sees something crazy in there. It's just a tractor driving around in a square. I AM running 2.72 so I dont think the fact that it might be a bug will be relevant seeing as it's kind of outdated.