Original Post
im not 100% sure that its creating non-NULL pointers but i dont know what else it could be doing.. THIS IS HOW I CHECK FOR NULL // AddAgent ///////// THE NEXT TO MAY NOT BE NEEDED BUT THEY COMPLETE THE CHAIN FROM NEW TO addAgent()...... /////////////// Agent::perform() Agent::Mate() ive followed the call from "Mate()"all the way to where it adds and the agent is fully defined... also I can call addAgent() all that i want durring my loading process, but durring run-time, it will crash... so the vector inside the map must be allocating non NULL Agent* any Ideas..
Agent * newAgent = NULL;
for (std::map<std::string, std::vector<Agent*> >::iterator iterator = agentMap.begin(); iterator != agentMap.end(); ++iterator)
{
for (std::vector<Agent*>::iterator agentIterator = iterator->second.begin(); agentIterator != iterator->second.end(); ++agentIterator)
{
// NULL CHECKING
if((*agentIterator) == NULL)
continue;
// crashes inside v--this function call after addAdd.. because (*agentIterator) is undefined
(*agentIterator)->Perceive(agentMap);
(*agentIterator)->Decide();
// returns new Agent or NULL
newAgent = (*agentIterator)->Perform(m_timer.m_elapsedTime);
(*agentIterator)->Update(m_timer.m_elapsedTime);
terrain->PutInBounds((*agentIterator)->m_state.GetPos());
if(newAgent != NULL)
AddAgent(newAgent); // adds agent to the map
}
}
void Test::AddAgent(Agent * pAgent)
{
if(pAgent == NULL)
return;
char buf[12];
agentID++;
std::string ID = "Agent_";
ID += itoa(agentID,buf,10);
(std::string)pAgent->GetName() = ID;
agentMap[pAgent->m_pStats->m_agentType].push_back(pAgent);
}
Agent * Agent::Perform(float eTime)
{
// other code clipped
if(m_action == "mate")
{
return Mate(m_pTarget);
}
return NULL;
}
Agent * Agent::Mate(Agent *& rAgent)
{
if(!rAgent->m_state.m_canAction)
return NULL;
// create new
Behavior * pBeh = new Behavior("Beh_offspring_");
Stats * pStats = new Stats("Stat_offspring_");
pStats->Merge(*m_pStats, *rAgent->m_pStats, MUTATION);
pBeh->Merge(*m_pBehavior, *rAgent->m_pBehavior, MUTATION);
WereModel * pModel = NULL;
if((rand() % 2) == 0)
pModel = m_pModel;
else
pModel = rAgent->m_pModel;
// create new
Agent * pnew = new Agent(pBeh,pStats,pModel, "Agent_");
// move to midpoint of parents
pnew->MoveTo((m_state.m_pos.m_position.x + rAgent->m_state.m_pos.m_position.x) * 0.5f,
(m_state.m_pos.m_position.y + rAgent->m_state.m_pos.m_position.y) * 0.5f,
(m_state.m_pos.m_position.z + rAgent->m_state.m_pos.m_position.z) * 0.5f);
return pnew;
}