Dan

Moderator
I would like to make the whispering roses.

You will get a random rose with a random name based on all the names already in use.

Could I use something like this? To get it accomplished? Or is there a better way -

Mobile otherPlayer in World.Mobiles.Values
 
yes, but don't forget null checks

foreach (Mobile otherPlayer in World.Mobiles.Values)
{
if (otherPlayer is PlayerMobile && otherPlayer != m && otherPlayer.Name != null )
{
 
Not sure if this is what your looking for our not:
I use the named pumpkin script to name the puppies and kittens for Valentines Day.
You can add whatever names you want in a PlayerNames.log file that is just inside the game folder not inside the scripts folder.
Ill attach my kittens and player names thing. This is really a nice script and I wish I remembered the author :( I had to zip the PlayerNames.log, but you can see how you just add names as you want in there.

Shazzy
 

Attachments

  • SleepingCat.cs
    2.6 KB · Views: 7
  • PlayersNames.zip
    160 bytes · Views: 4
It is very cool, I want it to pull from all names of all characters ever created though.

I took the idea from the "Unique Name System" that checks if your name already exists. Trying to apply it here. I will post my errors or completed script when finished!
 
Do we have LinQ with ServUO?
Code:
var names = World.Mobiles.Values.OfType<PlayerMobile>().Select(pm=>pm.Name).Where(name=> name != null).ToList();
 
Okay so this is what I got so far. I will keep tinkering with it but any help is always appreciated!

SHOW.png


Code:
using System;
using System.Linq;
using Server.Mobiles;

namespace Server.Items
{
    public class WhisperingRose1 : Item
    {
        [Constructable]
        public WhisperingRose1()
            : base(0x18EA)
        {                   
            this.Name = "A Whispering Rose From " + World.Mobiles.Values.OfType<PlayerMobile>().Select(pm=>pm.Name).Where(name=> name != null).ToList();
            this.LootType = LootType.Blessed;
        }
 

Attachments

  • WhisperingRose1.cs
    871 bytes · Views: 7
Code:
var names = World.Mobiles.Values.OfType<PlayerMobile>().Select(pm=>pm.Name).Where(name=> name != null).ToList();

That linq statement will get you a generic list of Names that are of type PlayerMobile in the World.Mobiles dictionary, from the dictionary's values.
So, all players names are in the list, to get one (say at random) should be something like (and forgive me been a bit since I messed with this)
Code:
names[Random(0,names.Count)];

Using 0 for the first index of names and assuming Random returns an exclusive max, the value of Count will never be used.

So what we can do here is use the first part to get the list, then check the list to make sure it has values in it, once we're sure there are values in the list we can then return an object form the list or instead we can return a Default value in the case that no values where found which shouldn't exist.

Code:
var names = World.Mobiles.Values.OfType<PlayerMobile>().Select(pm=>pm.Name).Where(name=> name != null).ToList();
Name = "A Whispering Rose From " + (names.Count >= 1 ? names[Random(0,names.Count) : "No One");

If my thoughts on ServUO's Random methods are wrong I'm sure Zero will beat me over the head with it ;)
 
Last edited:
Code:
    CS0118: Line 14: 'System.Random' is a 'type' but is used like a 'variable'
Scripts: One or more scripts failed to compile or no script files were found.
- Press return to exit, or R to try again.
[doublepost=1486414319][/doublepost]
Code:
var names = World.Mobiles.Values.OfType<PlayerMobile>().Select(pm=>pm.Name).Where(name=> name != null).ToList();
            this.Name = "A Whispering Rose From " + names[Random(0,names.Count)];
 
What assembly reference should I add for that?
[doublepost=1486414583][/doublepost]
Code:
Errors:
+ CUSTOM/TEST/WhisperingRose1.cs:
    CS0234: Line 14: The type or namespace name 'Random' does not exist in the namespace 'Server' (are you missing an assembly reference?)
Scripts: One or more scripts failed to compile or no script files were found.
- Press return to exit, or R to try again.
 
Code:
var names = World.Mobiles.Values.OfType<PlayerMobile>().Select(pm=>pm.Name).Where(name=> name != null).ToList();           
this.Name = "A Whispering Rose From " + names[Server.Utility.Random(0,names.Count)];
 
Dude you rock.

Code:
[Constructable]
        public WhisperingRose1()
            : base(0x18EA)
        {  
            var names = World.Mobiles.Values.OfType<PlayerMobile>().Select(pm=>pm.Name).Where(name=> name != null).ToList();
            this.Name = "A Whispering Rose From " + names[Utility.Random(0, names.Count)];
            this.LootType = LootType.Blessed;
        }


This worked great :)
[doublepost=1486414889][/doublepost]Oh woops my way was a little different. I left out the Server.Utility

Should I add it?
 
I think you should apply the count check, because if for any reason the List has 0 values in it you will get an index out of range exception.

Which was the whole (names.Count >= 1 ? : ) part. it's a short hand/in line if statement pretty much.
 
but even to create this item you would have to have atleast the owners name on the list no? I will add it regardless since the extra check can't hurt. Adding it to a pretty active shard.
 

Active Shards

Donations

Total amount
$0.00
Goal
$1,000.00
Back