Facepunch / sbox-issues

175 stars 12 forks source link

Updating a Dictionary doesn't update UI #6058

Open Dimmies opened 3 months ago

Dimmies commented 3 months ago

Describe the bug

Updating the value of a Dictionary doesn't seem to trigger BuildHash to update Razor panels.

Here's an example I made. When you press E (interact), it adds a new value to the Dictionary, but it wont update and show on the UI until you press Tab (Score) to update var1.

<root>
    <span style="color: lime;">@(testDict.Count)</span> // Stays the same unless we press Tab (Score) to update var1
    <span style="color: red;">@(var1)</span> // Updates just fine whenever we press Tab (Score)
    @foreach ( var text in testDict ) 
    {
        <div class="messaging-msg @(text.Value ? "messaging-received" : "messaging-sent")">@text.Key</div>
    }
</root>

@code
{

    private Dictionary<string, bool> testDict = new Dictionary<string, bool>();
    private int var1 = 0;

    protected async override void OnUpdate()
    {
        Log.Info(testDict.Count);

        if (Input.Released("interact"))
        {
            testDict.Add("TEST" + Game.Random.Next(), true);
        }

        if (Input.Released("Score"))
        {
            var1++;
        }
    }

    // Note: I've tried with and without GetHashCode
    protected override int BuildHash() => System.HashCode.Combine( testDict.GetHashCode(), var1 );
}

To Reproduce

  1. Create a Panel and define a Dictionary
  2. Add a value to the dictionary
  3. See that it doesn't update the panel

Expected behavior

Panel to update when dictionaries do

Media/Files

No response

Additional context

No response

DevulTj commented 3 months ago

I don't believe this is something for us to fix, GetHashCode for a dictionary isn't the correct way to use it.

Alternatively, you could make an extension method that lets you calculate hash codes for each keyvalue and combine it into a hash.

Also, If you control the whole system, you could update the panel manually with StateHasChanged()