Unit Selection defaults to Move, costs an action point

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.

The UnitActionSystem should not be triggering Actions in the same frame as you select a Unit, I don’t remember exactly on which lecture this was added but I can see the code on on the lecture “Action Points” at 04:35

Also when you select a unit, the units position should not be a valid move position, so even if you were to trigger the action in the same frame as you select the unit it shouldn’t move (assuming your camera angle is mostly pointing down)

Hi I am also having this problem. I have checked and I have all the code exactly the same code as shown at that part you said and according to my debug OnMouseButtonDown is only getting called once so I have no idea why this would be happening? This issue was definitely initially fixed in Unit Selection lecture, but for some reason now having the code in the Update on UnityActionSystem returning before the HandleSelectedAction if TryHandleUnitSelection = true, doesn’t seem to be working for me for some reason. Cannot work it out.

Hi 5GrandPint.
Let’s take a look at your UnitActionSystem.cs, and we’ll go from there.

1 Like

lol sorry for wasting your time, I suddenly realised when I was copy and pasting it all there and ensuring everything was exactly as I thought that I was using GetMouseButton instead of GetMouseButtonDown haha. Sorry and thanks for taking the time to help! :slight_smile:

1 Like

I think this is what I was doing anyway, I didn’t actually see that because I had moved the GetMouseButtonDown check into Update just for testing and the bug disappeared but I had already been off visual studio so I couldn’t undo to see if this is exactly what I had done before. I assume it is as when I change it to this the bug returns…

Privacy & Terms