My code is very different from the code in the course, but here’s a quick rundown of what it basically comes down to.
I added one more event to the UnitActionSystem
;
public event EventHandler OnActionCompleted;
I then added a new method called HandleActionCompleted
to clear the busy state and invoke the new event
private void HandleActionCompleted()
{
ClearBusy();
OnActionCompleted?.Invoke(this, EventArgs.Empty);
}
This replaces ClearBusy
in HandleSelectedAction
when starting the action
SetBusy();
selectedAction.TakeAction(mouseGridPosition, HandleActionCompleted); // <- HERE
OnActionStarted?.Invoke(this, EventArgs.Empty);
Next, I went to GridSystemVisual
and subscribed to this event as well as OnActionStarted
// In Start()
UnitActionSystem.Instance.OnActionStarted += UnitActionSystem_OnActionStarted;
UnitActionSystem.Instance.OnActionCompleted += UnitActionSystem_OnActionCompleted;
I also added an isActive
flag
private bool isActive = true;
Implement these with
private void UnitActionSystem_OnActionStarted(object sender, EventArgs e)
{
isActive = false;
UpdateGridVisual();
}
private void UnitActionSystem_OnActionCompleted(object sender, EventArgs e)
{
isActive = true;
UpdateGridVisual();
}
Lastly, in the UpdateGridVisual
I exit the method early if we are not active
private void UpdateGridVisual()
{
HideAllGridPosition();
if (!_isActive) return;
// ... rest of the code
}
In my case the grid positions are hidden while the action is being performed and then appear again, updated, when the action is completed