Text Templates in UnrealScript
class TextTemplates extends Object;
struct NameStruct
{
var string Name;
};
function Main()
{
local string Template1, Template2, Template3, Template4;
local array<string> Languages;
local NameStruct PersonStruct;
local Map<string, string> PersonMap;
// UnrealScript doesn't have built-in text template support,
// so we'll simulate it using string operations
// Simple template
Template1 = "Value is {0}";
`log(ReplaceText(Template1, "{0}", "some text"));
`log(ReplaceText(Template1, "{0}", string(5)));
Languages.AddItem("UnrealScript");
Languages.AddItem("C++");
Languages.AddItem("Java");
Languages.AddItem("Python");
`log(ReplaceText(Template1, "{0}", string(Languages)));
// Template with struct
Template2 = "Name: {0}";
PersonStruct.Name = "Jane Doe";
`log(ReplaceText(Template2, "{0}", PersonStruct.Name));
// Template with map
PersonMap.Set("Name", "Mickey Mouse");
`log(ReplaceText(Template2, "{0}", PersonMap.Get("Name")));
// Conditional template
Template3 = "{{if .}} yes {{else}} no {{end}}";
`log(ConditionalTemplate(Template3, "not empty"));
`log(ConditionalTemplate(Template3, ""));
// Range template
Template4 = "Range: {0}";
`log(ReplaceText(Template4, "{0}", JoinArray(Languages, " ")));
}
function string ConditionalTemplate(string Template, string Value)
{
if (Len(Value) > 0)
{
return ReplaceText(Template, "{{if .}} yes {{else}} no {{end}}", "yes");
}
else
{
return ReplaceText(Template, "{{if .}} yes {{else}} no {{end}}", "no");
}
}
function string JoinArray(array<string> Arr, string Separator)
{
local int i;
local string Result;
for (i = 0; i < Arr.Length; i++)
{
if (i > 0)
{
Result $= Separator;
}
Result $= Arr[i];
}
return Result;
}
DefaultProperties
{
}
This UnrealScript code simulates the functionality of text templates as shown in the original example. Here’s an explanation of the key differences and adaptations:
UnrealScript doesn’t have built-in support for text templates, so we use string operations to simulate this functionality.
The
template.New()
andtemplate.Parse()
operations are replaced with simple string assignments.Instead of using
{{.}}
for template placeholders, we use{0}
and theReplaceText()
function to substitute values.Structs and maps are used similarly to the original example, but we access their values directly in UnrealScript.
The conditional template is implemented using a separate function
ConditionalTemplate()
that checks if the value is empty.The range functionality is simulated using a
JoinArray()
function that concatenates array elements with a separator.UnrealScript uses
log
instead offmt.Println
for output. In this example, we use thelog
statement.Error handling is not implemented in this example, as UnrealScript doesn’t have a direct equivalent to Go’s
panic()
function.
This code demonstrates how to achieve similar functionality to Go’s text templates in UnrealScript, despite the language differences. The core concepts of string manipulation, conditional logic, and iterating over collections are preserved.