One possibility you have is to timestamp a few things. You''ve already mentioned the idea of decaying information over time. To me, that means you''d have to know when you actually learnt something (timestamp), and when time has elapsed, you remove it. Be useful to sort from oldest to newest as well.
My idea is that rather than store a pointer straight into the near memory of the NPC, you store a unique identifier for that NPC (serial for MMORPG type stuff), and a timestamp as to when you last got information. When you want to look at their near info, you do a lookup to get a pointer to the NPC from the serial. Get a pointer to the near memory, and iterate through that list and only examining information older than the time stamp. Something kinda like
struct FarMemoryEntry
{
SERIAL npcID;
long timestamp;
};
struct Information
{
string info;
long timeLearnt;
};
class NPC
{
private:
vector< Information > nearMemory;
vector< FarMemoryEntry > farMemory;
SERIAL myID;
public:
(vector< Information > *)GetMemory( void );
void CompareMemory( void );
SERIAL GetID( void );
};
then something like:
void NPC::CompareMemory( void )
{
for( long iNPC = 0; iNPC < farMemory.size(); iNPC++ )
{
NPC *targNPC = SomeFuncToGetPointer( farMemory[iNPC].npcID );
(vector< Information > *)otherMemory = targNPC->GetMemory();
for( long iCounter = 0; iCounter < otherMemory->size(); iCounter++ )
{
if( (*otherMemory)[iCounter].timeLearnt < farMemory[iNPC].timestamp ) // Facts we knew from prior contact
{
DoSomething();
}
else
{
cout << "New info, ignoring" << endl;
}
}
}
}
Something kinda like that. Sorry for the sheer amount of code
but I find it easier to express my ideas that way. And I''m not sure about the (*otherMemory)[iCounter] syntax, but it looks good
.
Anyway, this only allows access to info they knew since last contact. So it''s no longer a global memory of sorts. And it gets around storing pointers to potentially dead objects, as you''re only storing a serial. You''ll have to put some safety in of course, but that''s the general idea.
You''ll already be using timestamps most likely if you support idea decay. And you can optimize this by having sorted from oldest->newest. Once you hit an entry more recent than prior contact, you can return, knowing anything that follows is newer than you should know.