Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: daniel on Thu 04/05/2017 11:43:48

Title: could use some pro-tips on randomization...
Post by: daniel on Thu 04/05/2017 11:43:48
hey guys,

i would like to play a background sound in my room and randomize certain aspects:

1. i would like to define a certain time frame to randomize the delay between each played sound (somewhere between 40-400 cycles)
2. i would like to randomize which sound is getting played (something like 3 different sounds should be fine)
3. i would like to randomize the panning property of the played sound so each time it plays from a different "spot"

first i thought i could just use random() to define the length of a timer but then realized that random() always starts at 0.
so i guess defining different timers is the only way?
anyway here's what i came up with so far:
function WoodCreak () {
  AudioChannel *WoodCreak1Channel = aWoodCreak1.Play (eAudioPriorityNormal, eOnce);
}

function CreakPlay () {
  int CreakRandom=Random(2);
    if (IsTimerExpired(2)) {
      if (CreakRandom==0) {
      WoodCreak();
      SetTimer (2, 100);
      }
      else if (CreakRandom==1) {
      WoodCreak();
      SetTimer (2, 200);
      }
      else if (CreakRandom==2) {
      WoodCreak();
      SetTimer (2, 400);
      }
    }
}


with CreakPlay (); in room_RepExec() this works fine to somewhat randomize the delay between each play. however if i now start to introduce 3 different sounds i would end up with a whole bunch of if statements...which would be fine but i'm sure there's some smarter way to go about this? also i would still be missing the random panning for the sound which i have no idea how to achieve.

any advice would be appreciated:)

cheers
Title: Re: could use some pro-tips on randomization...
Post by: Snarky on Thu 04/05/2017 11:53:30
Quote from: daniel on Thu 04/05/2017 11:43:48
first i thought i could just use random() to define the length of a timer but then realized that random() always starts at 0.

Yes, but fortunately there is an obscure mathematical operation that allows us to deal with that. Maybe you've heard of it...

Code (ags) Select
SetTimer(2, Random(360)+40); // Random number from 40 to 400

:P :-D

Anyway, what you want is something like this:

Code (ags) Select
#define CREAK_TIMER 2
#define CREAK_MIN 40
#define CREAK_MAX 400

void creak()
{
  int creakSound = Random(4); // <-- However many different creaks you have minus 1
  int creakPan = Random(200) - 100; // Random panning from -100 to 100

  AudioChannel* creakChannel;
  if(creakSound == 0)
    creakChannel = aWoodCreak1.Play();
  // I'm just making up the rest of these clips, use the real names obviously
  else if(creakSound == 1)
    creakChannel = aWoodCreak2.Play();
  else if(creakSound == 2)
    creakChannel = aDoorCreak1.Play();
  else if(creakSound == 3)
    creakChannel = aDoorCreak2.Play()
  else if(creakSound == 4)
    creakChannel = aWindowCreak1.Play();

  if(creakChannel != null)
    creakChannel.Panning = creakPan;

  SetTimer(CREAK_TIMER, Random(CREAK_MAX-CREAK_MIN) + CREAK_MIN);
}

// Or if you just want it in a certain room, call it from the room RepeatedlyExecute function
function repeatedly_execute_always()
{
  if(IsTimerExpired(CREAK_TIMER))
    creak();
}


You also need to call creak() or set the timer manually to start the whole thing, probably from a room_load or firstLoad function.
Title: Re: could use some pro-tips on randomization...
Post by: daniel on Thu 04/05/2017 12:17:53
thanks! that's exactly what i was looking for:)

i have another noob question if i may:
what does "void" mean/do? i searched the AGS dynamic help but couldn't find any info on it...
Title: Re: could use some pro-tips on randomization...
Post by: Khris on Thu 04/05/2017 12:32:09
"void" is a return type and means "nothing". It's used when functions are defined.

Returning a float:
float product(float a, float b) {
  return a * b; // we're returning a float
}

  float c = product(3.2, 5.63); // assign returned value to c


nothing gets returned, the function only *does* something:void scream() {
  player.Say("Aaaaaaaaaaah!");
}

  scream();  // simple call, no return value, no assignment


The void keyword is a basic feature of C like languages. "function" is actually "int", and as opposed to, say, Java, AGSScript doesn't complain when you don't actually return anything. However when you *do* return anything, the return type has to match the data type of whatever you're returning.

So as a noob, it's basically always safe to use "function", because you usually won't return Dynamic arrays from your functions or the like, merely use them to avoid duplicate code.
Title: Re: could use some pro-tips on randomization...
Post by: daniel on Thu 04/05/2017 12:40:01
ok i see, thanks guys!