Jibble

Author Topic: Using String.Format with a String function  (Read 585 times)

Chrille

  • Posts: 726
    • Chrille worked on a game that was nominated for an AGS Award!
      Chrille worked on a game that won an AGS Award!
Using String.Format with a String function
« on: 17 Aug 2010, 23:06 »
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: [Select]
function Message (String text) {
 dostuff;
}

and here's how it's used with String.Format
Code: [Select]
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

  • Posts: 3,436
    • I can help with play testing
    • I can help with scripting
    • I can help with story design
Re: Using String.Format with a String function
« Reply #1 on: 17 Aug 2010, 23:19 »
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: [Select]
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

  • Posts: 726
    • Chrille worked on a game that was nominated for an AGS Award!
      Chrille worked on a game that won an AGS Award!
Re: Using String.Format with a String function
« Reply #2 on: 18 Aug 2010, 01:10 »
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

  • Posts: 7,031
  • SEND PIZZA.
    • Best Innovation Award Winner 2017, for his work to help AGS games reach the widest possible audience - through popular distribution platforms (Steam, Galaxy) as well as other operating systems (Android, Linux)
Re: Using String.Format with a String function
« Reply #3 on: 18 Aug 2010, 05:32 »
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