2017-12-29 17 views
0

나는이 오류를 잠시 동안 사용 해왔다. 문제를 해결하는 방법을 모르겠다.C++ Map/Set iterator 증가하지 않음 오류

저는 개발중인 게임 내 모든 엔티티를 처리하는 EntityManager를 가지고 있습니다. 각 엔티티는 각 엔티티가 제공되는 임의의 ID로 정렬 된 맵에 추가됩니다. 일반적으로 "이"같은 통해,

void EntityManager::RemoveEntity(Entity* entity) 
{ 
    entities.erase(entity->GetID()); 
    delete entity; 
} 

삭제되고있는 개체를 전달 : 나는 게임 내에서 엔티티를 삭제할 때

void EntityManager::AddEntity(Entity* entity) 
{ 
    int ID = rand(); 
    for(int i = 0; i < IDS.size(); i++) 
    { 
     if(IDS[i] == ID) 
     { 
      ID = rand(); 
      i = 0; 
     } 
     else 
     { 
      break; 
     } 
} 

entity->SetID(ID); 
entities.insert(pair<int, Entity*>(ID, entity)); 

, 단순히이 RemoveEntity 함수를 호출 :

virtual void Effect::RemoveEffect() 
{ 
    DeleteEntity(this); 
} 

void DeleteEntity(Entity* entity) 
{ 
    entityManager->RemoveEntity(entity); 
} 

게임의 모든 항목은이 엔티티 클래스에서 상속받습니다.

제가하는 데 문제는 내가 "RemoveEntity"함수를 호출 할 때 발생이 오류이며이 코드 줄 실행 :

entities.erase(entity->GetID()); 

이 게임은 단순히지도/설정 반복자와 충돌하지 않음을 증분 오류 및 나는 이유를 알아낼 수 없습니다. 이 오류가있는 사람들에 대한 다른 주제를 살펴 봤지만 해결 방법은 for 루프와 같은 것을 변경하는 것입니다.

내가주의해야하지만이 오류는 지금까지 내가 엔티티 이런 식으로 삭제있을 때 발생 : 나는 개체가 삭제 될 때 지연 타이머가

destroyTimer += deltaTime; 
if(destroyTimer >= destroyDelay) 
{ 
    entityManager->RemoveEntity(this); 
} 

. 이 코드 섹션은 모든 프레임에서 GameLoop 중에 호출되는 Update 함수에 있습니다. 위와 같은 타이머없이 즉석에서 "RemoveEntity"함수를 호출 할 때 Entity를 제거하면 오류없이 작동하지만 오류가 발생하는 이유는 알 수 없습니다.

무슨 일이 일어나는지 아는 사람 있습니까?

편집 위의 예에 도시 RemoveEntity 함수는 각각의 개체가있는 엔티티의 업데이트 기능 내에 는 전체 업데이트 루프는 다음과 같다 :

void PointEffect::Update(float deltaTime) 
{ 
    Entity::Update(deltaTime); 

    destroyTimer += deltaTime; 
    if(destroyTimer >= destroyDelay) 
    { 
     RemoveEffect(); 
    } 
} 

엔터티 :: 업데이트 (deltaTime)은 바로이 호출

각각의 개체에서 발견 된 업데이트 루프입니다 다음 GameStates 업데이트 기능에 의해 호출되는 실제 EntityManagers 업데이트 루프에 의해 불려
virtual void Update(float deltaTime) 
{ 
    if(animationHandler != nullptr) 
    { 
     animationHandler->Update(deltaTime);    
    } 

    if(moveSpeed != 0) 
    { 
     position += velocity * moveSpeed; 
    } 

    if(collision != nullptr) 
    { 
     collision->Update(GetSpriteRect(), position); 
    } 
} 

그런 다음 GameLoop 자체에서 호출됩니다. EntityManager의 업데이트 기능 :

void EntityManager::Update(float deltaTime) 
{ 
if(!entities.empty()) 
{ 
     for(const auto entity : entities) 
     {   
      entity.second->Update(deltaTime); 
     } 
    } 
} 

GameState 업데이트 기능 : EntityManager::Update에서 그 for 루프가 실행되는 동안

void MainGame::Update(float deltaTime) 
{ 
    entityManager->Update(deltaTime); 
} 
+2

[MCVE]를 게시하십시오. 또한 엔티티를 "자체 삭제"하는 것은 나쁜 디자인의 코드 냄새와 신호입니다. 당신은 아마 그 일을 처리하는 일종의 관리자가 필요합니다. –

+1

이것이 당신의 [이전 질문]과 어떻게 다른가요? (https://stackoverflow.com/questions/47851790/c-game-map-set-iterator-no-incrementable- 오류)이 문제가 있습니까? – 1201ProgramAlarm

+0

@ 1201ProgramAlarm 그 중 하나가 엉망 이었지만, 지금은 문제를 발견 했음에도 불구하고 내가 무엇에 대해 이야기하고 있는지 전혀 알지 못했습니다. – codelyoko373

답변

1

당신은 entities에서 entity을 삭제할 수 없습니다. for 루프가 여전히 반복기를 사용하고 있기 때문입니다.루프가 실행 중일 때 삭제할 엔티티를 메모하거나 기존의 for 루프를 사용하여 Update을 호출하기 전에 루프 반복자를 다음 요소로 이동해야합니다. 다른 엔티티 만 삭제하는 것은 아닙니다).

+0

그게 효과가 있습니다!, 고맙습니다. :) – codelyoko373