Background Speech (tried modules)

Started by Nixxon, Mon 02/11/2015 23:10:02

Previous topic - Next topic

Snarky

#20
Oh right, that's because SayBackgroundClip() doesn't wait until the character has stopped saying the first thing, so the second thing immediately overrides it.

As written, the code structure doesn't really allow for sequences of actions within each case. You can have multiple things that all happen together (e.g. saying a line and changing a view), but not saying a line, waiting until it's finished, and then doing something else (i.e. queuing commands).

It's possible to code around this, but I should point out that all of this is essentially what the BgSpeech module is for. The only thing it is missing is playing voice speech, and it would probably be easier to add that to the module than to hack together the queuing of commands. Or get in touch with Phemar and ask him to extend the module for you.

However, as a quick hack to make this more complicated thing work, the code in repeatedly_execute_always() will have to change:

Code: ags

int pedoState; // This is the old "which" variable

function repeatedly_execute_always() {
  cPedoMan_speechTimer++;
  bool stateSwitched = (cPedoMan_speechTimer % 600 == 0);
  if(stateSwitched)
    pedoState++;
  if (cPedoMan.Room != player.Room)
    return; // do nothing
 
  if (pedoState == 1 && stateSwitched)
    cPedoMan.SayBackgroundClip("&1 DIALOG 1", aPEDO7);
  else if (pedoState == 2) {
    if(stateSwitched)
      cPedoMan.SayBackgroundClip("&2 DIALOG 2", aStonethrow);
    else if(cPedoMan_speechTimer == 1400)    // Hardcoding the timing - ugly!
      cPedoMan.SayBackgroundClip("&0 DIALOG X", aWhatever);
  }
  else if (pedoState == 3 && stateSwitched)
    cPedoMan.SayBackgroundClip("&3 DIALOG 3", aMonstereat);
  else if (pedoState == 4 && stateSwitched)
    cPedoMan.SayBackgroundClip("&4 DIALOG 4", aPickuppebble);
  else if (pedoState == 5 && stateSwitched)
    cPedoMan.SayBackgroundClip("&5 DIALOG 5", aOpentrunk);
  else if (stateSwitched) {
      cPedoMan_speechTimer = 0; // Loop around - set to -1 if you don't want a double pause when it loops around
      pedoState = 0;
  }
}


Here the pedoState==2 demonstrates how to change the conditions to queue multiple commands or blocks of code. Obviously in this case it's very important that the whole sequence doesn't take more than 600 game cycles, since at that point it will be interrupted by the next state.

Nixxon

Thanks Snarky,

Fortunately it doesn't matter too much as all i really wanted to do was set off a change view script. So all good :) i will save your for future reference.

I do have one more question though.

Is it possible to have two separate timers in one room?

I want to have another event running on a timer similar to the cPedoMan_speechTimer++;

Can i have another function in the function repeatedly_execute_always() {

Thanks in advance.

Snarky

Well, first of all, what you have here isn't really a timer. It's just a counter. AGS also has a built-in timer type (you set them off and then they count down to 0 on their own, and you can check whether they've finished), though you don't really want one for this task.

But sure, you can have as many of these counters as you want, and multiple (though limited) built-in timers.

As for the second question, you can certainly call a function from repeatedly_execute_always(). You can't declare a function inside a function, because that doesn't really make sense.

One way to think about a function is that it's just a shortcut for a whole chunk of code: instead of having to do write a hundred lines every time you want to do something, you wrap it up as a function, and then you can just call that function to run those hundred lines.

So what you can do in order to keep things neat is this:

Code: ags

void updatePedoMan() {
  // All the related code you had in repeatedly_execute_always()
}

void doSomethingElse() {
  // Some other background event
}

function repeatedly_execute_always() {
  updatePedoMan();
  doSomethingElse();
}

SMF spam blocked by CleanTalk