Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: shaun9991 on Thu 21/05/2015 10:09:46

Title: Inserting a String into a custom speech GUI label
Post by: shaun9991 on Thu 21/05/2015 10:09:46
Hi guys,

I've got this code to replace the .Say function with .Saying, so I can have a custom gui with portraits and keep Lucasarts style animated sprite speech.
Code (ags) Select

void Saying(this Character*, String message) {
  lSaying.Text = message;
  gSaying.Visible = true;
  this.Say(message);
  gSaying.Visible = false;
}


The main character in the game can be named, and this is stored in a string called PlayerName.

With my adapted .Saying script above, the game crashes every time cEgo.Saying("....%s", PlayerName). Is there a way of incorporating the PlayerName string definition into the .Saying definition above?

Any help would be super appreciated.

Thanks,
Shaun
Title: Re: Inserting a String into a custom speech GUI label
Post by: Khris on Thu 21/05/2015 10:23:18
You need something like:
  lSaying.Text = String.Format("%s: %s", this.Name, message);

Afaik, only Character.Say() and Display() support inserting variables directly, for everything else you have to do it manually. There's no way to have a custom function that supports an arbitrary number of arbitrary typed arguments.
Title: Re: Inserting a String into a custom speech GUI label
Post by: shaun9991 on Thu 21/05/2015 11:04:50
Thanks Khris, thats a massive help :)

I'll call it manually each time it comes up
Title: Re: Inserting a String into a custom speech GUI label
Post by: Snarky on Thu 21/05/2015 13:50:26
Yes, if you want to call Character.Saying() with the player name (or any other variable) inserted into the string, you should do something like:

Code (ags) Select

  String msg = String.Format("My name is %s. Pleasure to meet you!", PlayerName);
  cEgo.Saying(msg);


You could also try:

Code (ags) Select

  cEgo.Saying(String.Format("My name is %s. Pleasure to meet you!", PlayerName));


...but AGS can be a bit finicky about nested functions, so it may not work â€" I'm not sure off the top of my head.
Title: Re: Inserting a String into a custom speech GUI label
Post by: Khris on Thu 21/05/2015 14:16:18
I knew my answer was off but I couldn't figure out how :D

Snarky's second example is exactly what you're looking for. Compose the String first, then pass it to cEgo.Saying(). It will work perfectly fine without an intermediate variable.