PigPen

Member
*I have a RunUO Version 2.1 setup on my shard*

Well . . I have driven my brain to mush on this one with no luck so . . here I am looking for a lesson.

I made a Trap item and now I am trying to make a TrapperKit so players can DBL Click on the Kit then select a LandTile on which to place the trap. The idea is for them to try luring a monster to walk over the trap.

I have had several Null Ref crashes in the "protected override void OnTarget" section. That problem may still be there but now my recent changes will not even Compile.

I'm having trouble recognizing a LandTile as a target location and then being able to place the Trap on that LandTile location.

Here are my most recent Errors:
Code:
Errors:
 + Customs/Crafting - Animal Traps/TrapperKit.cs:
    CS1061: Line 109: 'object' does not contain a definition for 'Location' and no extension method 'Location' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
    CS1061: Line 109: 'object' does not contain a definition for 'Map' and no extension method 'Map' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

and . . Here is my most recent script attempt:
Code:
using System; 
using Server;
using System.Text; 
using Server.Misc;
using Server.Gumps; 
using Server.Items;
using Server.Multis;
using Server.Network;
using Server.Mobiles;
using Server.Commands;
using Server.Targeting;
using System.Collections;
using System.Collections.Generic;

namespace Server.Items
{
    public class TrapperKit : Item
    {
        private int m_UsesRemaining;

        [CommandProperty( AccessLevel.GameMaster )]
        public int UsesRemaining
        {
            get{ return m_UsesRemaining; }
            set{ m_UsesRemaining = value; InvalidateProperties(); }
        }

        [Constructable]
        public TrapperKit() : base( 0x1EB7 )
        {
            Name = "Trapper's Kit";
            ItemID = 0x1EB7;
            Weight = 0.10;
            Hue = 0;
            m_UsesRemaining = 25;
        }

        public override void AddNameProperties( ObjectPropertyList list )
        {
            base.AddNameProperties( list );

            if ( m_UsesRemaining >= 0 )
                list.Add( 1060658, "Uses Remaining\t{0}", m_UsesRemaining.ToString() );
        }

        public override void OnDoubleClick( Mobile from )
        {
            if ( !IsChildOf( from.Backpack ) )
            {
                from.SendLocalizedMessage( 1042001 ); // That must be in your pack for you to use it.
                return;
            }
            else
            {
                if ( m_UsesRemaining > 1 )
                {
                    m_UsesRemaining -= 1;
                    InvalidateProperties();
                    from.SendMessage( "Place trap where?" );
                    from.Target = new InternalTarget();
                }
                else if ( m_UsesRemaining == 1 )
                {
                    from.SendMessage( "Place trap where?" );
                    from.Target = new InternalTarget();
                    this.Delete();
                    return;
                }
            }
        }

        private class InternalTarget : Target
        {
            private AnimalSpikeTrap m_AnimalSpikeTrap;

            public InternalTarget() : base( 3, true, TargetFlags.None )
            {
            }

            protected override void OnTarget( Mobile from, object targeted )
            {
                LandTarget land = targeted as LandTarget;

                if ( targeted == null )
                {
                    return;
                }

                if ( land == null )
                {
                    return;
                }

                if ( (targeted != null) && targeted == land)
                {
                    return;
                }

                if ( !from.CanSee( targeted ) )
                {
                    from.SendLocalizedMessage( 500237 ); // Target can not be seen.
                    return;
                }

                if ( (targeted != null) && targeted is LandTarget)
                {
                    from.SendMessage( "You set a trap" );
                    m_AnimalSpikeTrap.Map = from.Map;
                    m_AnimalSpikeTrap.MoveToWorld(targeted.Location, targeted.Map);
                }
                else
                {
                    from.SendMessage( "You cannot set a trap there" );
                    return;
                }
            }

            protected override void OnTargetOutOfRange( Mobile from, object targeted )
            {
                from.LocalOverheadMessage( MessageType.Regular, 0x3B2, 502825 ); // That location is too far away
            }
        }

        public TrapperKit( Serial serial ) : base( serial )
        {
        }

        public override void Serialize( GenericWriter writer )
        {
            base.Serialize( writer );
            writer.Write( (int) 1 ); // version

            writer.Write( (int) m_UsesRemaining );
        }

        public override void Deserialize( GenericReader reader )
        {
            base.Deserialize( reader );
            int version = reader.ReadInt();

            m_UsesRemaining = (int)reader.ReadInt();
        }
    }
}

Can anyone help me recognize a Landtile as the Target location and then MoveToWorld my trap on that location?

Help from the community is appreciated.

Many Thanks
 
Try this:

C#:
            protected override void OnTarget( Mobile from, object targeted )
            {
                if ( !from.CanSee( targeted ) )
                {
                    from.SendLocalizedMessage( 500237 ); // Target can not be seen.
                    return;
                }

                if (targeted is LandTarget)
                {
                    LandTarget land = targeted as LandTarget;
                    from.SendMessage( "You set a trap" );
                    m_AnimalSpikeTrap.Map = from.Map;
                    m_AnimalSpikeTrap.MoveToWorld(land.Location, land.Map);
                }
                else
                {
                    from.SendMessage( "You cannot set a trap there" );
                    return;
                }
            }
 
Hi Lokai

I can't thank you enough for the fast reply and for setting me on the right track with your example.

It did give me a small hiccup (no biggy).

For anyone's information . .

1) At first it did not compile because LandTarget does not contain a definition of 'Map'. To solve that I used from.Map instead of land.Map

2) Then it compiled but gave me a null ref Crash so I inserted a few 'SendMessage' comments to follow the process (even with a Crash you still see the Journal entries).

3) When I ran it again, after the Crash the Journal entries showed me that the null ref problem was caused by the line 'm_AnimalSpikeTrap.MoveToWorld(land.Location, from.Map);'.

4) I then realized the script (at that line) did not know what m_AnimalSpikeTrap was. To solve the problem I inserted 'm_AnimalSpikeTrap = (new AnimalSpikeTrap());' on a line before 'm_AnimalSpikeTrap.MoveToWorld(land.Location, from.Map);'.

So . . here is what I ended up with (and would have never got there without your help).

Note: My SendMessage lines are commented out after the script performed well for me but I left them in (if it helps) for this example.

Code:
            protected override void OnTarget( Mobile from, object targeted )
            {
                if ( !from.CanSee( targeted ) )
                {
                    from.SendLocalizedMessage( 500237 ); // Target can not be seen.
                    return;
                }

                if (targeted is LandTarget)
                {
                    LandTarget land = targeted as LandTarget;
                    //from.SendMessage( "A) You set a trap" );

                    m_AnimalSpikeTrap = (new AnimalSpikeTrap());
                    //from.SendMessage( "B) You set a trap" );

                    m_AnimalSpikeTrap.MoveToWorld(land.Location, from.Map);
                    //from.SendMessage( "C) You set a trap" );
                }
                else
                {
                    from.SendMessage( "You cannot set a trap there" );
                     return;
                }
            }

Thanks again Lokai . . love this community.

*bows*
 

Active Shards

Donations

Total amount
$0.00
Goal
$1,000.00
Back