ServUO Version
Publish 57
Ultima Expansion
Endless Journey
Looking to add a random name into a string, but not exactly sure how to do it.. I was thinking something like this:
C#:
        private static string FormatName()
        {
            switch (Utility.Random(3))
            {
                default:
                case 0:
                    return "Name1";
                    break;
                case 1:
                    return "Name2";
                    break;
                case 2:
                    return "Name3";
                    break;
            }
        }
C#:
case 1: ItemID = 0; Name = "String " + FormatName; break;
But clearly doesn't work haha..
C#:
error CS0019: Operator '+' cannot be applied to operands of type 'string' and 'method group' [C:\ServUO\Scripts\Scripts.csproj]

Not really sure what I'm doing with this one..
 
Well it would work, but you forgot to use the brackets, since it is a method.

You could also add an string array and fill it with the names. Then use a random entry of that
 
True I just can't remember where I've used an array before so can't remember it lol, I was actually working on something else that used the function method for something similar haha.. also doh so simple, added in the brackets and it compiles I'll test it here in a bit when I get home.

**EDIT**
Yep a little bit of code cleanup and that worked great, thank you.
 
Last edited:
Simple:
C#:
private static string FormatName()
{
    return Utility.RandomList("Name 1", "Name 2", "Name 3");
}

Efficient (backed by static array):
C#:
private static readonly string[] m_Names = 
{
    "Name 1",
    "Name 2",
    "Name 3",
};

private static string FormatName()
{
    return Utility.RandomList(m_Names);
}
 
Back