Enemy Unit does not move towards Friendly Units to shoot

Hi CM! Big fan, recent newcomer to your tutorials and I absolutely love the way you code and how you teach clean code.

I’ve followed every step, double and triple checked the logic but it seems as if the enemy units do not physically move to the grid position where they can fire at the friendly units.

I’ve tried adding an action value to the spinaction (5) to see if the moveaction is just somehow returning 0 instead of 10 or 20 (based on the targetCountAtGridPosition * 10 logic), and the enemies do not spin.

Checking the targetCountAtGridPosition with a debug, it seems like the logic can determine where the best gridposition is, and is returning the correct actionvalues of 10 or 20. This logic is firing off so the enemies will never take the lower actionvalue Spin action, which is working correctly.

However, even when the enemy units receive the best gridposition to move to, they aren’t actually moving to shoot at the friendly units, and then it just ends their turn as they have no valid action to take.

Any help on this will be appreciated! Thanks

I found the culprit!

I had an extra curly bracket between the if/else statement under the TryTakeEnemyAction function.

private bool TryTakeEnemyAIAction(Unit enemyUnit, Action onEnemyAIActionComplete)
    {
        EnemyAIAction bestEnemyAIAction = null;
        BaseAction bestBaseAction = null;

        foreach (BaseAction baseAction in enemyUnit.GetBaseActionArray())   
        {
            if (!enemyUnit.CanSpendActionPointsToTakeAction(baseAction))
            {
                // Enemy cannot afford this action
                continue;
            }

            if (bestEnemyAIAction == null)
            {
                bestEnemyAIAction = baseAction.GetBestEnemyAIAction();
                bestBaseAction = baseAction;
            }
            else
            {
                EnemyAIAction testEnemyAIAction = baseAction.GetBestEnemyAIAction();
                if (testEnemyAIAction != null && testEnemyAIAction.actionValue > bestEnemyAIAction.actionValue)
                {
                    bestEnemyAIAction = testEnemyAIAction;
                    bestBaseAction = baseAction;
                }
            }
        }

Right after the if (testEnemyAIAction != null && testEnemyAIAction.actionValue > bestEnemyAIAction.actionValue) statement.

This caused the game to never replace the old best EnemyAI Action with the new best EnemyAIAction, hence the enemy never moves and the turn just ends!

3 hours of debugging. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms