Inserting a String into a custom speech GUI label

Started by shaun9991, Thu 21/05/2015 10:09:46

Previous topic - Next topic

shaun9991

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

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
Support Cloak and Dagger Games on Patreon: https://www.patreon.com/user?u=460039

Khris

You need something like:
Code: ags
  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.

shaun9991

Thanks Khris, thats a massive help :)

I'll call it manually each time it comes up
Support Cloak and Dagger Games on Patreon: https://www.patreon.com/user?u=460039

Snarky

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

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


You could also try:

Code: ags

  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.

Khris

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.

SMF spam blocked by CleanTalk