Iomega0318

Member
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);
}
 

Active Shards

  • Unchained
    Custom (Classic)
    • Players
    • 144 Online
    • 257 Peak
  • UO Eventine
    Time Of Legends
    • Players
    • 85 Online
    • 137 Peak
  • The Crossroads
    Mondain's Legacy
    • Players
    • 64 Online
    • 138 Peak
  • Insane UO
    Endless Journey
    • Players
    • 59 Online
    • 101 Peak
  • UO: Felucca
    Renaissance
    • Players
    • 55 Online
    • 87 Peak
  • Arth
    Custom (Modern)
    • Players
    • 24 Online
    • 34 Peak
  • CALYPSO
    Custom (Modern)
    • Players
    • 23 Online
    • 30 Peak
  • UO Phoenix
    Custom (Classic)
    • Players
    • 21 Online
    • 35 Peak
  • UOItalia Reborn
    Custom
    • Players
    • 19 Online
    • 37 Peak
  • UO Enigma
    Custom (Modern)
    • Players
    • 19 Online
    • 172 Peak

Donations

Total amount
$0.00
Goal
$500.00
Back