Selected unit still "active" if dead when transitioning to player turn

Not sure if I missed something, or this was not covered, but if the selected unit at the end of my turn dies during the enemy turn, they are taken off the unit lists correctly, but when the game turns back to my turn, they are still the selectedUnit in the UnitActionSystem, meaning that the game displays valid grid positions for the dead units selected action. If I click on any of those squares, I get a very understandable error.

I fixed this by changing Die in Unit to set the selectedUnit in the UniActionsSystem to the first one in the friendlyUnitList, if the list.count > 0.

    private void Die()
    {
        OnDead?.Invoke(this, EventArgs.Empty);
        List<Unit> friendlyUnitList = UnitManager.Instance.GetFriendlyUnitList();
        if (friendlyUnitList.Count > 0)
        {
            UnitActionSystem.Instance.SetSelectedUnit(friendlyUnitList[0]);
        }
    }

To do this, I had to make SetSelectedUnit public. Now arguing with myself is this is the correct way, or to get the UnitActionSystem to subscribe to an event. I’m going with the above method as the UAS is a singleton, but happy to be convinced otherwise (or an even better solution, of course).

I enjoyed the recent devology that CodeMonkey guested on. Good stuff. One of my mental blocks is adding my first iterations of code to the code produced by 5 months of hard work for a course. It seems like sacrilege. This (and Nathan’s 3d combat course) have got me thinking it is better to write my code in a separate class that extends what we do in the courses? Just tag the methods as virtual and copy them into my class and tinker with them there. That way, when I break them, I don’t break the game, and I don’t disturb the surrounding code. Hmm. One to play with.

1 Like

Hmm you’re right I missed that, in all my testing it never happened that last selected Unit died so I never noticed that.

The better approach is probably to handle that in the UnitActionSystem itself, it makes more sense that way rather than the Unit itself being responsible for not breaking the UnitActionSystem. Although I don’t know exactly where that error will pop up but I think only in the start of the next turn?
If so then I would handle it in the UnitActionSystem, when it’s the player’s turn check if the selected unit is dead, if so select the next alive unit. If no unit is left alive, go into some kind of Game Over state.

You should definitely be making your own modifications to the source code, that’s the best way to learn which is the primary purpose of the course. If you do break something you can always download the project files from the GitLab commit for that lecture and continue from there.
And beyond that I would definitely encourage you to keep building things even after the end of the course.

Also “breaking” things is all part of the learning process, you break something, then learn why it broke and fix it. The next time something similar breaks in a similar way you’ll remember what happened and fix it quickly. I actually had an example of that in a Livestream I made a while ago Code Monkey - The Power of EXPERIENCE! (Recognizing a past issue and fixing it quickly)

I’m glad you enjoyed the Livestream! Thanks!

6 Likes

Thanks, CodeMonkey. The takeaway lesson for me here is to maintain notes on each classes function (possibly including jobs of class methods) so that I can remind myself further down the line!

Anyhow, I’ve taken your advice and got the UnitActionSystem subscribing to the TurnSystem.Instance.OnTurnChanged event. I then added:

    private void TurnSystem_OnTurnChanged(object sender, EventArgs e)
    {
        if (!TurnSystem.Instance.IsPlayerTurn())
        {
            return;
        }

        if (selectedUnit != null)
        {
            return;
        }

        List<Unit> friendlyUnitList = UnitManager.Instance.GetFriendlyUnitList();
        if (friendlyUnitList.Count == 0)
        {
            // TODO: Update Unit Manager to check if friendlyUnitList.Count == 0
            //             and to implement GAME OVER scene if it is. Then remove this check. 
            Debug.Log("Your team is not responding! This can't be good.");
        }
        else
        {
            SetSelectedUnit(friendlyUnitList[0]);
        }
    }
5 Likes

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

Privacy & Terms