background speech

Started by Mouth for war, Tue 14/11/2006 21:26:02

Previous topic - Next topic

Mouth for war

Hi..I want a drunk to talk in the background without pausing the game. I don't know what the problem is since this worked in the previous version of AGS. here's the code for it...it seems like the "speech" commandÃ,  doesn't work anymore...what should i do instead?

if (speech != null) {Ã, 
if (!speech.Valid) {Ã,  Ã, 
string text;
int rand = Random(10);
if (rand == 0) StrCopy(text, "Oww...");
else if (rand == 1) StrCopy(text, "My head hurts...");
else if (rand == 2) StrCopy(text, "I shouldn't have drank that last beer...");
else if (rand == 3) StrCopy(text, "I feel like I'm going to puke...");
else if (rand == 4) StrCopy(text, "Where am I?...");
else if (rand == 5) StrCopy(text, "Hic...");
else if (rand == 6) StrCopy(text, "Who are you?...");
else if (rand == 7) StrCopy(text, "I'm tired...");
else if (rand == 8) StrCopy(text, "I should eat something...");
else if (rand == 9) StrCopy(text, "Are you my mommy?...");
else if (rand == 10) StrCopy(text, "Where's my house?...");
speech = cDrunk.SayBackground(text);
Ã,  Ã,  }
}
else {
Ã,  string text;
Ã,  int rand = Random(10);
Ã,  if (rand == 0) StrCopy(text, "Oww...");
Ã,  else if (rand == 1) StrCopy(text, "My head hurts...");
Ã,  else if (rand == 2) StrCopy(text, "I shouldn't have drank that last beer...");
Ã,  else if (rand == 3) StrCopy(text, "I feel like I'm going to puke...");
Ã,  else if (rand == 4) StrCopy(text, "Where am I?...");Ã, 
Ã,  else if (rand == 5) StrCopy(text, "Hic...");
Ã,  else if (rand == 6) StrCopy(text, "Who are you?...");
else if (rand == 7) StrCopy(text, "I'm tired...");
else if (rand == 8) StrCopy(text, "I should eat something...");
else if (rand == 9) StrCopy(text, "Are you my mommy?...");
else if (rand == 10) StrCopy(text, "Where's my house?...");
speech = cDrunk.SayBackground(text);Ã,  }

I also tried this but then he toggles between the different lines so fast you can't read it

int blah = Random(5);
if (blah==0) cDrunk.SayBackground("I have absolutely nothing to say to myself");
else if (blah==1) cDrunk.SayBackground("I hate this place");
else if (blah==2) cDrunk.SayBackground("Why do I always get myself into trouble?");
else if (blah==3) cDrunk.SayBackground("Did you know that swedish girls are very easy?");
else if (blah==4) cDrunk.SayBackground("Boooooring");
else if (blah==5) cDrunk.SayBackground("I don`t want to waste time talking to myself");   A little help please ...I'd really appreciate it


mass genocide is the most exhausting activity one can engage in, next to soccer

Ashen

#1
You don't say WHERE in the code you've put either of these, but I'd guess repeatedly_execute?
The second way ... If it IS in rep_ex blah will be changed every loop, which is probably why you don't get a chance to read a line before the new one replaces it.
The first way should work - what's wrong with it (error mesages, just not saying anything, etc)? While it shouldn't be causing a problem, you might want to update the string and StrCopy commands to new-style Strings.

You could also try using a timer, to have him speak at intervals.
I know what you're thinking ... Don't think that.

Mouth for war

yes in repeatedly execute...The first example used to work in the previous version but now i get an error message that says Error (line 4) undefined symbol 'speech'
mass genocide is the most exhausting activity one can engage in, next to soccer

Ashen

#3
So ... Have you declared speech? It should be an Overlay pointer, e.g.:
Code: ags

// room script file 
Overlay *speech;

#sectionstart room_a  // DO NOT EDIT OR REMOVE THIS LINE
function room_a() {
  // script for Room: Repeatedly execute
  if (speech != null) {  
// And so on


And I assume line 4 is if (speech != null) {? If not, what is it?
I know what you're thinking ... Don't think that.

Mouth for war

#4
Ok that took care of the Speech issue but now i got another error message

"Error (line 10) must have an instance of the struct to access a non-static member"

What to do?

EDIT Line 10:
if (rand == 0) String.Copy()(text, "Oww my head hurts...");
mass genocide is the most exhausting activity one can engage in, next to soccer

Ashen

#5
Start by editing your post to tell us what line 10 says, and we'll go from there ...

Remember: The more information you give in your posts, the more likely it is someone'll be able to help.

EDIT:
OK, read the manual, that's not how you use String.Copy(). The proper usage would be:
Code: ags

String newstring = mystring.Copy();

It's really only used to copy the contents on one String into another (in this case, to copy mystring into newstring).

However, the manual also says:
Quote
You should not normally need to use this, since strings can be assigned using the = operator.

So what you want to use is:
Code: ags

if (rand == 0)  text = "Oww my head hurts...";

And same for the other if (rand == x) lines.

Also, why are you using a String there anyway? Why not just:
Code: ags

if (rand == 0) speech = cDrunk.SayBackground( "Oww...");
else if (rand == 1) speech = cDrunk.SayBackground("My head hurts...");
else if (rand == 2) speech = cDrunk.SayBackground("I shouldn't have drank that last beer...");
else if (rand == 3) speech = cDrunk.SayBackground("I feel like I'm going to puke...");
// etc


The only reason I can see for using the String method, is if you need to have variables in the speech, e.g.;
Code: ags

String text = String.Format("%d bottles of beer on the wall, %d bottles of beer ...", bottlecount, bottlecount);
cDrunk.SayBackground(text);
bottlecount --;
I know what you're thinking ... Don't think that.

Mouth for war

almost but not quite there yet hehe here's the entire code
It works BUT he says everything so fast...you can't read it because it toggles between the different sentences way too fast

  // script for Room: Repeatedly execute
Overlay *speech;
if (speech != null) { 
if (!speech.Valid) {   
String text;
int rand = Random(10);
if (rand == 0)  text = "Oww my head hurts...";
else if (rand == 1)  text = "I'm a looser...";
else if (rand == 2)  text = "I shouldn't have drank that last beer...";
else if (rand == 3)  text = "I feel like I'm going to puke...";
else if (rand == 4)  text = "Where am I?...";
else if (rand == 5)  text = "Hic...";
else if (rand == 6)  text = "Who are you?...";
else if (rand == 7)  text = "I'm tired...";
else if (rand == 8)  text = "I should eat something...";
else if (rand == 9)  text = "Are you my mommy?...";
else if (rand == 10) text = "Where's my house?...";
speech = cDrunk.SayBackground(text);
    }
}
else {
  String text;
  int rand = Random(10);
  if (rand == 0)  text = "Oww my head hurts...";
  else if (rand == 1)  text = "My head hurts...";
  else if (rand == 2)  text = "I shouldn't have drank that last beer...";
  else if (rand == 3)  text = "I feel like I'm going to puke...";
  else if (rand == 4)  text = "Where am I?..."; 
  else if (rand == 5)  text = "Hic...";
  else if (rand == 6)  text = "Who are you?...";
else if (rand == 7)  text = "I'm tired...";
else if (rand == 8)  text = "I should eat something...";
else if (rand == 9)  text = "Are you my mommy?...";
else if (rand == 10) text = "Where's my house?...";
speech = cDrunk.SayBackground(text);  }
}
mass genocide is the most exhausting activity one can engage in, next to soccer

monkey0506

Also, if you wanted to use the same messages outside of that function for any reason, you could first store them in a String array like this:

Code: ags
String messages[11];

// game_start
messages[0] = "Ow...";
messages[1] = "My head hurts...";
// etc.


Then you could just use messages[Random(10)] instead of all the "if (rand == 0) ... else if (rand == 1) ..." business.

And Ashen edited his post...He said that he tried that bit and it didn't work Ashen...but Character.SayBackground doesn't prevent another Character.SayBackground command from being run, so presumably he's running this code repeatedly...he may want to look at one of the queued background speech modules.

SSH's queued background speech module
monkey_05_06's queued background speech module (no subtitles, and I recommend v2.0 as I don't remember how stable/functional the 2.2 BETA was...)

And now Mouth for war edits his post...and...yeah...you definitely want to look into the queued background speech modules.

Ashen

#8
The modules might well be the way to go, the 'extra' features they allow (queued background speech, talking animation with BG speech, etc) are nice too.

However looking at your posted code, you've declared the speech Overlay INSIDE the rep_ex function. I think this means a new - and more importantly null - version will be created every loop, so the last else condition will run every loop, changing the text.

But yeah, go with one of the modules.
I know what you're thinking ... Don't think that.

monkey0506

Seeing as I've been playing buggers with my hosting lately, you could grab my module on jasonjkay's site here. Like I said...I'm not entirely sure how stable the v2.2 BETA release was...and I haven't worked on it in forever (I'd like to get back to this one)...but I think you could do something like this:

Code: ags
// rep_ex
if (!QueuedSpeech.IsQueueFull()) QueuedSpeech.Say(cDrunk, messages[Random(10)]);


Assuming you stored the messages in a String array.

But like Ashen pointed out:

Since you created the pointer within the repeatedly_execute function, it will be created and destroyed every loop at the beginning and end (respectively) of the function. You should have declared it outside the function (by editing the room script directly instead of using the Interaction Editor).

SMF spam blocked by CleanTalk