Update Grenade Visuals

I’ve altered my code so that each player only gets one grenade. If I use the grenade on my first turn, it will show that I can throw the grenade again (even though I can’t). However, if I switch to another action (like move) & back to grenades, the visual updates properly.

How do I call the updated visual manually?

Sorry I missed this last week.

I think the most straightforward way to do this to take a look at UnitActionSystemUI.cs.

We have an event we’re subscribing to UnitActionSystem.OnActionStarted.
In the event handler, right now, we’re simply updating the number of points available. Try adding UpdateSelectedVisual(); to this event handler

private void UnitActionSystem_OnActionStart(object sender, EventArgs e)
{
     UpdateActionPoints();
     UpdateSelectedVisual();
}

Now this by itself won’t actually do much, because the UnitActionSystem will still show the current selected action as the grenade, and it will just set the action. UpdateSelectedActionVisual() actuall calls

actionButtonUI.UpdateSelectedVisual();

on each actionButtonUI.

Let’s play around with the UpdateSelectedVisual() method in ActionButtonUI

    public void UpdateSelectedVisual()
    {
        BaseAction selectedBaseAction = UnitActionSystem.Instance.GetSelectedAction();
        selectedGameObject.SetActive(selectedBaseAction == baseAction);
    }

Here, we can add one more line to the method to make the button interactable if the selectedBaseAction has not acted this turn and if the unit has enough points to take that action.

    public void UpdateSelectedVisual()
    {
        BaseAction selectedBaseAction = UnitActionSystem.Instance.GetSelectedAction();
        button.interactable = !selectedBaseAction.HasActedThisTurn() && selectedBaseAction.GetActionPointsCost() <=
            selectedBaseAction.GetUnit().GetActionPoints();
        selectedGameObject.SetActive(selectedBaseAction == baseAction);
    }

Hmmm, I just tested this and it failed a bit… I’ll need to take a closer look at this later.

I solved it!

Under the Unit script, I added the following code:

    public int GetGrenadePoints()
    {
        //If anything needs this value, they look at this rather than a value that could accidentally be manipulated
        return grenadeActionPoints;
    }
    
    private void GrenadeAction_OnGrenadeActionStarted(object sender, EventArgs e)
    {
        SpendGrenadePoints(1);
    }

    public void SpendGrenadePoints(int amount)
    {
        //Change the count
        grenadeActionPoints -= amount;

        //Send out a message to update values
        OnAnyGrenadePointsChanged.Invoke(this, EventArgs.Empty);
    }

From there, under GrenadeAction, I added the following:

        if(unit.GetGrenadePoints() <= 1)
        {
            grenadesLeftBool = false;
            GridSystemVisual.Instance.UpdateVisualForGrenade();
        }

Under the GridSystemVisual script, I added the following item:

    public void UpdateVisualForGrenade()
    {
        UpdateGridVisual();
    }

I apologize…I can see I’ve missed another line of code (to see how to keep track of the grenades), but I think this should be enough of a hint for others in the future. Hope this helps others!

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

Privacy & Terms