Hello,
I implement an Temporal planning for my Ai. Therefor i implemented the following function to simulate the next GameState. http://pastebin.com/R14hMvvr
TArray<FActionScore> UPerformAction::getSkillScore(UAIGameState* newGameState, ABaseCharacter* owner, int depth)
{
TArray<FActionScore> temporalActionScoreList;
FActionScore attackScore = getBestAutoAttack(newGameState->getEnemyCurrentAIState());
if (attackScore.score > 0)
{
attackScore.score = 0.2f;
temporalActionScoreList.Add(attackScore);
}
else
{
attackScore.score = 0.2f;
attackScore.action = AIAction::IDLE;
attackScore.target = m_owner;
temporalActionScoreList.Add(attackScore);
}
for (int i = 0; i < 8; i++)
{
if (owner->canUseSkill(i))
{
USkill* skill = owner->skillList[i];
if (skill && !(owner->skillIsOnCooldown(i)))
{
switch (skill->properties.targetType)
{
case TargetType::ENEMY:
temporalActionScoreList.Append(calcTempSkillScore(owner, newGameState->getEnemyCurrentAIState(), skill, i));
break;
case TargetType::FRIEND:
temporalActionScoreList.Append(calcTempSkillScore(owner, newGameState->getAlliesCurrentAIState(), skill, i));
break;
case TargetType::SELFFRIEND:
temporalActionScoreList.Append(calcTempSkillScore(owner, newGameState->getAlliesCurrentAIState(), skill, i));
temporalActionScoreList.Append(calcTempSkillScore(owner, newGameState->getOwnerState(), skill, i));
break;
case TargetType::SELF:
temporalActionScoreList.Append(calcTempSkillScore(owner, newGameState->getOwnerState(), skill, i));
break;
default:
break;
}
}
}
}
if (depth == 0)
{
temporalActionScoreList.Sort(FActionScore::ConstPredicate);
TArray<FActionScore> bestActionList;
bestActionList.Add(temporalActionScoreList[0]);
return bestActionList;
}
else
{
FActionScore bestAction;
bestAction.score = 0.f;
TArray<FActionScore> bestActionList;
for (auto& action : temporalActionScoreList)
{
UAIGameState* nextGameState = simulateNextState(newGameState, action);
TArray<FActionScore> bestTempActionList = getSkillScore(nextGameState, nextGameState->getOwner(), depth--);
float score = 0.f;
for (auto& tempAction : bestTempActionList)
{
score = action.score * tempAction.score;
if (score < bestAction.score)
{
bestAction = tempAction;
bestActionList = bestTempActionList;
}
}
}
bestActionList.Add(bestAction);
return bestActionList;
}
return temporalActionScoreList;
}
This function calculates an action score for each action and saves this action in the temporalActionScoreList.
With these action I simulate my next gameState and call this function again.But instead of creating a new instance of the temporalList it uses the same instance of the temporalActionScoreList and appends my future actions. This causes my for loop to run infinite because every time i call getSkillScore it appends more actions.
Is there anyway to force the unreal engine to create a new Instance of the temporalActionScoreList?