A quick side question

I know it is not the right topic to ask but I am on this lecture atm again cos I am trying to implement this logic into my game that has already been build upon… is there a way to update grid visuals only when the move action is done rather than entire time it is moving?

Edit: Better to say, has anyone done it already and can share the code so I don’t have to figure it out - my guess would be to use ActionComplete and than UpdateVisual or add list Valid grid Positions…

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

3 Likes

Thank you very much, I believe I might go with the same approach. Seems more logical to me and what you have done is simple yet brilliant :slight_smile:

Already implemented it… works like a charm. Thank you very much!

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

Privacy & Terms