Adventure Game Studio | Forums

AGS Support => Beginners' Technical Questions => Topic started by: Chrille on Tue 17/08/2010 23:06:38

Title: Using String.Format with a String function
Post by: Chrille on Tue 17/08/2010 23:06:38
I'd like to know if you can use String.Format in functions in the same way that Display() works.

Here's the very basic function:

function Message (String text) {
dostuff;
}


and here's how it's used with String.Format
Message (String.Format ("bla bla bla %d",stat));

With Display, you don't have to enter String.Format, and that's how I'd like my function to work.
It's a small problem that I hardly ever run into. Still, if it's possible...
Title: Re: Using String.Format with a String function
Post by: GarageGothic on Tue 17/08/2010 23:19:30
I don't think there's a way of doing it unless you know for sure what the type of the variable is (or if there's multiple in which order they appear).

If it's always an int, you could make the variable optional, like so:

import function Message(String text, int stat = -1); //set the default value to something you wouldn't use when calling the function

function Message(String text, int stat) {
  if (stat != -1) text = String.Format(text, stat);
  dostuff(text);
  }


In most cases simply using String.Format within the function call would be simpler imho.
Title: Re: Using String.Format with a String function
Post by: Chrille on Wed 18/08/2010 01:10:29
I thought about using the int, but it's easier as you say to just use String.Format when calling the function. It's very rarely I need String.Format really, would just have been a luxury :)
Title: Re: Using String.Format with a String function
Post by: monkey0506 on Wed 18/08/2010 05:32:10
If you're looking through the manual entries, any of the functions which list ... as the final parameter (following a const string parameter of course) can use string formatting. Unfortunately we can't utilize ... as a parameter. Last I checked you can use it in your functions, but there's no way to actually access the additional data passed to the function, rendering it useless.

So, we just use String.Format where necessary. :P