During character creation, I give players an item they need to take possession of by 2xclicking it. How can I make this happen automatically?
Code:
PackItem(new Homestone());
 
in CharacterCreation, insert code in the method: private static void EventSink_CharacterCreated(CharacterCreatedEventArgs args)

after AddBackpack(newChar);

search the character's backpack for Homestone and if found then call it's doubleclick method
 
HomeStone.cs source here here.

To take possession, this is the method

Code:
        private void SwitchOwner(Mobile from)
        {
            if ( owner == null ) // double check..
            {                  
                owner = from;
                from.SendMessage("You take possession of this homestone!");
                RenameThisStone();
            }
            else
                from.SendMessage( "This is not your homestone!" );
        }

The other option would be to set an owner = from when adding it to the backpack, bit I'm not sure how to do either
 
I think to set the owner directly you'd just need to add something like this instead of
PackItem(new Homestone());

Code:
var hs = new Homestone();
hs.owner = m;
PackItem(hs);
 
In the HomeStone code, you could have a method override for OnAdded:

Code:
public override void OnAdded(IEntity parent)
{
	base.OnAdded(parent);

	if( this.Owner == null || this.Owner.Deleted ) // Owner is null or deleted?
	{
		this.Owner = this.RootParent as Mobile; // Set owner to the Mobile parent, or null if not a Mobile
	}
}

This technique ensures that the Owner is never null if the item has been placed in their pack, bank, or any child of the two.
It will not replace the Owner if the owner is already set.

Personally, I prefer to use the BlessedFor property that's exposed by all Items, when it comes to inferring ownership of an item.

As for the original request;

Code:
var item = mobile.Backpack.FindItemByType<Homestone>();

if( item != null )
{
	item.OnDoubleClick( mobile );
}
 
Back