Skip to content

Commit

Permalink
Added a format function to the standard library.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
McJones committed Jan 25, 2024
1 parent 4888959 commit 86ee535
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
5 changes: 5 additions & 0 deletions YarnSpinner/Dialogue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down

0 comments on commit 86ee535

Please sign in to comment.