Full Version : Nameable Containers
xmlspawner >>Scripting Support >>Nameable Containers


<< Prev | Next >>

Zyle- 02-09-2006
I'm trying to make a tool which, when you double click it gives you a target. If you target a container, you get a prompt which allows you to type in a name for the container. However I can't get past this error...

CODE

using System;
using Server.Misc;
using Server.Network;
using Server.Prompts;
using Server.Items;
using Server.Mobiles;
using Server.Targeting;

namespace Server.Items
{
public class EngravingTool : Item
{
 [Constructable]
 public EngravingTool() : base( 0x12B3 )
 {
  Weight = 1.0;
  Name = "Engraving Tool";
  Hue = 548;
 }
 
 public EngravingTool( Serial serial ) : base( serial )
 {
 }

 public override void Serialize( GenericWriter writer )
 {
  base.Serialize( writer );

  writer.Write( (int) 0 ); // version
 }

 public override void Deserialize( GenericReader reader )
 {
  base.Deserialize( reader );

  int version = reader.ReadInt();
 }

 
 public override void OnDoubleClick( Mobile from )
 {
  if ( from.InRange( this.GetWorldLocation(), 2 ) )
  {
   from.Target = new EngravingToolTarget( this );
  }
  else
  {
   from.LocalOverheadMessage( MessageType.Regular, 0x3B2, 1019045 ); // I can't reach that.
  }
 }  
}


public class EngravingToolTarget : Target
   {
    private EngravingTool m_Tool;

       public EngravingToolTarget( EngravingTool tool ) : base( 18, false, TargetFlags.None )
       {
        m_Tool = tool;
       }

       protected override void OnTarget( Mobile from, object targeted )
       {
         if ( m_Tool.Deleted )
            return;
 
         if ( targeted is BaseContainer )
            m_Tool.ContainerNameChangePrompt( from, this );

       }
}

public class ContainerNameChangePrompt : Prompt
 {
 private Item m_Item;
 public ContainerNameChangePrompt( Mobile from, Item item )
 {
  m_Item = item;
  from.SendMessage("What would you like to name the container (2-16 characters)?");
 }

public override void OnResponse( Mobile from, string text )
 {
  if (m_Item == null || !m_Item.IsChildOf( from.Backpack ) )
  from.SendLocalizedMessage( 1042001 );

  text = text.Trim();
  if (!NameVerification.Validate( text, 2, 16, true, false, true, 1, NameVerification.SpaceDashPeriodQuote ))
   from.SendMessage( "Names must contain 2-16 alphabetic characters." );
  else
  {
   from.Name = text;
  // m_Item.Delete();
  }
 }
}
}



CODE

- Error: Scripts\Custom\Ally\EngravingTool.cs: CS0117: (line 69, column 14) 'Se
rver.Items.EngravingTool' does not contain a definition for 'ContainerNameChange
Prompt'


Any help understanding wtf this error is trying to get me to do would be appreciated - I'm lost on this one.

(edit)
Hmm copypasting messed my nice formatting up a bit. And for some reason it won't let me colour the problematic lines - it's the following line which is giving me problems (and the if statement before it is probably wrong too):

m_Tool.ContainerNameChangePrompt( from, this );

Thanks

ArteGordon- 02-09-2006
that would be how you would refer to ContainerNameChangePrompt if it were a method in your EngravingTool class, but it isnt. What you need to do is to create a new instance of your ContainerNameChangePrompt class like

from.Prompt = new ContainerNameChangePrompt( from, this );

instead of

m_Tool.ContainerNameChangePrompt( from, this );

oh, and the problem putting color tags inside of code tags is just some weird quirk of the forum. If you use quote tags instead of code tags, then it works.

Zyle- 02-09-2006
Ahh, I get it now, thanks smile.gif That threw up a few more errors which I'm working on now - hopefully I can figure it out myself from here. Thanks a lot smile.gif

Zyle- 02-09-2006
Or not... I got a rid of a few but I'm stumped with this one. :/

- Error: Scripts\Custom\Ally\EngravingTool.cs: CS0115: (line 63, column 30) 'Se
rver.Items.EngravingToolTarget.OnTarget(Server.Mobile, Server.Item)': no suitabl
e method found to override

QUOTE
using System;
using Server.Misc;
using Server.Network;
using Server.Prompts;
using Server.Items;
using Server.Mobiles;
using Server.Targeting;

namespace Server.Items
{
public class EngravingTool : Item
{
  [Constructable]
  public EngravingTool() : base( 0x12B3 )
  {
  Weight = 1.0;
  Name = "Engraving Tool";
  Hue = 548;
  }
 
  public EngravingTool( Serial serial ) : base( serial )
  {
  }

  public override void Serialize( GenericWriter writer )
  {
  base.Serialize( writer );

  writer.Write( (int) 0 ); // version
  }

  public override void Deserialize( GenericReader reader )
  {
  base.Deserialize( reader );

  int version = reader.ReadInt();
  }

 
  public override void OnDoubleClick( Mobile from )
  {
  if ( from.InRange( this.GetWorldLocation(), 2 ) )
  {
    from.Target = new EngravingToolTarget( this );
  }
  else
  {
    from.LocalOverheadMessage( MessageType.Regular, 0x3B2, 1019045 ); // I can't reach that.
  }
  } 
}


public class EngravingToolTarget : Target
    {
    private EngravingTool m_Tool;

        public EngravingToolTarget( EngravingTool tool ) : base( 18, false, TargetFlags.None )
        {
        m_Tool = tool;
        }

        protected override void OnTarget( Mobile from, Item targeted )
        {
          if ( m_Tool.Deleted )
            return;
 
          if ( targeted is BaseContainer )
            from.Prompt = new ContainerNameChangePrompt( from, targeted ); 
        }
    }

public class ContainerNameChangePrompt : Prompt
{
  private Item m_Item;
  public ContainerNameChangePrompt( Mobile from, Item item )
  {
  m_Item = item;
  from.SendMessage("What would you like to name the container (2-16 characters)?");
  }

public override void OnResponse( Mobile from, string text )
  {
  if (m_Item == null || !m_Item.IsChildOf( from.Backpack ) )
  from.SendLocalizedMessage( 1042001 );

  text = text.Trim();
  if (!NameVerification.Validate( text, 2, 16, true, false, true, 1, NameVerification.SpaceDashPeriodQuote ))
    from.SendMessage( "Names must contain 2-16 alphabetic characters." );
  else
  {
    from.Name = text;
  // m_Item.Delete();
  }
  }
}
}



ArteGordon- 02-09-2006
the calling args to the OnTarget method should be

protected override void OnTarget( Mobile from, object targeted )

note, this also means that you will have to cast the argument to your Prompt constructor appropriately since it is now an object instead of an Item and the constructor expects an Item

from.Prompt = new ContainerNameChangePrompt( from, (Item)targeted );

Zyle- 02-09-2006
Thanks so much Arte, it works! Had to change a bit in the prompt, I modified the prompt class from a name change deed but apparantly not modified enough as it decided to rename me first. wink.gif Got it now though, thanks a ton tongue.gif


Now to try and modify it so it's a tool with uses...

Zyle- 02-09-2006
Argh... I realised I have no check to see if the container is in the player's backpack which would be quite an important one...

I tried changing adding it in the check to see if it's a container:
CODE

if ( targeted is BaseContainer && targeted.IsChildOf( from.Backpack ) )


however...
- Error: Scripts\Custom\Ally\EngravingTool.cs: CS0117: (line 68, column 45) 'ob
ject' does not contain a definition for 'IsChildOf'

I know this is the same error as in my first post and I've tried everything I can think of but still stuck. Argh. Hope this will be my last problem with this.

ArteGordon- 02-09-2006
IsChildOf is a method available to things in the Item class. Since you check to make sure that the targeted object is a BaseContainer, you can then safely cast it to a BaseContainer like

if ( targeted is BaseContainer && ((BaseContainer )targeted).IsChildOf( from.Backpack ) )

or to an Item, since BaseContainers are also Items

if ( targeted is BaseContainer && ((Item)targeted).IsChildOf( from.Backpack ) )

Zyle- 02-09-2006
Ahhhh that makes sense, thanks smile.gif

*skulks off to read more tutorials*