jayates

Member
Trying to put together a vendor who'll buy Pet Powerscrolls according to their values (based off the Powerscroll Exchange system). I got a wierd error though. Can someone help me with it? Here's the error:
+ CUSTOM/CustomCooking/Questgiver/Gordon.cs:
CS0161: Line 100: 'Server.Mobiles.Gordon.OnDragDrop(Server.Mobile, Server.Item)': not all code paths return a value
script:
C#:
using System;
using System.Collections;
using Server.Items;
using Server.Targeting;
using Server.ContextMenus;
using Server.Gumps;
using Server.Misc;
using Server.Network;
using Server.Spells;
using System.Collections.Generic; 

namespace Server.Mobiles
{
    [CorpseName( "gordon corpse" )]
    public class Gordon : Mobile
    {
        public virtual bool IsInvulnerable{ get{ return true; } }
        public static int i_PPSReward = 0;
        
        [Constructable]
        public Gordon()
        { 
            Name = "Gordon";
            Title = " ...Pet Powerscroll Exchanges";
            BodyValue = 400;            
            HairItemID = 0x2045;
            HairHue = 2227;            
            Hits = 10000;
            Hue = 0;
            Direction = Direction.East;

        AddItem(new Backpack());
        AddItem( new Tunic( Utility.RandomDyedHue() ) );
        AddItem( new Boots() );
        AddItem( new LongPants( Utility.RandomNeutralHue() ) );
            AddItem(new CooksRobe() );
        
            CantWalk = true;
            Blessed = true;
            
            }

            public virtual int GetBootsHue()
            {
            return 1157;
        }

        public Gordon( Serial serial ) : base( serial )
        {
        }
    
        public override void GetContextMenuEntries(Mobile from, List<ContextMenuEntry> list) 
            {  
                    base.GetContextMenuEntries( from, list ); 
                    list.Add( new GordonEntry( from, this ) ); 
            } 

        public override void Serialize( GenericWriter writer )
        {
            base.Serialize( writer );
            writer.Write( (int) 0 );
        }

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

        public class GordonEntry : ContextMenuEntry
        {
            private Mobile m_Mobile;
            private Mobile m_Giver;
            
            public GordonEntry( Mobile from, Mobile giver ) : base( 6146, 3 )
            {
                m_Mobile = from;
                m_Giver = giver;
            }

            public override void OnClick()
            {
                

                          if( !( m_Mobile is PlayerMobile ) )
                    return;
                
                PlayerMobile mobile = (PlayerMobile) m_Mobile;

                {
                    if ( ! mobile.HasGump( typeof( GordonGump ) ) )
                    {
                        mobile.SendGump( new GordonGump( mobile ));
                        
                    } 
                }
            }
        }

        public override bool OnDragDrop( Mobile from, Item dropped )
        {                  
            Mobile m = from;
            PlayerMobile mobile = m as PlayerMobile;

            if ( mobile != null)
            {
               if ( dropped is PetPowerScroll )
            {
                PetPowerScroll deed = (PetPowerScroll)dropped;
                i_PPSReward = 0; //default value
                if ( deed.Value == 120.0 )
                    i_PPSReward = 400;
                else if ( deed.Value == 115.0 )
                    i_PPSReward = 200;
                else if ( deed.Value == 110.0 )
                    i_PPSReward = 100;
                else if ( deed.Value == 105.0 )
                    i_PPSReward = 50;        
            }
            
            else i_PPSReward = 0;
            dropped.Delete();
            
            if ( i_PPSReward > 0 )
            {
                from.AddToBackpack( new PSCredits(i_PPSReward) );
                from.SendMessage( 1173, "You were rewarded {0} Powerscroll Credits for this scroll!", i_PPSReward );
            }
            
            else if (i_PPSReward == 0 ) from.SendMessage( 1173, "That wasn't a Pet Powerscroll... hopefully is wasn't anything of great value, cause I put it in the trash!" );
            
            return true;
        
           }        
        }
    }
}
 
Hey,

You're getting that error because you only return inside of
C#:
if (mobile != null)

See this:

C#:
public override bool OnDragDrop(Mobile from, Item dropped)
{
    base.OnDragDrop(from, dropped);
    Mobile m = from;
    PlayerMobile mobile = m as PlayerMobile;

    if (mobile != null)
    {
        //...
        //Removed code here to make it more clear
        //...

        //You are returning only here but you also need
        //need to return incase mobile is null
        return true;
    }

    //return true/false here too
    return true;
}
 
Thank you! So, my new code should look like this?
C#:
        public override bool OnDragDrop( Mobile from, Item dropped )
        {    
            base.OnDragDrop(from, dropped);        
            Mobile m = from;
            PlayerMobile mobile = m as PlayerMobile;

            if ( mobile != null)
            {
               if ( dropped is PetPowerScroll )
            {
                PetPowerScroll deed = (PetPowerScroll)dropped;
                i_PPSReward = 0; //default value
                if ( deed.Value == 120.0 )
                    i_PPSReward = 400;
                else if ( deed.Value == 115.0 )
                    i_PPSReward = 200;
                else if ( deed.Value == 110.0 )
                    i_PPSReward = 100;
                else if ( deed.Value == 105.0 )
                    i_PPSReward = 50;    
        return true;                
            }
            
            else i_PPSReward = 0;
            dropped.Delete();
            
            if ( i_PPSReward > 0 )
            {
                from.AddToBackpack( new PSCredits(i_PPSReward) );
                from.SendMessage( 1173, "You were rewarded {0} Powerscroll Credits for this scroll!", i_PPSReward );
            }
            
            else if (i_PPSReward == 0 ) from.SendMessage( 1173, "That wasn't a Pet Powerscroll... hopefully is wasn't anything of great value, cause I put it in the trash!" );
            
            return false;
        
           }        
        }
    }
}
 
Thank you! So, my new code should look like this?
C#:
        public override bool OnDragDrop( Mobile from, Item dropped )
        {   
            base.OnDragDrop(from, dropped);       
            Mobile m = from;
            PlayerMobile mobile = m as PlayerMobile;

            if ( mobile != null)
            {
               if ( dropped is PetPowerScroll )
            {
                PetPowerScroll deed = (PetPowerScroll)dropped;
                i_PPSReward = 0; //default value
                if ( deed.Value == 120.0 )
                    i_PPSReward = 400;
                else if ( deed.Value == 115.0 )
                    i_PPSReward = 200;
                else if ( deed.Value == 110.0 )
                    i_PPSReward = 100;
                else if ( deed.Value == 105.0 )
                    i_PPSReward = 50;   
        return true;               
            }
           
            else i_PPSReward = 0;
            dropped.Delete();
           
            if ( i_PPSReward > 0 )
            {
                from.AddToBackpack( new PSCredits(i_PPSReward) );
                from.SendMessage( 1173, "You were rewarded {0} Powerscroll Credits for this scroll!", i_PPSReward );
            }
           
            else if (i_PPSReward == 0 ) from.SendMessage( 1173, "That wasn't a Pet Powerscroll... hopefully is wasn't anything of great value, cause I put it in the trash!" );
           
            return false;
       
           }       
        }
    }
}
That will still gives you the same error, take a closer look at your code.
If you go from top to bottom, you will see that you are only returning a value inside of your
C#:
if (mobile != null)
{
    ...
}

but since we are required to return something (in this case the bool true/false) we have to make sure that no matter how the method runs we will always return a value.

See this example:
C#:
public int Addition(int a, int b)
{
    //We can return earlier but we still need to return a value incase we don't return here
    if (a == 0 && b == 0)
        return 0;
    else if (a > 0 && b == 0)
        return a;

    return a + b;
}

So if you change this in your code it should work:
C#:
                else
                    i_PPSReward = 0;

                dropped.Delete();
          
                if ( i_PPSReward > 0 )
                {
                    from.AddToBackpack( new PSCredits(i_PPSReward) );
                    from.SendMessage( 1173, "You were rewarded {0} Powerscroll Credits for this scroll!", i_PPSReward );
                }
                else if (i_PPSReward == 0 )
                    from.SendMessage( 1173, "That wasn't a Pet Powerscroll... hopefully is wasn't anything of great value, cause I put it in the trash!" );
            
                //Move this down
                //return false;
            }       
            //to here
            return false;
        }
    }
}
 
Thank you for the awesome help. I'll rework it in tomorrow morning. I've had a long day and i've turned in for the night.
 

Active Shards

Donations

Total amount
$0.00
Goal
$1,000.00
Back