I’m not sure if this is the course code or something I messed up somewhere, but I was having an issue where clicking on a unit caused it’s Move action to trigger, showing all the spaces it can move to. As intended. But what was odd was that just doing that drained an action point. So if I just selected on a unit, then another, both would have had an action point drained even though neither has moved yet.
I think a better architecture would involve some sort of “action complete” callback or event or something, so that the action isn’t really done until it’s… well, done. And can be cancelled up to that point.
In the meantime, I fixed it by making this small change to the unit action system:
I took:
if (!selectedUnit.TrySpendActionPointsToTakeAction(selectedAction)) return;
GridPosition mouseGridPosition = LevelGrid.Instance.GetGridPosition(MouseWorld.GetPosition());
and changed it to:
if (!selectedUnit.CanSpendActionPointsToTakeAction(selectedAction)) return;
GridPosition mouseGridPosition = LevelGrid.Instance.GetGridPosition(MouseWorld.GetPosition());
This way, it’s still checking to make sure you can spend the points, but it’s not actually doing so yet.
Then I added the TrySpendActionPointsToTakeAction method into the if block before, right before TakeAction():
SetBusy();
selectedUnit.TrySpendActionPointsToTakeAction(selectedAction);
selectedAction.TakeAction(mouseGridPosition, ClearBusy);
At this point, there’s really nothing to “Try” since the check was already made earlier to see if you had enough action points. (You couldn’t even select the action if you didn’t have enough points… something I may want to reconsider at some point.) But here, it just checks again (unnecessarily) and then spends the points.
This keeps my code close to the course’s code, but fixes that little problem. Now I can go back and forth selecting my units without draining their action points until I actually take the action.