From 86ee535177f6a90d99c440bc26b4cd6a13ea0681 Mon Sep 17 00:00:00 2001 From: Tim Nugent Date: Thu, 25 Jan 2024 11:34:42 +1100 Subject: [PATCH] Added a format function to the standard library. This is a wrapper around string.format so whatever format strings apply there apply here. Due to a limitation of yarn functions you can only pass in a single argument parameter, for now. Fixes #348 --- .../BuiltInFunctionsAndCommands.ysls.json | 21 +++++++++++++++++++ YarnSpinner/Dialogue.cs | 5 +++++ 2 files changed, 26 insertions(+) diff --git a/YarnSpinner.LanguageServer/src/Server/Documentation/BuiltInFunctionsAndCommands.ysls.json b/YarnSpinner.LanguageServer/src/Server/Documentation/BuiltInFunctionsAndCommands.ysls.json index 8dcac881e..e41e6c07b 100644 --- a/YarnSpinner.LanguageServer/src/Server/Documentation/BuiltInFunctionsAndCommands.ysls.json +++ b/YarnSpinner.LanguageServer/src/Server/Documentation/BuiltInFunctionsAndCommands.ysls.json @@ -282,6 +282,27 @@ } ], "ReturnType": "number" + }, + { + "YarnName": "format", + "DefinitionName": "format", + "Documentation": "Formats the argument parameter into the format_string. Intended for user facing text. Is a wrapper around String.Format and uses the format string rules from that.", + "Signature": "format(format_string, argument)", + "Parameters": [ + { + "Name": "format_string", + "Type": "string", + "Documentation": "The format string that argument is to be injected into. Must follow C# string format rules.", + "IsParamsArray": false + }, + { + "Name": "argument", + "Type": "any", + "Documentation": "The value to be injected into the format_string", + "IsParamsArray": false + } + ], + "ReturnType": "string" } ] } diff --git a/YarnSpinner/Dialogue.cs b/YarnSpinner/Dialogue.cs index 63a4973cb..fe45b7ac9 100644 --- a/YarnSpinner/Dialogue.cs +++ b/YarnSpinner/Dialogue.cs @@ -1209,6 +1209,11 @@ public StandardLibrary() return Math.Truncate(num); }); + this.RegisterFunction("format", delegate (string formatString, object argument) + { + return string.Format(System.Globalization.CultureInfo.CurrentCulture, formatString, argument); + }); + #endregion Operators } }