Armed NPCs currently have a targetID int that represented the index position in the array of the NPC the armed NPC is targetting.
The problem arises when an NPC is removed from the new vector container. The indexes of all NPCs change and targetID of the armed NPC no longer points to the correct NPC. (and could crash!)
To fix this I planned on adding unique npcID and uniqueTargetID ints to the NPC class. To minimize looping through the NPC vector I planned on using this only when a NPC is removed.
Code: Select all
void updateNPCTargetIDs()
{
int i = 0;
for (auto npc : NPCVector)
{
for (auto npc2 : NPCVector)
{
if (npc->uniqueTargetID == npc2->npcID)
{
npc->targetID = i;
}
i += 1;
}
}
}
NPC class holds index number of other NPCs in the NPCVector vector. Index number is invalidated upon removal of an NPC.
The questions:
Is this code sample a good way to re-validate these indexes? Would an array give me better performance when I need to hold an index to another member in the container?