Why is your HUD stuck on old numbers?

The panel doesn't know the number changed.

Why is your HUD stuck on old numbers?

The problem

The value changed. The panel didn't.

@inherits PanelComponent <root>    <div class="ammo">@Player.Ammo</div></root> @code {    public Player Player { get; set; }}

A Razor panel draws, then only redraws when something tells it to.

The fix

Hash what you show.

BuildHash returns a number made from everything on screen. When the number changes, the panel redraws.

Ammo changes→BuildHash() changes→panel redraws

The fix in code

One method under the markup.

@code {    public Player Player { get; set; }     protected override int BuildHash()        => System.HashCode.Combine( Player?.Ammo );}

Every value the HUD shows goes in. Our lobby HUD combines eight.

Watch out

Don't hash what moves every frame.

A value that changes every frame redraws the whole panel every frame.

Ammo, health, state
good to hash
Time.Now
a redraw every frame
Camera angle
a redraw when you turn

The rule

On screen? Put it in BuildHash.

Redraws when
the hash changes
Hash
every value you show
Never hash
what moves every frame