WTF? NPC/Map Help...

11/25/2022 04:19 LepEatWorld#1
As you can see, it'll update the NPC in the Database and for each player. If the map refreshes it will re-update the NPC back to the original view for the player. Also, weirdly enough, it'll add the same NPC to each map to it's original 1st location added:
[Only registered and activated users can see links. Click Here To Register...]

Working on adding the Guild Controller scrolls into the source but, I'm getting this weird issue: I can add the NPC with the same UID to every map that the NPC does not exist in. It only updates to one map on the database, so it's not like it's making multiple. It's the same NPC with the same code.

This is the script I'm currently working with, it was from an example of Furniture from another source that I had lying around:
Code:
if (client.HasItem(720021))
                    {
                        client.DeleteItem(720021);
                        var _newNPC = new DbNpc()
                        {
                            UID = 102093,
                            Type = (NpcType)2,
                            Mesh = look,
                            X = packet.DataLow,
                            Y = packet.DataHigh,
                            Map = client.MapID
                        };
                        ServerDatabase.Context.Npcs.AddOrUpdate(_newNPC);
                        client.Map.Insert(new Npc(_newNPC));
                    }
                    break;
Code:
public Npc(DbNpc _npc)
        {
            BaseNpc = _npc;
            UID = _npc.UID;
            Location = new Point(_npc.X, _npc.Y);
            SpawnPacket = SpawnNpcPacket.Create(this);
            foreach (var player in PlayerManager.Players.Values)
            {
                player.Send(SpawnPacket);
            }
        }
Code:
public static SpawnNpcPacket Create(Npc _npc)
        {
            var packet = new SpawnNpcPacket();
            packet.UID = _npc.UID;
            packet.X = _npc.X;
            packet.Y = _npc.Y;
            packet.Mesh = _npc.Mesh;
            packet.Type = _npc.Type;
            if (_npc.Name != null && _npc.Name.Length > 0)
            {
                packet.Name = new NetStringPacker();
                packet.Name.AddString(_npc.Name);
            }

            return packet;
        }
11/27/2022 04:20 Arcо#2
Without looking at the code, it can be that the collection containing your NPCs isn't updating. Can we see your Map.Insert(Npc) function?
11/27/2022 06:06 LepEatWorld#3
Quote:
Originally Posted by Arcо View Post
Without looking at the code, it can be that the collection containing your NPCs isn't updating. Can we see your Map.Insert(Npc) function?
Code:
        public bool Insert(ILocatableObject obj)
        {
            if (obj == null)
                return false;
            if (Objects.ContainsKey(obj.UID))
                return false;
            obj.Map = this;
            Objects.TryAdd(obj.UID, obj);
            return true;
        }
11/27/2022 07:14 Santa#4
Code:
        public bool Insert(ILocatableObject obj)
        {
            if (obj == null)
                return false;
            if (Objects.ContainsKey(obj.UID))
                return false;
            obj.Map = this;
            Objects.TryAdd(obj.UID, obj);
            return true;
        }
I''m going to guess this returns false when you call it from the following (because it already exists):
Code:
if (client.HasItem(720021))
                    {
                        client.DeleteItem(720021);
                        var _newNPC = new DbNpc()
                        {
                            UID = 102093,
                            Type = (NpcType)2,
                            Mesh = look,
                            X = packet.DataLow,
                            Y = packet.DataHigh,
                            Map = client.MapID
                        };
                        ServerDatabase.Context.Npcs.AddOrUpdate(_newNPC);
                        client.Map.Insert(new Npc(_newNPC));
                    }
                    break;
11/27/2022 15:29 LepEatWorld#5
Quote:
Originally Posted by Santa View Post
I''m going to guess this returns false when you call it from the following (because it already exists):
Correct, I didn't realize until Arco pointed out the ContainsKey part. I got it working now, although I'm not too sure if this is the best solution to the project.
I added the following to the normal script:
Code:
foreach (var maps in MapManager.ActiveMaps.Values)
    maps.Remove(new Npc(_newNPC));
I tried to use a AddOrUpdate instead of a Insert but it wasn't working out for me, I'll need to look into why exactly it wasn't working at a later time. I also tried to put a Objects.TryUpdate inside the Insert method where the Object exist, but the same issue was occuring, so I gave up and decided to just remove and re-add rather than update.

Thank you both for the insight and if you have a better way of dealing with this I'd love to learn about it.