Weird code

06/11/2011 23:49 miketheking#1
well would some1 tell me why this code gives me an error saying that not all code patterns returns a value while i think all of them do

Code:
public static Game.Location getfreemobcoord(ushort X, ushort Y, ushort Spread)
        {
            Game.Location loc = new Game.Location();
            loc.X = (ushort)Program.Rnd.Next(X, X + Spread);
            loc.Y = (ushort)Program.Rnd.Next(Y, Y + Spread);
            foreach (Game.NPC npc in Game.World.H_NPCs.Values)
            {
                if (npc.Loc.Equals(loc))
                {
                    return getfreemobcoord(X, Y, Spread);
                }
                else
                {
                    foreach (Game.Mob mon in Game.World.H_Mobs.Values)
                    {
                        if (mon.Loc.Equals(loc))
                        {
                            return getfreemobcoord(X, Y, Spread);
                        }
                        else
                        {
                            return loc;
                        }
                    }
                }
            }
        }
06/12/2011 00:11 _DreadNought_#2
Quote:
Originally Posted by miketheking View Post
well would some1 tell me why this code gives me an error saying that not all code patterns returns a value while i think all of them do

Code:
public static Game.Location getfreemobcoord(ushort X, ushort Y, ushort Spread)
        {
            Game.Location loc = new Game.Location();
            loc.X = (ushort)Program.Rnd.Next(X, X + Spread);
            loc.Y = (ushort)Program.Rnd.Next(Y, Y + Spread);
            foreach (Game.NPC npc in Game.World.H_NPCs.Values)
            {
                if (npc.Loc.Equals(loc))
                {
                    return getfreemobcoord(X, Y, Spread);
                }
                else
                {
                    foreach (Game.Mob mon in Game.World.H_Mobs.Values)
                    {
                        if (mon.Loc.Equals(loc))
                        {
                            return getfreemobcoord(X, Y, Spread);
                        }
                        else
                        {
                            return loc;
                        }
                    }
                }
                [B]return null;//or 0?[/B]
            }
        }
.
06/12/2011 03:09 _tao4229_#3
Wrong, its saying that if that collection World.H_NPC.Values or whatever is empty, the foreach wont execute once and therefore the method will end without returning anything.
06/12/2011 07:26 -Shunsui-#4
by the way this that you are doing is poor, i recommend using Kinshi's TinyMap server for your source, it helps as well with this you are doing
06/12/2011 13:15 _DreadNought_#5
Quote:
Originally Posted by _tao4229_ View Post
Wrong, its saying that if that collection World.H_NPC.Values or whatever is empty, the foreach wont execute once and therefore the method will end without returning anything.
So yea my code would fix that. You are correct tho.

Should note my code I put in is mis-placed, move it to somewhere after the foreach loop.
06/12/2011 15:26 BaussHacker#6
Quote:
Originally Posted by _DreadNought_ View Post
.
Why would you return it at first? Then it would loop one time and return. It should be outside the foreach loop.
06/12/2011 16:08 _DreadNought_#7
Was a mistake since I did it in this tiny quick reply thingy. Look at the post above yours :)