Using String.Format with a String function

Started by Chrille, Tue 17/08/2010 23:06:38

Previous topic - Next topic

Chrille

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:

Code: ags
function Message (String text) {
 dostuff;
}


and here's how it's used with String.Format
Code: ags
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...
GASPOP software
http://www.gaspop.com

GarageGothic

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:

Code: ags
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.

Chrille

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 :)
GASPOP software
http://www.gaspop.com

monkey0506

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

SMF spam blocked by CleanTalk